JAVA后端递归构建树结构及使用mybatis构建树结构

目录

1.说明

2.java示例

3.mybatis示例


1.说明

①获取所有的顶级节点信息

②循环顶级节点信息,寻找每个顶级节点下载子节点,找到子节点后,递归调用寻找子节点的方法

2.java示例

package com.ruoyi.web.controller.tool;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author lin
 * @Date 2024/4/7 19:02
 * @Version 1.0
 */
public class TreeUtils {
    public static void main(String[] args) {
        List<StudentTree> list = new ArrayList<>();
        StudentTree stu1 = new StudentTree();
        stu1.setId("1");
        stu1.setPid("0");
        stu1.setName("名1");
        StudentTree stu2 = new StudentTree();
        stu2.setId("2");
        stu2.setPid("0");
        stu2.setName("名2");
        StudentTree stu3 = new StudentTree();
        stu3.setId("11");
        stu3.setPid("1");
        stu3.setName("名11");
        StudentTree stu4 = new StudentTree();
        stu4.setId("12");
        stu4.setPid("1");
        stu4.setName("名12");
        StudentTree stu5 = new StudentTree();
        stu5.setId("111");
        stu5.setPid("11");
        stu5.setName("名111");
        StudentTree stu6 = new StudentTree();
        stu6.setId("112");
        stu6.setPid("11");
        stu6.setName("名112");
        StudentTree stu7 = new StudentTree();
        stu7.setId("21");
        stu7.setPid("2");
        stu7.setName("名21");
        StudentTree stu8 = new StudentTree();
        stu8.setId("22");
        stu8.setPid("2");
        stu8.setName("名22");
        StudentTree stu9 = new StudentTree();
        stu9.setId("212");
        stu9.setPid("21");
        stu9.setName("名212");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        list.add(stu5);
        list.add(stu6);
        list.add(stu7);
        list.add(stu8);
        list.add(stu9);
        List<StudentTree> studentTrees = buildTree(list);
        System.out.println(studentTrees);
    }

    public static List<StudentTree>  buildTree(List<StudentTree> list){
        // 获取最顶级的树节点
        List<StudentTree> treeList = new ArrayList<>();
        for (StudentTree studentTree : list) {
            if("0".equals(studentTree.getPid())){
                treeList.add(studentTree);
            }
        }
        // 循环每个顶级节点,查找顶级节点的子节点
        for (StudentTree studentTree : treeList) {
            buildChild(studentTree, list);
        }
        return treeList;
    }

    public static void buildChild(StudentTree student, List<StudentTree> list){
        List<StudentTree> treeList = new ArrayList<>();
        for (StudentTree studentTree : list) {
            if(student.getId().equals(studentTree.getPid())){
                // 查找到子节点后,递归调用此方法,查找子节点的子节点
                buildChild(studentTree,list);
                // 将子节点存储到集合中
                treeList.add(studentTree);
            }
        }
        // 将查询到的子节点集合设置到child中
        student.setChild(treeList);
    }
    
    // 树结构实体类
    public static class StudentTree{
        private String id;
        private String pid;
        private String name;
        private List<StudentTree> child;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPid() {
            return pid;
        }

        public void setPid(String pid) {
            this.pid = pid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<StudentTree> getChild() {
            return child;
        }

        public void setChild(List<StudentTree> child) {
            this.child = child;
        }
    }

}

3.mybatis示例

    <resultMap id="menuTree" type="com.kingagroot.info.common.auth.domains.beans.TreeItem">
        <id column="MENU_ID" property="id" jdbcType="INTEGER" />
        <result column="PARENT_ID" property="parentId" jdbcType="INTEGER" />
        <collection column="MENU_ID" property="children" select="getMenuByPid"/>
    </resultMap>

    <select id="getMenuTree" resultMap="menuTree">
        select MENU_ID,PARENT_ID from pf_menu where MENU_ID = #{menuId,jdbcType=INTEGER}
    </select>

    <select id="getMenuByPid" resultMap="menuTree">
        select MENU_ID,PARENT_ID from pf_menu where PARENT_ID = #{parentId,jdbcType=INTEGER}
    </select>
相关推荐
喵叔哟5 分钟前
重构代码之移动字段
java·数据库·重构
喵叔哟5 分钟前
重构代码之取消临时字段
java·前端·重构
fa_lsyk7 分钟前
maven环境搭建
java·maven
Daniel 大东26 分钟前
idea 解决缓存损坏问题
java·缓存·intellij-idea
wind瑞32 分钟前
IntelliJ IDEA插件开发-代码补全插件入门开发
java·ide·intellij-idea
HappyAcmen33 分钟前
IDEA部署AI代写插件
java·人工智能·intellij-idea
马剑威(威哥爱编程)38 分钟前
读写锁分离设计模式详解
java·设计模式·java-ee
鸽鸽程序猿39 分钟前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
修道-032340 分钟前
【JAVA】二、设计模式之策略模式
java·设计模式·策略模式
九圣残炎1 小时前
【从零开始的LeetCode-算法】2559. 统计范围内的元音字符串数
java·算法·leetcode