1、新建服务接口:在CategoryService中新建接口方法listWithTree
public interface CategoryService extends IService<CategoryEntity> {
PageUtils queryPage(Map<String, Object> params);
List<CategoryEntity> listWithTree();
}
2、 实现服务接口:com/wang/gulimall/product/service/impl/CategoryServiceImpl实现接口:listWithTree()
- 查询出所有数据方法实现
@Override
public List<CategoryEntity> listWithTree() {
//返回树形结构数据需要两个步骤:1、查询出所有数据;2、将所有数据以树形结构封装起来
//baseMapper是包含泛型CategoryEntity,查询出来的结果便是它的集合
//查询出所有的菜单
List<CategoryEntity> entities = baseMapper.selectList(null);
return entities;
}
访问:http://localhost:8400/product/category/list/tree
- 找到菜单一级分类的实现
@Override
public List<CategoryEntity> listWithTree() {
//返回树形结构数据需要两个步骤:1、查询出所有数据;2、将所有数据以树形结构封装起来
//baseMapper是包含泛型CategoryEntity,查询出来的结果便是它的集合
//查询出所有的菜单
List<CategoryEntity> entities = baseMapper.selectList(null);
List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity) -> {
return categoryEntity.getCatId() == 0;
}).collect(Collectors.toList());
return level1Menus;
}
访问:http://localhost:8400/product/category/list/tree
3. 实现树形结构数据组装
- 在com/wang/gulimall/product/entity/CategoryEntity类中添加子菜单属性:children
@TableField(exist = false)//表示这个属性在表中是不存在的 private List<CategoryEntity> children;
- com/wang/gulimall/product/service/impl/CategoryServiceImpl中实现封装
@Override public List<CategoryEntity> listWithTree() { //返回树形结构数据需要两个步骤:1、查询出所有数据;2、将所有数据以树形结构封装起来 //baseMapper是包含泛型CategoryEntity,查询出来的结果便是它的集合 //查询出所有的菜单 List<CategoryEntity> entities = baseMapper.selectList(null); List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity) -> { return categoryEntity.getParentCid() == 0; //找出这个菜单的子菜单 }).map((menu)->{ //找到并设置这个菜单的子菜单。getChildrenMenus(menu,entities)是返回菜单menu的子菜单,然后通过setChildren函数进行设置子菜单集合 menu.setChildren(getChildrenMenus(menu,entities)); return menu; //对菜单进行排序 }).sorted((menu1,menu2)->{ return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort()); //将菜单转换成集合返回 }).collect(Collectors.toList()); return level1Menus; } public List<CategoryEntity> getChildrenMenus(CategoryEntity root,List<CategoryEntity> all) { //过滤找到root的子菜单 List<CategoryEntity> childrenMenus = all.stream().filter(categoryEntity -> { return categoryEntity.getParentCid().equals(root.getCatId()); //同样,子菜单也可能有自己的子菜单,所以需要递归.map的作用便是让元素以另一种形式输出。 }).map(categoryEntity -> { categoryEntity.setChildren(getChildrenMenus(categoryEntity, all)); return categoryEntity; }).sorted((menu1, menu2) -> { return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort()); }).collect(Collectors.toList()); return childrenMenus; }
- 访问:http://localhost:8400/product/category/list/tree
3、测试服务接口:在gulimall-product模块中在com.wang.gulimall.product.controller包下的CategoryController类中添加如下访问方法listWithTree():
/**
* 查询出所有分类以及子分类,以树形结构组装起来
*/
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R list(){
//在categoryService接口中另外添加一个可以得到菜单的属性结构数据方法
List<CategoryEntity> entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}