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


持续学习&持续更新中...

守破离


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

分页查询

java 复制代码
@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;
    }

}
java 复制代码
    @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);
    }

分页请求参数:

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

分页返回数据:

java 复制代码
{
	"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:
sql 复制代码
	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)
  • 品牌名发生变化,需要更新;
  • 分类名发生变化,需要更新;
  • 也就是级联更新

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

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

        //TODO 当brand表中的某些字段被其他表使用,并且该字段发生了更新,那么也需要修改那些表的信息
        if (!StringUtils.isEmpty(brand.getName())) { // 品牌名发生了变化
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
        }
    }
java 复制代码
    @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));
    }

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

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

        return R.ok();
    }
java 复制代码
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);

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

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

}
xml 复制代码
<?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架构师 | 微服务 | 大型电商项目.


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


相关推荐
离陌在学C#1 小时前
C# 重载与重写:深入理解面向对象编程的核心概念
java·c#
洛阳泰山1 小时前
AI 应用层被 Python 卷成红海,为什么我偏要用 Java 造一个 RAG + 工作流引擎?
java·人工智能·后端
二十雨辰2 小时前
[Java]-Spring面试题
java·开发语言
老马历写记2 小时前
Maven POM 依赖管理总结
java·maven·system·pom·optional
古法安卓2 小时前
Android-车机 GNSS 定位数据接收问题排查
android·java·android studio
蔬菜_2 小时前
前端转全栈-day5(数组、list、set)
java·前端·数据结构·list
萧瑟余晖2 小时前
Java深入解析篇二十二之虚拟线程
java·开发语言
OuO-22 小时前
笔试强训 Day 34:ISBN 号码、kotori 和迷宫、矩阵最长递增路径
java·算法·矩阵
油丶酸萝卜别吃3 小时前
Java 集合类全景介绍
java·开发语言
钱栈up3 小时前
"Flowable 工作流引擎进阶实战(高级篇):任务分配、流程变量与监听器"
java