谷粒商城-商品服务API-查询-递归树形结构数据获取

在这里插入图片描述
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()

  1. 查询出所有数据方法实现
@Override
    public List<CategoryEntity> listWithTree() {
        //返回树形结构数据需要两个步骤:1、查询出所有数据;2、将所有数据以树形结构封装起来
        //baseMapper是包含泛型CategoryEntity,查询出来的结果便是它的集合

        //查询出所有的菜单
        List<CategoryEntity> entities = baseMapper.selectList(null);
        return entities;
    }

访问:http://localhost:8400/product/category/list/tree
在这里插入图片描述

  1. 找到菜单一级分类的实现
@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. 实现树形结构数据组装

  1. 在com/wang/gulimall/product/entity/CategoryEntity类中添加子菜单属性:children
    @TableField(exist = false)//表示这个属性在表中是不存在的
    private List<CategoryEntity> children;
    
  2. 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;
    }
    
  3. 访问: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);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值