• Java递归查询树形结构(详解)


    一.数据准备

    数据库表结构如下所示,

    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (1, 0, '合伙企业');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (2, 0, '有限公司');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (3, 0, '股份有限公司');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (4, 0, '政府');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (5, 0, '行政与事业单位');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (6, 0, '协会');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (7, 0, '公益机构');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (101, 1, '有限合伙');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (102, 1, '普通合伙');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (201, 2, '一人有限公司');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (202, 2, '合资公司');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (301, 3, '上市股份公司');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (302, 3, '非上市股份公司');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (401, 4, '党办机构');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (402, 4, '中央政府');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (403, 4, '省级政府');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (404, 4, '市级政府');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (405, 4, '县区级政府');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (406, 4, '乡镇政府');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (501, 5, '行政机关');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (502, 5, '事业单位');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (601, 6, '行业协会');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (602, 6, '社会团体');
    INSERT INTO `jg_user`.`com_type`(`type_id`, `parent_id`, `type_name`) VALUES (701, 7, '公益机构');
     

    二.控制层代码编写

    1. @Log(title = "查询机构类型树", businessType = BusinessType.OTHER)
    2. @PostMapping("/selectComTypeTrees")
    3. public AjaxResult selectComTypeTrees(@RequestBody ComType comType) {
    4. AjaxResult ajax = AjaxResult.success();
    5. ajax.put("comTypeTrees", comMainService.selectComTypeTrees(comType));
    6. return ajax;
    7. }

    三.业务层代码编写

    1. @Override
    2. public List selectComTypeTrees(ComType comType) {
    3. List comTypeList = comMainMapper.selectComTypeTrees(comType);
    4. List collect = comTypeList.stream()
    5. .filter(item -> item.getParentId() == 0)
    6. .map(item -> {
    7. item.setChildList(getChildren(item, comTypeList));
    8. return item;
    9. })
    10. .collect(Collectors.toList());
    11. return collect.size() == 0 ? comTypeList : collect;
    12. }
    13. public static List getChildren(ComType comType, List comTypeList) {
    14. List collect = comTypeList.stream()
    15. .filter(item -> item.getParentId().equals(comType.getTypeId()))
    16. .map(item -> {
    17. item.setChildList(getChildren(item, comTypeList));
    18. return item;
    19. })
    20. .collect(Collectors.toList());
    21. return collect;
    22. }

    四.数据库层代买编写

    1. <resultMap type="com.ruoyi.user.domain.ComType" id="ComTypeResult">
    2. <result property="typeId" column="type_id" />
    3. <result property="parentId" column="parent_id" />
    4. <result property="typeName" column="type_name" />
    5. resultMap>
    6. <select id="selectComTypeTrees" parameterType="com.ruoyi.user.domain.ComType" resultMap="ComTypeResult">
    7. select type_id,parent_id,type_name from com_type
    8. <where>
    9. <if test="parentId != null and parentId != '' "> and parent_id = #{parentId}if>
    10. where>
    11. select>

    五.测试

    http://localhost:8080/user/com/selectComTypeTrees

    入参:

    1. {
    2. "parentId": 0
    3. }

    出参:

    1. {
    2. "msg": "操作成功",
    3. "code": 200,
    4. "comTypeTrees": [
    5. {
    6. "typeId": 1,
    7. "parentId": 0,
    8. "typeName": "合伙企业",
    9. "childList": [
    10. {
    11. "typeId": 101,
    12. "parentId": 1,
    13. "typeName": "有限合伙",
    14. "childList": [
    15. {
    16. "typeId": 101001,
    17. "parentId": 101,
    18. "typeName": "测试三级菜单",
    19. "childList": []
    20. }
    21. ]
    22. },
    23. {
    24. "typeId": 102,
    25. "parentId": 1,
    26. "typeName": "普通合伙",
    27. "childList": []
    28. }
    29. ]
    30. },
    31. {
    32. "typeId": 2,
    33. "parentId": 0,
    34. "typeName": "有限公司",
    35. "childList": [
    36. {
    37. "typeId": 201,
    38. "parentId": 2,
    39. "typeName": "一人有限公司",
    40. "childList": []
    41. },
    42. {
    43. "typeId": 202,
    44. "parentId": 2,
    45. "typeName": "合资公司",
    46. "childList": []
    47. }
    48. ]
    49. },
    50. {
    51. "typeId": 3,
    52. "parentId": 0,
    53. "typeName": "股份有限公司",
    54. "childList": [
    55. {
    56. "typeId": 301,
    57. "parentId": 3,
    58. "typeName": "上市股份公司",
    59. "childList": []
    60. },
    61. {
    62. "typeId": 302,
    63. "parentId": 3,
    64. "typeName": "非上市股份公司",
    65. "childList": []
    66. }
    67. ]
    68. },
    69. {
    70. "typeId": 4,
    71. "parentId": 0,
    72. "typeName": "政府",
    73. "childList": [
    74. {
    75. "typeId": 401,
    76. "parentId": 4,
    77. "typeName": "党办机构",
    78. "childList": []
    79. },
    80. {
    81. "typeId": 402,
    82. "parentId": 4,
    83. "typeName": "中央政府",
    84. "childList": []
    85. },
    86. {
    87. "typeId": 403,
    88. "parentId": 4,
    89. "typeName": "省级政府",
    90. "childList": []
    91. },
    92. {
    93. "typeId": 404,
    94. "parentId": 4,
    95. "typeName": "市级政府",
    96. "childList": []
    97. },
    98. {
    99. "typeId": 405,
    100. "parentId": 4,
    101. "typeName": "县区级政府",
    102. "childList": []
    103. },
    104. {
    105. "typeId": 406,
    106. "parentId": 4,
    107. "typeName": "乡镇政府",
    108. "childList": []
    109. }
    110. ]
    111. },
    112. {
    113. "typeId": 5,
    114. "parentId": 0,
    115. "typeName": "行政与事业单位",
    116. "childList": [
    117. {
    118. "typeId": 501,
    119. "parentId": 5,
    120. "typeName": "行政机关",
    121. "childList": []
    122. },
    123. {
    124. "typeId": 502,
    125. "parentId": 5,
    126. "typeName": "事业单位",
    127. "childList": []
    128. }
    129. ]
    130. },
    131. {
    132. "typeId": 6,
    133. "parentId": 0,
    134. "typeName": "协会",
    135. "childList": [
    136. {
    137. "typeId": 601,
    138. "parentId": 6,
    139. "typeName": "行业协会",
    140. "childList": []
    141. },
    142. {
    143. "typeId": 602,
    144. "parentId": 6,
    145. "typeName": "社会团体",
    146. "childList": []
    147. }
    148. ]
    149. },
    150. {
    151. "typeId": 7,
    152. "parentId": 0,
    153. "typeName": "公益机构",
    154. "childList": [
    155. {
    156. "typeId": 701,
    157. "parentId": 7,
    158. "typeName": "公益机构",
    159. "childList": []
    160. }
    161. ]
    162. }
    163. ]
    164. }

    尽自己的一些绵薄之力,希望对大家有所帮助,谢谢!^_^

  • 相关阅读:
    qnx 工程目录创建工具 addvariant
    shell_46.Linux使用 getopts 命令
    基本计算器 [括号匹配的变形]
    信号量在线程中的应用
    一.node的http模块;二.同步和异步;三.异步操作的实现:ajax;四.jQuery中对ajax封装;五.Node的Web框架
    9.6 并查集
    【LeetCode高频SQL50题-基础版】打卡第1天:第1~10题
    基于Matlab计算经典CFAR阈值
    在网络隔离下实现文件传输交换,你的方式真的安全吗?
    多标签分类任务-服装分类
  • 原文地址:https://blog.csdn.net/qq_45443475/article/details/127979127