官网:分页插件 | MyBatis-Plus
/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:categorybrandrelation:save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok();
}
/**
*
* @param categoryBrandRelation
*/
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
Long brandId = categoryBrandRelation.getBrandId();
Long catelogId = categoryBrandRelation.getCatelogId();
//查询详细名字
BrandEntity brandEntity = brandDao.selectById(brandId);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
categoryBrandRelation.setBrandName(brandEntity.getName());
categoryBrandRelation.setCatelogName(categoryEntity.getName());
this.save(categoryBrandRelation);
}
当我们在品牌或者分类中修改了名字,在品牌分类联合表中也需要修改
同步更新冗余字段
=============BrandServiceImpl.java=============
@Override
public void updateDetail(BrandEntity brand) {
//保证冗余字段的数据一致
this.updateById(brand);
//如果要更改品牌名 其他表也需要修改
if (!StringUtils.isEmpty(brand.getName())){
categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
}
//TODO 更新其他关联
}
=============CategoryServiceImpl.java============
@Override
public void updateDetail(CategoryEntity category) {
this.updateById(category);
if (!StringUtils.isEmpty(category.getName())){
categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
}
//TODO 修改其他关联
}
=============CategoryBrandRelationServiceImpl.java============
@Override
public void updateBrand(Long brandId, String name) {
this.update(
new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId).set("brand_name",name);
);
}
@Override
public void updateCategory(Long catId, String name) {
this.update(
new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId).set("catlog_name",name);
);
}
属性表
属性分组表
中间表
代码实现
================AttrGroupController.java===============
/**
* 列表
*/
@RequestMapping("/list/{catelogId}")
//@RequiresPermissions("product:attrgroup:list")
public R list(@RequestParam Map<String, Object> params,
@PathVariable("catelogId") Long catelogId){
PageUtils page = attrGroupService.queryPage(params,catelogId);
return R.ok().put("page", page);
}
===============AttrGroupServiceImpl.java===================
@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
if (catelogId == 0){
//默认查询条件为空 查所有
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
new QueryWrapper<AttrGroupEntity>());
return new PageUtils(page);
}else {
String key = (String) params.get("key");
//模糊查询多字段
//查询指定id select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like key)
//构造查询条件
//填写要查询的表对应的实体类
QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
if (!StringUtils.isEmpty(key)) {
wrapper.and((obj) -> {
obj.eq("attr_group_id", key).or().like("attr_group_name", key);
});
}
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
wrapper);
return new PageUtils(page);
}
}
用pageUtils封装返回
postman测试返回page封装数据
Object划分:
新建AttrVo.java
package com.henu.soft.merist.gulimall.product.vo;
import lombok.Data;
@Data
public class AttrVo {
/**
* 属性id
*/
private Long attrId;
/**
* 属性名
*/
private String attrName;
/**
* 是否需要检索[0-不需要,1-需要]
*/
private Integer searchType;
/**
* 属性图标
*/
private String icon;
/**
* 可选值列表[用逗号分隔]
*/
private String valueSelect;
/**
* 属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]
*/
private Integer attrType;
/**
* 启用状态[0 - 禁用,1 - 启用]
*/
private Long enable;
/**
* 所属分类
*/
private Long catelogId;
/**
* 快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整
*/
private Integer showDesc;
private Long attrGroupId;
}
AttrServiceImpl.java
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao attrAttrgroupRelationDao;
@Transactional
@Override
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
//保存基础数据
this.save(attrEntity);
//保存关联关系
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
}
}
前端请求参数列表
为了返回所属分类、所属分组 名字 新建一个vo来返回
package com.henu.soft.merist.gulimall.product.vo;
import lombok.Data;
@Data
public class AttrRespVo extends AttrVo{
private String catelogName;
private String groupName;
}
在大数据的情况下,连表查询消耗大,所以我们分开查,先查出 attr 的所有属性,利用BeanuUils的工具方法将 attr 的数据放到 AttrRespVo 对象中,再查询分类名、分组名。
=============================AttrServiceImpl.java=============================
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> queryWrapper= new QueryWrapper<>();
if (catelogId != 0){
queryWrapper.eq("catelog_id",catelogId);
}
//获取检索条件
//attr_id = key or attr_name like key
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
queryWrapper.and((wrapper)->{
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> records = page.getRecords();
List<Object> respVos = records.stream().map((attrEntity) -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity,attrRespVo);
//设置分组、分类的名字
AttrAttrgroupRelationEntity attr_id = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (attr_id != null) {
attrRespVo.setGroupName(attrGroupDao.selectById(attr_id).getAttrGroupName());
}
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList());
pageUtils.setList(respVos);
return pageUtils;
}
修改设置回显分类
=============================AttrServiceImpl.java================================
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo respVo = new AttrRespVo();
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,respVo);
//设置分组信息
AttrAttrgroupRelationEntity attr_id = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (attr_id != null) {
respVo.setAttrGroupId(attr_id.getAttrGroupId());
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr_id);
if (attrGroupEntity != null){
respVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
//设置分类信息
Long catelogId = attrEntity.getCatelogId();
Long[] catelogPath = categoryService.findCatelogPath(catelogId);
respVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryService.getById(catelogId);
if (categoryEntity != null){
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}
规格修改
=============================AttrServiceImpl.java================================
@Transactional
@Override
public void updateAttr(AttrVo attrVo) {
//修改基本数据
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attrVo,attrEntity);
this.updateById(attrEntity);
//修改关联分组
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
relationEntity.setAttrId(attrVo.getAttrId());
Long count = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrVo.getAttrId()));
if (count > 0){
attrAttrgroupRelationDao.update(relationEntity,new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrVo.getAttrId()));
}else {
attrAttrgroupRelationDao.insert(relationEntity);
}
}
参数规格 和 销售属性 都放在属性表中
封装成一个枚举,在增删改查方法上加上相应的判断即可,销售属性 不用加分组关系也就是不用关联表
======================AttrServiceImpl.java===================
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
// relationDao.delete(new QueryWrapper<>().eq("attr_id",1L).eq("attr_group_id",1L));
List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
//批量删除
relationDao.deleteBatchRelations(entities);
}
=====================AttrAttrGroupRelationDao.xml========================
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.henu.soft.merist.gulimall.product.dao.AttrAttrgroupRelationDao">
<resultMap type="com.henu.soft.merist.gulimall.product.entity.AttrAttrgroupRelationEntity" id="attrAttrgroupRelationMap">
<result property="id" column="id"/>
<result property="attrId" column="attr_id"/>
<result property="attrGroupId" column="attr_group_id"/>
<result property="attrSort" column="attr_sort"/>
resultMap>
<delete id="deleteBatchRelations">
DELETE from `pms_attr_attrgroup_relation` where
<foreach collection="entities" item="item" separator=" OR ">
(attr_id=#{item.attrId} AND attr_group_id=#{item.attrGroupId})
foreach>
delete>
mapper>