目录
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>