【今日成果】:
//允许多组的条件查询;
【快速回顾】:
(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;
}