栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java递归查询子节点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java递归查询子节点

//module为根节点id
public List getModuleByProjectNo(String moduleId) {
        //查询该根模块id下所有模块
        LambdaQueryWrapper moduleLbq = new LambdaQueryWrapper<>();
        //设置查询条件
        moduleLbq.eq(Module::getId,moduleId);
        //查询
        List moduleList = this.list(moduleLbq);
        //存放根节点
        List rootModule = new ArrayList<>();
        //循环模块集合把第一级过滤出来
        moduleList.stream().forEach(module->{
            if(module.getLevel().equals("0")){
                ModuleChildOutVo moduleChildOutVo = new ModuleChildOutVo();
                BeanUtils.copyProperties(module,moduleChildOutVo);
                rootModule.add(moduleChildOutVo);
            }
        });
        //为根模块设置子模块,getClild是递归调用的
        for (ModuleChildOutVo module : rootModule) {
            
            List childList = getChild(module.getId(), moduleList);
            module.setModuleChilds(childList);//给根节点设置子节点
        }
        return rootModule;
    }


    
    public List getChild(Integer id, List allModule){
        //子模块
        List childList = new ArrayList();
        for (Module module : allModule) {
            // 遍历所有节点,将所有模块的父id与传过来的根节点的id比较
            //相等说明:为该根节点的子节点。
            if(module.getParentId().equals(id+"")){
                ModuleChildOutVo moduleChildOutVo = new ModuleChildOutVo();
                BeanUtils.copyProperties(module,moduleChildOutVo);
                childList.add(moduleChildOutVo);
            }
        }
        //递归
        for (ModuleChildOutVo nav : childList) {
            nav.setModuleChilds(getChild(nav.getId(), allModule));
        }
        //Collections.sort(childList,order());//排序
        //如果节点下没有子节点,返回一个空List(递归退出)
        if(childList.size() == 0){
            return new ArrayList();
        }
        return childList;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1021234.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号