第二十一章第六节:属性分组-关联功能查询和移除

1、关联功能列表查询

1>定义查询接口

com.applesnt.onlinemall.product.service.AttrService

    List<AttrEntity> getRetaionAttr(Long attrgroupId);

2>定义查询接口实现

com.applesnt.onlinemall.product.service.impl.AttrServiceImpl


    /*注入关联关系dao*/
    @Autowired
    AttrAttrgroupRelationDao relationDao;
    
    /*根据分组id 查找规格参数相关属性*/
    @Override
    public List<AttrEntity> getRetaionAttr(Long attrgroupId) {
        /*根据attrgroupId查询出关联关系*/
        List<AttrAttrgroupRelationEntity> relationEntities =  relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().
                eq("attr_group_id",attrgroupId));
        /*从relationEntities中收集attrid*/
        List<Long> attrids = relationEntities.stream().map((obj)->{
            return obj.getAttrId();
        }).collect(Collectors.toList());

        if(attrids ==null || attrids.size() ==0){
            return null;
        }
        /*根据attrids集合查询规格参数*/
        List<AttrEntity> attrEntities = this.baseMapper.selectBatchIds(attrids);
        return attrEntities;
    }

3>controller调用

com.applesnt.onlinemall.product.controller.AttrGroupController


    /*注入属性分组service*/
    @Autowired
    private AttrService attrService;

    /*根据分组id 查找规格参数相关属性*/
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrService.getRetaionAttr(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }

2、关联功能移除

1、定义一个vo 接收前端传递的参数

com.applesnt.onlinemall.product.vo.AttrGroupRetaionVo

package com.applesnt.onlinemall.product.vo;

import lombok.Data;

@Data
public class AttrGroupRetaionVo {
    private Long attrId;
    private Long attrGroupId;
}

2、定义删除接口

com.applesnt.onlinemall.product.service.AttrService

void deleteRetaion(List<AttrGroupRetaionVo> vos);

3、定义删除接口实现

com.applesnt.onlinemall.product.service.impl.AttrServiceImpl

/*批量删除*/
    @Override
    public void deleteRetaion(List<AttrGroupRetaionVo> vos) {
        for (AttrGroupRetaionVo vo : vos) {
            relationDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>().
                    eq("attr_id",vo.getAttrId()).
                    eq("attr_group_id",vo.getAttrGroupId()));
        }
    }

4、controller调用

com.applesnt.onlinemall.product.controller.AttrGroupController

    @RequestMapping("/attr/relation/delete")
    public R deleteRetaion(@RequestBody List<AttrGroupRetaionVo> vos){
        attrService.deleteRetaion(vos);
        return R.ok();
    }
posted @ 2021-06-19 16:02  努力的校长  阅读(51)  评论(0)    收藏  举报