【雷丰阳-谷粒商城 】【分布式基础篇-全栈开发篇】【07】【商品服务】分页查询_品牌分类关联与级联更新


持续学习&持续更新中…

守破离


【雷丰阳-谷粒商城 】【分布式基础篇-全栈开发篇】【07】【商品服务】分页查询_品牌分类关联与级联更新

分页查询

@Configuration
@EnableTransactionManagement //开启事务
@MapperScan("com.atguigu.gulimall.product.dao")
public class MyBatisConfig {

    //引入分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
         paginationInterceptor.setOverflow(true);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        paginationInterceptor.setLimit(1000);
        return paginationInterceptor;
    }

}
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        IPage<AttrGroupEntity> page;
        IPage<AttrGroupEntity> queryPage = new Query<AttrGroupEntity>().getPage(params);
        if (catelogId == 0) { // 规定在分页查询下,id为0,代表查询所有
            page = this.page(queryPage, new QueryWrapper<>());
        } else {
            /*
            分页查询:
                # 假设每页15条(pageSize = 15)
                # 假设查询第n页 (n >= 1)
                #   SELECT * FROM student LIMIT (n - 1) * pageSize, pageSize;
                    SELECT * FROM student LIMIT 0, 15; # 查询第一页
                    SELECT * FROM student LIMIT 15, 15; # 查询第二页

            总数量:101条
            每一页显示20条
            公式:总页数 = (总数量  +  每页的数量   -   1) / 每页的数量
                        = ( 101   +    20        -   1) / 20
             */
            /*
            该请求这么查询:
            select * from pms_attr_group
            where catelog_id = catelogId and (attr_group_id = key or attr_group_name like '%key%')
             */
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
            Object key = params.get("key");
            if (!StringUtils.isEmpty(key)) {
                wrapper.and((obj) -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                });
            }
            page = this.page(queryPage, wrapper);
        }
        return new PageUtils(page);
    }

分页请求参数:

{
   page: 1,//当前页码
   limit: 10,//每页记录数
   sidx: 'id',//排序字段
   order: 'asc/desc',//排序方式
   key: '华为'//检索关键字
}

分页返回数据:

{
	"msg": "success",
	"code": 0,
	"page": { 
		"totalCount": 0, //总记录数
		"pageSize": 10,  //每页大小
		"totalPage": 0,  //总页码
		"currPage": 1, //当前页码
		"list": [{  //当前页所有数据
			"brandId": 1,
			"name": "aaa",
			"logo": "abc",
			"descript": "华为",
			"showStatus": 1,
			"firstLetter": null,
			"sort": null
		}]
	}
}

为数据库表设计冗余字段

  • 对数据库大表,比如pms_category_brand_relation pms_brand pms_category
  • pms_brand和pms_category这两张表的关联表pms_category_brand_relation
  • 设计的时候可以不用设计上brand_name和catelog_name
  • 但是每次需要使用的时候就去关联查询会对数据库的性能有非常大的影响
  • 所以,我们对于大表数据从不做关联,因此就可以设计这两个冗余字段brand_name和catelog_name:
	create table gulimall_pms.pms_category_brand_relation
	(
	    id           bigint auto_increment
	        primary key,
	    brand_id     bigint       null comment '品牌id',
	    catelog_id   bigint       null comment '分类id',
	    brand_name   varchar(255) null,
	    catelog_name varchar(255) null
	)
	    comment '品牌分类关联';

冗余字段带来的问题以及处理

  • 如果某张表中有其他表的冗余字段(pms_category_brand_relation中有pms_brand和pms_category的字段),那么,当其他表中的数据要更新时
  • 也就是当pms_brand和pms_category更新的时候
  • 我们也要更新pms_category_brand_relation这张表中受影响的字段(brand_name,catelog_name)
  • 品牌名发生变化,需要更新;
  • 分类名发生变化,需要更新;
  • 也就是级联更新

品牌名发生变化,进行级联更新

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
//        brandService.updateById(brand);
        brandService.updateCascade(brand);
        return R.ok();
    }
    @Transactional
    @Override
    public void updateCascade(BrandEntity brand) {
        this.updateById(brand); // 先修改自己的数据

        //TODO 当brand表中的某些字段被其他表使用,并且该字段发生了更新,那么也需要修改那些表的信息
        if (!StringUtils.isEmpty(brand.getName())) { // 品牌名发生了变化
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
        }
    }
    @Override
    public void updateBrand(Long brandId, String brandName) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(brandName);

        this.update(entity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

分类名发生变化,进行级联更新

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateCascade(category);

        return R.ok();
    }
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);

        //TODO 当category表中的某些字段被其他表使用,并且该字段发生了更新,那么也需要修改那些表的信息
        if (!StringUtils.isEmpty(category.getName())) { // 分类名发生了变化
            categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
        }
    }
    @Override
    public void updateCategory(Long catId, String catelogName) {
        this.baseMapper.updateCategory(catId, catelogName);
    }
@Mapper
public interface CategoryBrandRelationDao extends BaseMapper<CategoryBrandRelationEntity> {

    void updateCategory(@Param("catId") Long catId,@Param("catelogName") String catelogName);

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.atguigu.gulimall.product.dao.CategoryBrandRelationDao">

	<!-- ...... -->

    <update id="updateCategory">
        UPDATE pms_category_brand_relation SET catelog_name = #{catelogName} WHERE catelog_id = #{catId}
    </update>

</mapper>

参考

雷丰阳: Java项目《谷粒商城》Java架构师 | 微服务 | 大型电商项目.


本文完,感谢您的关注支持!


最近更新

  1. TCP协议是安全的吗?

    2024-06-09 10:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-09 10:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-09 10:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-09 10:04:03       20 阅读

热门阅读

  1. RuoyiAdmin项目搭建及Docker 部署备忘

    2024-06-09 10:04:03       13 阅读
  2. 学习分享-注册中心Naocs的优雅上下线

    2024-06-09 10:04:03       9 阅读
  3. Redisson 源码分析 —— 调试环境搭建

    2024-06-09 10:04:03       9 阅读
  4. 面试题之webpack与vite系列

    2024-06-09 10:04:03       7 阅读