1.两表通过关联表进行相关查询时候,应该尽量避免关联查询,根据情况使用多个单表查询和设置一个Vo对象属性来设置完整的返回对象,通过单表查询出来的对象在后边使用对应属性的时候必须做非空判断。
****************
当使用查询出来的对象进行赋值的时候应该是selectone 或者是stream流的map形式 只能是对一个对象连续赋值
下边代码的需求:根据中间表 查找对应组的id 和name
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo respVo = new AttrRespVo();
//获得当前属性对象
//根据属性id 查询属性实体 然后去关联表查询分组
当使用查询出来的对象进行赋值的时候应该是selectone 或者是stream流的map形式 只能是对一个对象连续赋值
//根据中间表 查找对应组的id 和name
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity,respVo);
AttrAttrgroupRelationEntity attr = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if (attr!=null){
respVo.setAttrGroupId(attr.getAttrGroupId());
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr.getAttrGroupId());
if (attrGroupEntity!=null){
respVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
//获得分类id
Long catelogId = attrEntity.getCatelogId();
Long[] catelogPath = categoryService.findCatelogPath(catelogId);
respVo.setCatelogPath(catelogPath);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
if (categoryEntity!=null)
{
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}
stream流的事例
List<AttrRespVo> respVos = records.stream().map((attrEntity -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity, attrRespVo);
AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (attrId != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
})).collect(Collectors.toList());
pageUtils.setList(respVos);
return pageUtils;
}