java实现商品中的三级分类讲解更新中

 本文详细概述了运行的整体过程以及实现的代码,文章内容持续更新中,如果中文描述过程有错误的请各位指出来,让大家一起学习。

 @Override
    public List<CategoryEntity> listWithTree() {
        //使用categoryDao.selectList();也是可以的  他得CategoryDao中也继承了baseMapper

        //因为本类继承了serviceImpl 。他的泛型中有baseMapper也可以直接使用
        //1.此出功能查出所有分类,selectList查询所有,括号中表示不需要任何条件查询所有
          List<CategoryEntity> entities = baseMapper.selectList(null);
        //2.组装成父子的树形结构
        //2.1找到所有一级分类
        List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity) -> {    //entities获取数据,categoryEntity是每一个数据需要过滤得元素。filter表示过滤器
            return categoryEntity.getParentCid() == 0;  //获取categoryEntity中得第一个数据父id是否等于0,等于0得会全部返回存在这里level1Menus中。此时第一个数据(parentCid是0 {图书})往下走
        }).map((menu)->{ //menu表示菜单 level1Menus值会传到menu中,此时有了图书第一个菜单,这是一个父分类。 Tip:map可以将每一个菜单改变
            menu.setChildren(getChildrens(menu,entities));//然后再把当前(menu第一个菜单 图书)父类的子分类保存进去。 Tip: setChildren是自己写的一个实现具体功能的方法
            return menu;//保存结束之后将当前菜单返回到level1Menus中
        }).sorted((menu1,menu2)->{
           return (menu1.getSort()==null?0:menu1.getSort())- (menu2.getSort()==null?0: menu2.getSort());
        }).collect(Collectors.toList());//最终(collect)收集成一个集合

        return level1Menus;
    }
        //递归查找所有菜单得子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1找到子菜单
            categoryEntity.setChildren(getChildrens(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;
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

originder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值