分布式项目-属性管理( 10 、11 、12)

文章讨论了在使用MybatisPlus时的分页配置,包括事务管理、分页拦截器的设置。同时,提出了在品牌和类别这种多对多关系中如何处理数据一致性问题,特别是当品牌或类别名称变更时。文章还展示了如何在VO层和Impl层处理保存属性信息,以及在查询规格参数和属性组时如何关联查询类别名称和属性组的名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【今日成果】:

在这里插入图片描述
在这里插入图片描述
//允许多组的条件查询;
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

【快速回顾】:

(1):
前端传过来的数据和后端数据库中的结构不一致; //不能频繁添加临时字段!!!

【具体细节】:

【mybatis分页配置文件】:

@Configuration
@MapperScan("com.msb.mall.product.dao")
@EnableTransactionManagement // 开启事务
public class MybatisPlusConfig {

    // 旧版
    @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;
    }
}

【 品牌 ==》 类别 】:

在这里插入图片描述
品牌和类别是多对多的关系 , 维护多对多关系的一张表。
《数据一致性问题》——catelog_name这一个字段 , 如果品牌名称/类别名称改了 ,后两个字段该怎么办呢 ?

【冗余数据同步】:

针对品牌名称和类别名称这类冗余的数据 , 我们需要做同步的处理;

【 VO 】:

[ Controller ]:

     @RequestMapping("/save")
    public R save(@RequestBody AttrVO vo){
        attrService.saveAttr(vo);

        return R.ok();
    }

[ Impl ]:

    @Transactional
    @Override
    public void saveAttr(AttrVO vo) {
        //1.保存规格参数的正常信息
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(vo , attrEntity);
        this.save( attrEntity );

        //2.保存规格参数和属性组的对应信息
        if ( vo.getAttrGroupId() != null ){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            //设置相关的属性
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(vo.getAttrGroupId());

            //将关联的数据存储到对应的表结构中
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }
    }

【 规格参数和属性组 - - - 后台代码 】

    @Override
    public PageUtils queryBasePage(Map<String, Object> params, Long catelogId, String attrType) {
        //(1):根据类别编号查询
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if ( catelogId != 0 ){
            wrapper.eq( "catelog_id" , catelogId );
        }

        //(2):根据key 模糊查询
        String key = (String) params.get("key");
        if ( !StringUtils.isEmpty(key) ){
            wrapper.and( (w)->{
                w.eq("attr_id",key).or().like("attr_name",key);
            });
        }

        //(3):分页查询
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params) ,
                wrapper
        );

        PageUtils pageUtils = new PageUtils(page);

        // 4. 关联的我们需要查询出类别名称和属性组的名称
        List<AttrEntity> records = page.getRecords();
        List<AttrResponseVo> list = records.stream().map((attrEntity) -> {
            AttrResponseVo responseVo = new AttrResponseVo();
            BeanUtils.copyProperties(attrEntity, responseVo);
            // 查询每一条结果对应的 类别名称和属性组的名称
            CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                responseVo.setCatelogName(categoryEntity.getName());
            }
            if("base".equalsIgnoreCase(attrType)){
                // 设置属性组的名称
                AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
                entity.setAttrId(attrEntity.getAttrId());
                // 去关联表中找到对应的属性组ID
                //attrAttrgroupRelationService.query(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrEntity.getAttrId()));
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao
                        .selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().           //查询单条信息用selectOne
                                eq("attr_id", attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null && attrAttrgroupRelationEntity.getAttrGroupId() != null) {
                    // 获取到属性组的ID,然后根据属性组的ID我们来查询属性组的名称
                    AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                    responseVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }

            return responseVo;
        }).collect(Collectors.toList());
        pageUtils.setList(list);
        return pageUtils;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值