SpringCloud(学习 商品服务-属性分组的开发)六


前言

本文主要记录开发商品服务的过程,及查询、新增、修改等功能的开发。


一、属性分组表关系

属性分组-规格参数-销售属性-三级分类 关联关系

属性分组关系表

商品属性表关系

商品属性关系表

二、查询功能的开发

查询功能的开涉及到多个字段的模糊查询,模糊查询的参数使用key来接收,并通过key来查询列表
查询功能

1、接口编写

在controller层编写接口,利用queryPage()方法来查询列表,catelogId为当前节点id,params为分页、条数、key信息。

 @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId")Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

2、创建方法

在service层创建queryPage方法

PageUtils queryPage(Map<String, Object> params, Long catelogId);

3、实现方法

实现该方法

  @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //模糊查询的参数
        String key = (String) params.get("key");
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catelogId == 0){
           IPage<AttrGroupEntity>page = this.page(new Query<AttrGroupEntity>().getPage(params),
                   wrapper);
            return new PageUtils(page);
        }else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity>page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
    }

三、新增和修改功能的开发

新增和修改功能主要是其中所属分类的选择和回显的开发。

新增和修改

1、所属分类的连级选择

调用之前的三级分类列表的接口,但是该接口会在第三级字段里面添加一个空的子集列表,这会导致组件显示异常,所以需要将该集合从返回数据中去除。

连级选择异常

在实体类children字段上利用@JsonInclude注解判断该字段是否为空,为空则不返回

/**
	 * 子集合
	 */
	@JsonInclude(JsonInclude.Include.NON_EMPTY) //为空不带
	@TableField(exist = false) //不是表中的字段,用来标记排除
	private List<CategoryEntity>children;
用法解释
ALWAYS默认策略,任何情况都执行序列化
NON_NULL非空
NON_ABSENTnull的不会序列化,但如果类型是AtomicReference,依然会被序列化
NON_EMPTYnull、集合数组等没有内容、空字符串等,都不会被序列化
NON_DEFAULT如果字段是默认值,就不会被序列化
CUSTOM此时要指定valueFilter属性,该属性对应一个类,用来自定义判断被JsonInclude修饰的字段是否序列化
USE_DEFAULTS当JsonInclude在类和属性上都有时,优先使用属性上的注解,此时如果在序列化的get方法上使用了JsonInclude,并设置为USE_DEFAULTS,就会使用类注解的设置

2、回显

由于通过点击事件后,他会回显为第三级的id,但是该组件需要完整的分类id,所以需要返回完整id的字段用于回显。

三级分类回显

1、实体类添加字段

实体类添加字段用来返回完整三级分类的id

/**
	 * 所属分类全id
	 */
	@TableField(exist = false)
	private Long[] catelogIds;

2、编写接口层

用于将原来返回的数据添加一个完整id的属性,使用findCatelogPath方法查找完整Id。

@RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[]path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogIds(path);
        return R.ok().put("attrGroup", attrGroup);
    }

3、创建方法

在service层创建findCatelogPath方法,catelogId为当前节点id

 Long[] findCatelogPath(Long catelogId);

4、实现方法

实现findCatelogPath方法,利用传过来的第三级id创建findParentPath方法递归查找父id,并最终返回完整id。

  @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId,paths);
        //逆序返回的id数组
        Collections.reverse(parentPath);
        return paths.toArray(new Long[parentPath.size()]);
    }

    /**
     * 
     * @param catelogId 当前节点id
     * @param paths 目前收集的id
     * @return 完整的id
     */
    private List<Long>findParentPath(Long catelogId,List<Long> paths){
        //1、收集当前节点id
        //2、当前节点存在父节点就递归本方法
        //3、收集完返回
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

Collections工具类方法:

方法解释
reverse(List list)反转列表中元素的顺序
shuffle(List list)对List集合元素进行随机排序
sort(List list)根据元素的自然顺序 对指定列表按升序进行排序
sort(List list, Comparator c)根据指定比较器产生的顺序对指定列表进行排序
swap(List list, int i, int j)在指定List的指定位置i,j处交换元素
rotate(List list, int distance)当distance为正数时,将List集合的后distance个元素“整体”移到前面;当distance为 负数时,将list集合的前distance个元素“整体”移到后边。该方法不会改变集合的长度

总结

本文主要讲了属性分组中,查询、新增、修改功能的开发,并记录开发过程中遇到的返回数据去除注解以及Collections工具类的使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天将降大任于我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值