• 2022谷粒商城学习笔记(七)属性分组相关功能


    前言

    本系列博客基于B站谷粒商城,只作为本人学习总结使用。这里我会比较注重业务逻辑的编写和相关配置的流程。有问题可以评论或者联系我互相交流。原视频地址谷粒商城雷丰阳版。本人git仓库地址Draknessssw的谷粒商城


    获取属性分组

    在商品属性分组Controller中新建

    在这里插入图片描述
    根据商品id查询属性分组信息

     /**
         * 列表
         */
        @RequestMapping("/list/{catelogId}")
        //@RequiresPermissions("product:attrgroup:list")
        public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
            PageUtils page = attrGroupService.queryPage(params, catelogId);
    
            return R.ok().put("page", page);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    实现类

    要是输入的值非空,则MybatisPlus的匹配条件添加上商品分类id或者组名的模糊匹配。而要是输入商品分类id查询,不输入分类id(默认为0)就查询全部,输入则匹配id进行查询。最后返回分页查询的结果。

     @Override
        public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
            if (!StringUtils.isEmpty(key)) {
                // 根据key多字段模糊查询
                // and (() or ())
                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 {
                // 根据catelogId查询
                wrapper.eq("catelog_id", catelogId);
                IPage<AttrGroupEntity> page = this.page(
                        new Query<AttrGroupEntity>().getPage(params),
                        wrapper
                );
                return new PageUtils(page);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    但是实际上,即便查询的结果是以分页查询返回,但是这里仍然需要分页配置
    在这里插入图片描述

    package com.xxxx.gulimall.product.config;
    
    import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    @Configuration
    @MapperScan("com.xxxx.gulimall.product.dao")
    @EnableTransactionManagement
    public class MybatisPlusConfig {
    
        /**
         * 分页插件
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
            paginationInterceptor.setOverflow(true);
            // 设置最大单页限制数量,默认 500 条,-1 不受限制
            paginationInterceptor.setLimit(1000);
            // 开启 count 的 join 优化,只针对部分 left join
            paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
            return paginationInterceptor;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    查找当前类别的路径功能

    在这里插入图片描述
    实现类
    由于查找父分类id是递归查找,所以路径得逆序输出

     /**
         * 根据catelogId查询所有父分类ID
         */
        @Override
        public Long[] findCatelogPath(Long catelogId) {
            List<Long> paths = new ArrayList<>();
            // 递归查询父类
            paths = findParentPath(catelogId, paths);
            // 逆序,父在前
            Collections.reverse(paths);
            return paths.toArray(new Long[paths.size()]);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
     /**
         * 递归查找父路径
         */
        private List<Long> findParentPath(Long catelogId, List<Long> paths) {
            paths.add(catelogId);
            CategoryEntity category = this.getById(catelogId);
            if (category.getParentCid() != 0) {
                findParentPath(category.getParentCid(), paths);
            }
            return paths;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    获取当前品牌关联的所有分类列表

    在这里插入图片描述
    通过品牌和分类关联实体类,匹配请求参数中品牌id。

    	@Autowired
        private CategoryBrandRelationService categoryBrandRelationService;
    
    • 1
    • 2
    @GetMapping(value = "/catelog/list")
        //@RequiresPermissions("product:categorybrandrelation:list")
        public R catelogList(@RequestParam Map<String, Object> params, @RequestParam("brandId") Long brandId){
    
            List<CategoryBrandRelationEntity> data = categoryBrandRelationService.
                    list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    
            return R.ok().put("data", data);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    保存当前品牌对应分类功能

    在这里插入图片描述

    /**
         * 保存
         */
        @RequestMapping("/save")
        //@RequiresPermissions("product:categorybrandrelation:save")
        public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
    		categoryBrandRelationService.saveDetail(categoryBrandRelation);
    
            return R.ok();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    实现类

    首先注入品牌和分类的dao

    	@Autowired
        private BrandDao brandDao;
    
        @Autowired
        private CategoryDao categoryDao;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    查询品牌和分类的详细信息,本来品牌和分类管理是没有具体的名称信息,也就是前端并没有显示,这里保存具体的关联信息。

     @Override
        public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
            Long brandId = categoryBrandRelation.getBrandId();
            Long catelogId = categoryBrandRelation.getCatelogId();
    
            //1、查询品牌详细信息
            BrandEntity brandEntity = brandDao.selectById(brandId);
            //2、查询分类详细信息
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
    
            categoryBrandRelation.setBrandName(brandEntity.getName());
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
    
            // 保存到数据库中
            this.baseMapper.insert(categoryBrandRelation);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    商品表冗余字段更新

    在这里插入图片描述

    /**
         * 修改
         */
        @RequestMapping("/update")
        //@RequiresPermissions("product:brand:update")
        public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
    		brandService.updateDetail(brand);
    
            return R.ok();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    实现类,添加事务注解,更新不成功回退
    根据商品id和名称进行更新商品品牌关联表

    @Transactional
        @Override
        public void updateDetail(BrandEntity brand) {
            //保证冗余字段的数据一致
            this.updateById(brand);
    
            if (!StringUtils.isEmpty(brand.getName())) {
                //同步更新其他关联表中的数据
                categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
    
                //TODO 更新其他关联
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    对应更新方法的实现类
    根据商品id进行更新

    @Override
        public void updateBrand(Long brandId, String name) {
            CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
            relationEntity.setBrandId(brandId);
            relationEntity.setBrandName(name);
            this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    但是此时,分类信息的冗余信息也需要同步更新

    在这里插入图片描述

    /**
         * 修改
         */
        @RequestMapping("/update")
        //@RequiresPermissions("product:category:update")
        public R update(@RequestBody CategoryEntity category){
    		categoryService.updateCascade(category);
    
            return R.ok();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    实现类

    传入要更新的分类id和分类名称

    /**
         * 级联更新所有关联数据
         * @param category
         */
    
        @Transactional
        @Override
        public void updateCascade(CategoryEntity category) {
            this.updateById(category);
            categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    实现类

    @Override
        public void updateCategory(Long catId, String name) {
            this.baseMapper.updateCategory(catId,name);
        }
    
    • 1
    • 2
    • 3
    • 4

    对应的dao层中创建

     void updateCategory(@Param("catId") Long catId,@Param("name") String name);
    
    • 1

    update语句

    <update id="updateCategory">
            UPDATE
                pms_category_brand_relation
            SET catelog_name = #{name}
            WHERE catelog_id = #{catId}
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    使用 OpenTelemetry 构建 .NET 应用可观测性(3):.NET SDK 概览
    分享一下我家网络机柜,家庭网络设备推荐
    win32程序步骤
    【区块链实战】什么是 hash 函数,区块链用 hash 函数来干什么
    RepVGG 核心讲解
    A. Two Elevators
    PHP:对象接口
    虚拟主播是什么,有什么技术原理?- 沉睡者IT
    含文档+PPT+源码等]精品基于Nodejs实现的医院患者服务系统[包运行成功]
    python pip 安装 Crypto 不可用解决方案
  • 原文地址:https://blog.csdn.net/qq_44737138/article/details/126387406