需求
- 查询: 根据catId 查询数据,支持根据key进行模糊搜索
- 保存:对attr 和 attrAtrrGroup 表进行修改
- 修改:首先是数据的回显,回显数据增加
vo
引入vo类,做前端返回数据
和实体类
的中间层。在这里插入代码片
查询和修改查询
vo 继承实体类,并添加前端所需的额外属性
@Data
public class AttrRespVo extends AttrEntity {
private Long attrGroupId;
private Long catelogId;
private Long[] catelogPath;
private String groupName;
private String catelogName;
}
- 查询避免直接联表查询,而是一级一级查询
- gid -> cid -> cname,path 则单独调用service封装的findpath
- 每次查到实体类时,应进行非空判断
查询
页面表查询
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
if (catelogId != 0) {
wrapper.and(i -> i.eq("catelog_id", catelogId));
}
if (params.containsKey("key")) { // 除此之外还有cid cname
String key = (String) params.get("key");
wrapper.and(i -> i.eq("attr_id", key).or().like("attr_id", key));
}
IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> vos = records.stream().map(entity -> {
AttrRespVo vo = new AttrRespVo();
BeanUtils.copyProperties(entity, vo);//
AttrAttrgroupRelationEntity relationEntity = relationService.getById(entity.getAttrId());
if (relationEntity != null) { // 每次查找新表,都要先判空
AttrGroupEntity groupEntity = groupService.getById(relationEntity.getAttrGroupId());
if (groupEntity != null) {
vo.setGroupName(groupEntity.getAttrGroupName());
}
}
CategoryEntity category = categoryService.getById(entity.getCatelogId());
if (category != null) {
vo.setCatelogName(category.getName());
}
return vo;
}).collect(Collectors.toList());
PageUtils pageUtils = new PageUtils();
pageUtils.setList(vos);
return pageUtils;
}
修改查询
@Override // vo : Long attrGroupId; Long catelogId;Long[] catelogPath;
public AttrRespVo queryByIdWithPath(Long attrId) {
AttrRespVo vo = new AttrRespVo();
AttrEntity entity = this.getById(attrId);
BeanUtils.copyProperties(entity,vo);
AttrAttrgroupRelationEntity relationEntity = relationService.getById(attrId);
if(relationEntity!=null){
Long attrGroupId = relationEntity.getAttrGroupId();
vo.setAttrGroupId(attrGroupId);
}
CategoryEntity categoryEntity = categoryService.getById(entity.getCatelogId());
if(categoryEntity!=null){
Long catelogId = categoryEntity.getCatId();
Long[] catelogPath = categoryService.findCategoryPath(catelogId);
vo.setCatelogId(catelogId);
vo.setCatelogPath(catelogPath);
}
return vo;
}
保存和修改保存
保存
@Transactional
@Override
public void saveAttr(AttrVo attrVo) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attrVo, attrEntity);
this.save(attrEntity);// 基本数据
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationService.save(relationEntity);// 关系表数据
}
修改保存
@Override
@Transactional
public void updateAttr(AttrRespVo vo) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(vo, attrEntity);
this.updateById(attrEntity); // 修改基础表
Long attrId = attrEntity.getAttrId();
Long attrGroupId = vo.getAttrGroupId();
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrId(attrId);
relationEntity.setAttrGroupId(attrGroupId);
UpdateWrapper<AttrAttrgroupRelationEntity> wrapper = // 修改关系表
new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId);
relationService.update(relationEntity, wrapper);
}
总结
- 对表结构不熟悉
- 对需求不熟悉
- 对方法应该加在哪一层不熟悉