谷粒商城项目实战——05商品服务之品牌分类

四. 品牌分类

1. 实现分页

1. 问题
  1. 查询出来的数据条数显示只有0条, 且没有实现分页功能

    image-20210516212533747

2. 解决
1. 官网
  1. 分页插件 | MyBatis-Plus (baomidou.com)

    image-20210516211752373

2. config配置类
  1. 创建配置类

    @Configuration
    // 开启事务
    @EnableTransactionManagement
    @MapperScan("com.hjf.gulimall.product.dao")
    public class MyBatisConfig {
        // 1. 引入分页插件
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
            paginationInterceptor.setOverflow(true);
            // 设置最大单页限制数量,默认 500 条,-1 不受限制
            paginationInterceptor.setLimit(500);
            // 开启 count 的 join 优化,只针对部分 left join
            paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
            return paginationInterceptor;
        }
    }
    

    image-20210516213036552

  2. 效果

    image-20210516212937794

2. 查询功能

1. 问题
  1. 品牌模糊查询方法查询时没有传入任何参数, 所以需要重写此方法

    image-20210516213444207

2. 解决
  1. service层代码

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 1. 获取key
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            // 如果id相同或者name相似, 则返回
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
            new Query<BrandEntity>().getPage(params), wrapper
        );
        return new PageUtils(page);
    }
    
  2. 效果

    image-20210516214002500

3. 关联分类

0. 前提
1. 文件替换
  1. 将项目中的common和product中的文件替换成课件中的文件

  2. 效果

    image-20210516220301581

2. 功能说明
  1. 每个品牌都是自己所属的分类, 同一个品牌可能属于多个分类, 一个分类有多个分类
1. 获取品牌分类列表
1. Controller层
  1. 接口/product/categorybrandrelation/catelog/list

    接口文档: 15、获取品牌关联的分类 - 谷粒商城 - 易文档 (easydoc.xyz)

    image-20210516222856173
  2. 代码

    /**
     * 获取当前品牌关联的所有分类关系
     * @param brandId
     * @return
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
            new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    
        return R.ok().put("data", data);
    }
    
2. 添加关联关系
1. 问题
  1. 默认生成的添加方法需要传入品牌名和分类名, 如果每次都是通过品牌id和分类id去数据库中读取对应的值, 会极大的影响数据库的性能, 所有我们需要在添加关联关系时, 将品牌名和分类名一同添加到中间表中

image-20210516224208109

2. 代码
  1. Controller层

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        // categoryBrandRelationService.save(categoryBrandRelation);
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }
    
  2. Service接口

    void saveDetail(CategoryBrandRelationEntity categoryBrandRelation);
    
  3. Service实现

    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;
    
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 1. 根据品类id和分类id获取名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        // 2. 关联关系实体类中设置品类名和分类名
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    
    }
    

    image-20210516230038579

3. 效果
  1. 测试

    image-20210516225538790

  2. 响应数据

    image-20210516225610177

  3. 数据库

    image-20210516230252716

5. 优化

添加品牌分类时, 并没有做联表, 而是直接在中间表中冗余存储了品牌名和分类名,

如果品牌名和分类名发生修改时, 并不会同步过来.

所以在修改品牌和分类时, 不能只修改品牌表和分类表, 还需要修改中间的关联表

1. 品牌修改
  1. controller层

    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
        // brandService.updateById(brand);
        brandService.updateDetail(brand);
        return R.ok();
    }
    
  2. BrandService接口

    void updateDetail(BrandEntity brand);
    
  3. BrandServiceImpl实现

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    
    @Override
    public void updateDetail(BrandEntity brand) {
        // 保证冗余字段的数据一致
        // 通过品牌id修改品牌表
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())) {
            // 同步更新其他关联表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
            // TODO: 更新其他关联
        }
    }
    

    image-20210516234508759

  4. CategoryBrandRelationService接口

    /**
     * 修改关联数据
     * @param brandId
     * @param name
     */
    void updateBrand(Long brandId, String name);
    
  5. CategoryBrandRelationServiceImpl实现

    /**
     * 修改关联数据
     * @param brandId
     * @param name
     */
    @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));
    }
    
2. 测试
  1. 修改品牌名

    image-20210516233559306

  2. 关联表中的数据会同步修改

    image-20210516233733410

    image-20210516233636651

2. 分类修改
  1. CategoryController

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        // categoryService.updateById(category);
        categoryService.updateCascade(category);
        return R.ok();
    }
    
  2. CategoryService

    /**
     * 级联更新所有关联的数据
     * @param category
     */
    void updateCascade(CategoryEntity category);
    
  3. CategoryServiceImpl

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    
    /**
     * 级联更新所有关联的数据
     * @param category
     */
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
    }
    
  4. CategoryBrandRelationService

    void updateCategory(Long catId, String name);
    

    image-20210516235400679

  5. CategoryBrandRelationServiceImpl

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }
    
  6. CategoryBrandRelationDao

    @Mapper
    public interface CategoryBrandRelationDao extends BaseMapper<CategoryBrandRelationEntity> {
        void updateCategory(@Param("cateId") Long catId, @Param("name") String name);
    }
    

    image-20210517000642942

  7. CategoryBrandRelationDao.xml

    <update id="updateCategory">
        update `pms_category_brand_relation`
        set catelog_name = #{name}
        where catelog_id = #{cateId}
    </update>
    

    image-20210517000747223

2. 演示
  1. 修改分类名称

    image-20210517000819952

  2. 关联表中的数据会同步更改

    image-20210517000905410

    image-20210517000917414

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值