当前位置:   article > 正文

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

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

目录

1.说明

2.java示例

3.mybatis示例


1.说明

获取所有的顶级节点信息

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

2.java示例

  1. package com.ruoyi.web.controller.tool;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * @Author lin
  6. * @Date 2024/4/7 19:02
  7. * @Version 1.0
  8. */
  9. public class TreeUtils {
  10. public static void main(String[] args) {
  11. List<StudentTree> list = new ArrayList<>();
  12. StudentTree stu1 = new StudentTree();
  13. stu1.setId("1");
  14. stu1.setPid("0");
  15. stu1.setName("名1");
  16. StudentTree stu2 = new StudentTree();
  17. stu2.setId("2");
  18. stu2.setPid("0");
  19. stu2.setName("名2");
  20. StudentTree stu3 = new StudentTree();
  21. stu3.setId("11");
  22. stu3.setPid("1");
  23. stu3.setName("名11");
  24. StudentTree stu4 = new StudentTree();
  25. stu4.setId("12");
  26. stu4.setPid("1");
  27. stu4.setName("名12");
  28. StudentTree stu5 = new StudentTree();
  29. stu5.setId("111");
  30. stu5.setPid("11");
  31. stu5.setName("名111");
  32. StudentTree stu6 = new StudentTree();
  33. stu6.setId("112");
  34. stu6.setPid("11");
  35. stu6.setName("名112");
  36. StudentTree stu7 = new StudentTree();
  37. stu7.setId("21");
  38. stu7.setPid("2");
  39. stu7.setName("名21");
  40. StudentTree stu8 = new StudentTree();
  41. stu8.setId("22");
  42. stu8.setPid("2");
  43. stu8.setName("名22");
  44. StudentTree stu9 = new StudentTree();
  45. stu9.setId("212");
  46. stu9.setPid("21");
  47. stu9.setName("名212");
  48. list.add(stu1);
  49. list.add(stu2);
  50. list.add(stu3);
  51. list.add(stu4);
  52. list.add(stu5);
  53. list.add(stu6);
  54. list.add(stu7);
  55. list.add(stu8);
  56. list.add(stu9);
  57. List<StudentTree> studentTrees = buildTree(list);
  58. System.out.println(studentTrees);
  59. }
  60. public static List<StudentTree> buildTree(List<StudentTree> list){
  61. // 获取最顶级的树节点
  62. List<StudentTree> treeList = new ArrayList<>();
  63. for (StudentTree studentTree : list) {
  64. if("0".equals(studentTree.getPid())){
  65. treeList.add(studentTree);
  66. }
  67. }
  68. // 循环每个顶级节点,查找顶级节点的子节点
  69. for (StudentTree studentTree : treeList) {
  70. buildChild(studentTree, list);
  71. }
  72. return treeList;
  73. }
  74. public static void buildChild(StudentTree student, List<StudentTree> list){
  75. List<StudentTree> treeList = new ArrayList<>();
  76. for (StudentTree studentTree : list) {
  77. if(student.getId().equals(studentTree.getPid())){
  78. // 查找到子节点后,递归调用此方法,查找子节点的子节点
  79. buildChild(studentTree,list);
  80. // 将子节点存储到集合中
  81. treeList.add(studentTree);
  82. }
  83. }
  84. // 将查询到的子节点集合设置到child中
  85. student.setChild(treeList);
  86. }
  87. // 树结构实体类
  88. public static class StudentTree{
  89. private String id;
  90. private String pid;
  91. private String name;
  92. private List<StudentTree> child;
  93. public String getId() {
  94. return id;
  95. }
  96. public void setId(String id) {
  97. this.id = id;
  98. }
  99. public String getPid() {
  100. return pid;
  101. }
  102. public void setPid(String pid) {
  103. this.pid = pid;
  104. }
  105. public String getName() {
  106. return name;
  107. }
  108. public void setName(String name) {
  109. this.name = name;
  110. }
  111. public List<StudentTree> getChild() {
  112. return child;
  113. }
  114. public void setChild(List<StudentTree> child) {
  115. this.child = child;
  116. }
  117. }
  118. }

3.mybatis示例

  1. <resultMap id="menuTree" type="com.kingagroot.info.common.auth.domains.beans.TreeItem">
  2. <id column="MENU_ID" property="id" jdbcType="INTEGER" />
  3. <result column="PARENT_ID" property="parentId" jdbcType="INTEGER" />
  4. <collection column="MENU_ID" property="children" select="getMenuByPid"/>
  5. </resultMap>
  6. <select id="getMenuTree" resultMap="menuTree">
  7. select MENU_ID,PARENT_ID from pf_menu where MENU_ID = #{menuId,jdbcType=INTEGER}
  8. </select>
  9. <select id="getMenuByPid" resultMap="menuTree">
  10. select MENU_ID,PARENT_ID from pf_menu where PARENT_ID = #{parentId,jdbcType=INTEGER}
  11. </select>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/414478
推荐阅读
相关标签
  

闽ICP备14008679号