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>
相关推荐
愤怒的代码9 分钟前
Spring Boot对访问密钥加解密——HMAC-SHA256
java·spring boot·后端
带多刺的玫瑰10 分钟前
Leecode刷题C语言之切蛋糕的最小总开销①
java·数据结构·算法
栗豆包25 分钟前
w118共享汽车管理系统
java·spring boot·后端·spring·tomcat·maven
夜半被帅醒31 分钟前
MySQL 数据库优化详解【Java数据库调优】
java·数据库·mysql
万亿少女的梦16837 分钟前
基于Spring Boot的网络购物商城的设计与实现
java·spring boot·后端
醒了就刷牙1 小时前
黑马Java面试教程_P9_MySQL
java·mysql·面试
m0_748233641 小时前
SQL数组常用函数记录(Map篇)
java·数据库·sql
编程爱好者熊浪2 小时前
JAVA HTTP压缩数据
java
吴冰_hogan2 小时前
JVM(Java虚拟机)的组成部分详解
java·开发语言·jvm
白宇横流学长3 小时前
基于java出租车计价器设计与实现【源码+文档+部署讲解】
java·开发语言