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

04-08 2007阅读

目录

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 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 studentTrees = buildTree(list);
        System.out.println(studentTrees);
    }
    public static List  buildTree(List list){
        // 获取最顶级的树节点
        List 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 list){
        List 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 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 getChild() {
            return child;
        }
        public void setChild(List child) {
            this.child = child;
        }
    }
}

3.mybatis示例

    
        
        
        
    
    
        select MENU_ID,PARENT_ID from pf_menu where MENU_ID = #{menuId,jdbcType=INTEGER}
    
    
        select MENU_ID,PARENT_ID from pf_menu where PARENT_ID = #{parentId,jdbcType=INTEGER}
    
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]