谷粒商城—商品服务—属性分组+品牌管理(70~75)

本文介绍了电商商品管理系统中SPU和SKU的概念,详细解析了商品数据库表结构,并展示了如何根据三级分类ID查询基本属性列表的实现过程,包括Controller、Service和SQL查询。同时,阐述了查询属性分组完整信息的步骤,包括获取属性分组及完整路径的方法。最后,提到了MybatisPlus分页插件的配置和使用。
摘要由CSDN通过智能技术生成

一.SPU 和 SKU 概念:

二.商品数据库 表结构 梳理:

三.根据 三级分类 id ,查询 基本属性 列表

            1)请求:http://127.0.0.1:88/api/product/attrgroup/list/225
            2)Controller:

    /**
     * 商品管理,根据 三级分类 id ,查询 基本属性 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long catelogId) {

        PageUtils page = attrGroupService.queryPage(params, catelogId);

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

            3)Service:

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

            4)查询 SQL: 
                         a:打印 SQL:

logging:
  level:
    com.guigu.gulimail.product.dao: debug

                         b:在 sql:

--  2  2021-04-01 11:20:36.546 DEBUG 5462 --- [o-11000-exec-10] c.g.g.p.dao.AttrGroupDao.selectPage      : ==>
 SELECT attr_group_id,attr_group_name,sort,descript,icon,catelog_id
 FROM pms_attr_group
 WHERE (catelog_id = 225)
 LIMIT 10;
-- ==>      Total: 3

​​​​​​​                         c:表结构: 


            5)结果报文: 

{
    "msg": "success",
    "code": 0,
    "page": {
        "totalCount": 3,
        "pageSize": 10,
        "totalPage": 1,
        "currPage": 1,
        "list": [
            {
                "attrGroupId": 1,
                "attrGroupName": "信息",
                "sort": 0,
                "descript": "荣耀",
                "icon": "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/1.jpg",
                "catelogId": 225,
                "catelogPath": null
            },
            {
                "attrGroupId": 2,
                "attrGroupName": "基本信息",
                "sort": 0,
                "descript": "华为",
                "icon": "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/1.jpg",
                "catelogId": 225,
                "catelogPath": null
            },
            {
                "attrGroupId": 4,
                "attrGroupName": "芯片",
                "sort": 0,
                "descript": "CPU型号",
                "icon": "https://mall-fire.oss-cn-shenzhen.aliyuncs.com/1.jpg",
                "catelogId": 225,
                "catelogPath": null
            }
        ]
    }
}




 

四.查询 属性分组 完整信息(包含 属性 完整路径)

            1)请求:http://127.0.0.1:88/api/product/attrgroup/info/225

            2)Controller:

    /**
     * 查询 属性分组 完整信息。
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {

        //查询 属性分组 完整信息。
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        /**
         *     根据 三级分类 id ,查找完整路径,
         *     【父 子 孙】
         */
        Long catelogId = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(paths);

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

            3)Service:

    //[2,25,225]
    @Override
    public Long[] findCatelogPath(Long attrGroupId) {

        ArrayList<Long> list = new ArrayList<>();

        List<Long> parentPath = findParentPath(attrGroupId, list);

        //反转 list 元素
        Collections.reverse(parentPath);

        //将 集合 转换为 数组。
        return (Long[]) parentPath.toArray(new Long[parentPath.size()]);
    }

    /**
     * 递归查找 所有菜单的子菜单
     */
    private List<Long> findParentPath(Long catelogId, List<Long> paths) {

        CategoryEntity byId = this.getById(catelogId);
        //收集 当前节点 id。
        paths.add(catelogId);
        // 如果 当前节点 有 父节点。
        if (byId.getParentCid() != 0) {
            List<Long> parentPath = findParentPath(byId.getParentCid(), paths);
        }

        return paths;
    }





​​​​​​​ 

五.安装 MybatisPlus 分页插件:

/**
 * @Author zhangsan
 * @Date 2021/3/16 8:00 下午
 * @Version 1.0
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.guigu.gulimail.product.dao")
public class MybatisPlusPageConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值