java代码
public List<CategoryEntity> listWithTree() {
//查出所有
List<CategoryEntity> categoryEntities = this.list();
List<CategoryEntity> lever1Menus = categoryEntities.stream()
.filter(categoryEntity -> categoryEntity.getParentCid() == 0)
.map((categoryEntity)->{
categoryEntity.setChildren(getChildren(categoryEntity,categoryEntities));
return categoryEntity;
})
.sorted((m,n)->{
return m.getSort() - n.getSort();
}
)
.collect(Collectors.toList());
return lever1Menus;
}
public List<CategoryEntity> getChildren(CategoryEntity menu,List<CategoryEntity> categoryEntities){
List<CategoryEntity> lowerMenus = categoryEntities
.stream()
.filter(categoryEntity -> categoryEntity.getParentCid() == menu.getCatId())
.map((categoryEntity)->{ //如果没有子菜单就不会调用map了,不要非空判断了
categoryEntity.setChildren(
getChildren(categoryEntity,categoryEntities));
return categoryEntity;
})
.sorted((m,n)->{
return m.getSort()==null||n.getSort()==null ? 0-0 : m.getSort() - n.getSort();
}
)
.collect(Collectors.toList());
return lowerMenus;
}
数据库里有数据
在控制台中查看数据发现并没有查出来
看debug看到java后台,发现下面行查询出来的结果还是没有丢失的.
//查出所有
List<CategoryEntity> categoryEntities = this.list();
所以应该就是stream处理的时候因为某些原因没有把数据封装进去,导致数据丢失.
有没有哪位大佬知道是什么原因