@Override
public List<CategoryEntity> listWithTree() {
//1、查询出所有分类
List<CategoryEntity> entities = super.baseMapper.selectList(null);
//2、组装成父子的树形结构
//2.1)、找到所有一级分类
List<CategoryEntity> levelMenus = entities.stream()
.filter(e -> e.getParentCid() == 0)
.map((menu) -> {
menu.setChildren(getChildrens(menu, entities));
return menu;
})
.sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
.collect(Collectors.toList());
return levelMenus;
}
//递归查找所有菜单的子菜单
private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid().equals(root.getCatId());
}).map(categoryEntity -> {
//1、找到子菜单(递归)
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
return children;
}
标签:return,递归,menu,List,categoryEntity,查询,getSort From: https://www.cnblogs.com/JavaYuYin/p/18016555