java 递归查询多级菜单

类目表是多级目录表,数据如下:

java 递归查询多级菜单_第1张图片

想获取所有数据的多级目录,代码如下:

  /**
     * 获取树形接口的 类目
     *
     * @return
     */
    @Override
    public List selectExamCategoryTreeList() {
        //1、 获取所有的一级目录
        ExamCategory examCategory = new ExamCategory();
        examCategory.setParentId( 0 );
        List examCategoryList = examCategoryDao.selectExamCategoryList( examCategory );
        
        List parentList = new ArrayList<>();
        for (ExamCategory category : examCategoryList) {
            if (category.getParentId().intValue() == 0) {
                parentList.add( category );
            }
        }

        //获取一级目录下的多级目录
        if (parentList != null) {
            for (ExamCategory category : parentList) {
                fillMenuNode( category );
            }
        }

        return parentList;
    }


    //递归实现查询多级类目
    public ExamCategory fillMenuNode(ExamCategory examCategory) {
        ExamCategory examCategorySearch = new ExamCategory();
        examCategorySearch.setParentId( examCategory.getCategoryId() );
        List examCategoryList = examCategoryDao.selectExamCategoryList( examCategorySearch );
        if (CollectionUtils.isEmpty( examCategoryList )) {
            return examCategory;
        }
        List subMenuList = examCategoryList.stream().map( subMenu -> fillMenuNode( subMenu ) ).collect( Collectors.toList() );//递归实现查询多级菜单
        examCategory.setChildrenList( subMenuList );
        return examCategory;
    }

你可能感兴趣的:(java,java)