@Override
public List<CategoryEntity> listWithTree() {
//无条件查出所有分类
List<CategoryEntity> entities= baseMapper.selectList(null);
//使用 stream.filter(箭头函数 (参数)->{}).collect(Collectors.toList()); 过滤分类获取父亲 0 map关系映射
List<CategoryEntity> listWithTree= entities.stream().filter(categoryEntity->
//筛选一级菜单 parentId==0
categoryEntity.getParentId() == 0
).map((menu)->{
//下级菜单子菜单
menu.setChidren(getChidrens(menu,entities));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort() == null ?0:menu1.getSort())-(menu2.getSort() == null?0:menu2.getSort());
}) .collect(Collectors.toList());
return listWithTree;
}
//获取子菜单 当前菜单 从哪里获取
private List<CategoryEntity> getChidrens( CategoryEntity root ,List<CategoryEntity> all) {
List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentId() == root.getId();
}).map(categoryEntity -> {
//映射接着找找子菜单
categoryEntity.setChidren(getChidrens(categoryEntity, all));
return categoryEntity;
}).sorted((menu1, menu2) -> {
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
}).collect(Collectors.toList());
return children;
}
谷粒商城三级菜单
最新推荐文章于 2024-10-16 10:15:47 发布