看视频学习,有个地方需要返回这样的效果,Map类型,key为一级分类的id,value是集合数组:
视频中用的是循环查库,所以想着用别的方法来写
实体类格式
Catelog2Vo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Catelog2Vo {
private String catelog1Id; // 一级分类的catId
private List<Catelog3Vo> catelog3List;
private String id;
private String name;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Catelog3Vo{
private String catelog2Id; // 二级分类的catId
private String id;
private String name;
}
}
CategoryEntity
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
private Long catId;
private String name;
private Long parentCid;
private Integer catLevel;
@TableLogic(value = "1", delval = "0")
private Integer showStatus;
private Integer sort;
private String icon;
private String productUnit;
private Integer productCount;
@TableField(exist = false)
private List<CategoryEntity> children;
}
代码
这是视频中的写法,循环查库,个人认为循环查库最好不要,除非数据量小
public Map<String,List<Catelog2Vo>> getCatalogJson() {
/**************************** 耗时2-300ms ****************************/
// 查出所有一级分类
List<CategoryEntity> category1List = baseMapper.selectList(new QueryWrapper<CategoryEntity>()
.eq("parent_cid",0));
Map<String, List<Catelog2Vo>> resultData = category1List.stream()
.collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
// 根据一级分类的catId查出二级分类
List<CategoryEntity> category2List = baseMapper.selectList(new QueryWrapper<CategoryEntity>()
.eq("parent_cid", v.getCatId()));
// 封装二级分类数据
List<Catelog2Vo> category2Data = category2List.stream().map(l2 -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(l2.getParentCid().toString(),
null,l2.getCatId().toString(),
l2.getName());
// 根据二级分类的catId查出三级分类
List<CategoryEntity> category3List = baseMapper.selectList(new QueryWrapper<CategoryEntity>()
.eq("parent_cid", l2.getCatId()));
if (category3List != null) {
List<Catelog2Vo.Catelog3Vo> category3Data = category3List.stream().map(l3 -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l3.getParentCid().toString(),
l3.getCatId().toString(),
l3.getName());
return catelog3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatelog3List(category3Data);
}
return catelog2Vo;
}).collect(Collectors.toList());
return category2Data;
}));
return resultData;
}
修改过后的写法,一次性查出所有分类,再通过stream流组装数据:
public Map<String,List<Catelog2Vo>> getCatalogJson() {
/**************************** 耗时1-20ms ****************************/
List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
// 所有一级分类
List<CategoryEntity> category1Data = categoryEntityList.stream()
.filter(item -> item.getParentCid() == 0)
.collect(Collectors.toList());
Map<String, List<Catelog2Vo>> data = category1Data.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
List<Catelog2Vo> catelog2Vos = categoryEntityList.stream()
.filter(f2 -> f2.getParentCid() == v.getCatId())
.map(lv2 -> {
List<Catelog2Vo.Catelog3Vo> catelog3Vos = categoryEntityList.stream()
.filter(f3 -> f3.getParentCid() == lv2.getCatId())
.map(lv3 -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo();
catelog3Vo.setCatelog2Id(lv3.getParentCid().toString());
catelog3Vo.setName(lv3.getName());
catelog3Vo.setId(lv3.getCatId().toString());
return catelog3Vo;
}).collect(Collectors.toList());
Catelog2Vo catelog2Vo = new Catelog2Vo();
catelog2Vo.setCatelog1Id(lv2.getParentCid().toString());
catelog2Vo.setName(lv2.getName());
catelog2Vo.setId(lv2.getCatId().toString());
catelog2Vo.setCatelog3List(catelog3Vos);
return catelog2Vo;
}).collect(Collectors.toList());
return catelog2Vos;
}));
return data;
}