mybatis 父子级树形结构查询

柔情只为你懂 2024-04-07 15:32 201阅读 0赞

针对父子级数据目录查询, 以前都是逐级的去根据父级id查询子集目录, 查出后最后再在代码中拼成树形结构, 相当复杂,我们可以利用 mybatis 提供的 collection 标签自动组织树形结构, 接下来我们一步一步看该如何建立

1.建表语句

  1. CREATE TABLE `parent_child` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) NOT NULL,
  4. `parent_id` int(11) DEFAULT NULL COMMENT '父级id',
  5. `create_time` varchar(32) NOT NULL,
  6. PRIMARY KEY (`id`) USING BTREE,
  7. KEY `index_parent_child_id` (`id`) USING BTREE,
  8. KEY `index_type_name` (`name`) USING BTREE,
  9. KEY `index_parent_id` (`parent_id`) USING BTREE
  10. ) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
  11. INSERT INTO `parent_child` VALUES ('1', '0-1', '0', '2022-09-13 11:59:13');
  12. INSERT INTO `parent_child` VALUES ('2', '0-2', '0', '2022-09-13 11:59:28');
  13. INSERT INTO `parent_child` VALUES ('3', '0-3', '0', '2022-09-13 14:01:20');
  14. INSERT INTO `parent_child` VALUES ('4', '1-1', '1', '2022-09-13 14:01:41');
  15. INSERT INTO `parent_child` VALUES ('5', '1-2', '1', '2022-09-13 14:01:58');
  16. INSERT INTO `parent_child` VALUES ('6', '1-3', '1', '2022-09-13 14:02:11');
  17. INSERT INTO `parent_child` VALUES ('7', '2-1', '2', '2022-09-13 14:02:24');
  18. INSERT INTO `parent_child` VALUES ('8', '2-2', '2', '2022-09-13 14:02:49');
  19. INSERT INTO `parent_child` VALUES ('9', '2-3', '2', '2022-09-13 14:03:03');
  20. INSERT INTO `parent_child` VALUES ('10', '3-1', '3', '2022-09-13 14:03:16');
  21. INSERT INTO `parent_child` VALUES ('11', '3-2', '3', '2022-09-13 14:03:28');
  22. INSERT INTO `parent_child` VALUES ('12', '3-3', '3', '2022-09-13 14:03:39');
  23. INSERT INTO `parent_child` VALUES ('13', '1-1-1', '4', '2022-09-13 14:04:05');
  24. INSERT INTO `parent_child` VALUES ('14', '1-1-2', '4', '2022-09-13 14:04:19');
  25. INSERT INTO `parent_child` VALUES ('15', '1-3-1', '6', '2022-09-29 14:04:34');
  26. INSERT INTO `parent_child` VALUES ('16', '1-3-2', '6', '2022-09-13 14:04:45');
  27. INSERT INTO `parent_child` VALUES ('17', '3-2-1', '11', '2022-09-13 14:05:07');
  28. INSERT INTO `parent_child` VALUES ('18', '3-2-2', '11', '2022-09-13 14:05:22');
  29. INSERT INTO `parent_child` VALUES ('19', '3-3-1', '12', '2022-09-13 14:05:37');
  30. INSERT INTO `parent_child` VALUES ('20', '3-2-1-1', '17', '2022-09-30 14:06:02');
  31. INSERT INTO `parent_child` VALUES ('21', '3-2-1-1-1', '20', '2022-09-13 14:06:19');
  32. INSERT INTO `parent_child` VALUES ('22', '3-2-1-1-2', '20', '2022-09-09 14:06:36');

2.建立返回数据结构 VO

get set方法这里省略自行补上就行

  1. public class ParentChildVo {
  2. private Integer id;
  3. private String name;
  4. private Integer parentId;
  5. private Date createTime;
  6. private List<ParentChildVo> children;
  7. }

3.mybatis 编写查询语句

3.1 mapper文件:
  1. /**
  2. * 查询树形目录
  3. * @param parentId
  4. * @return
  5. */
  6. List<ParentChildVo> getParentChildTree(int parentId);
3.2 mybatis文件
  1. <!-- 树形结构 -->
  2. <resultMap id="ParentChildTreeMap" type="com.ashen.game.common.vo.ParentChildVo">
  3. <id column="id" jdbcType="VARCHAR" property="id" />
  4. <id column="name" jdbcType="VARCHAR" property="name" />
  5. <id column="parentId" jdbcType="INTEGER" property="parentId" />
  6. <id column="createTime" jdbcType="VARCHAR" property="createTime" />
  7. <collection property="children" ofType="com.ashen.game.common.vo.ParentChildVo" column="id" select="getParentChildTree" fetchType="eager"/>
  8. </resultMap>
  9. <!--查询树形分组-->
  10. <select id="getParentChildTree" resultMap="ParentChildTreeMap">
  11. SELECT
  12. a.id,
  13. a.name,
  14. a.parent_id as parentId,
  15. a.create_time as createTime
  16. FROM parent_child AS a
  17. WHERE
  18. a.parent_id = #{parentId}
  19. </select>
3.3 查询结果
  1. {
  2. "data": [
  3. {
  4. "id": 1,
  5. "name": "0-1",
  6. "parentId": 0,
  7. "createTime": "2022-09-13 11:59:13",
  8. "children": [
  9. {
  10. "id": 4,
  11. "name": "1-1",
  12. "parentId": 1,
  13. "createTime": "2022-09-13 14:01:41",
  14. "children": [
  15. {
  16. "id": 13,
  17. "name": "1-1-1",
  18. "parentId": 4,
  19. "createTime": "2022-09-13 14:04:05",
  20. "children": []
  21. },
  22. {
  23. "id": 14,
  24. "name": "1-1-2",
  25. "parentId": 4,
  26. "createTime": "2022-09-13 14:04:19",
  27. "children": []
  28. }
  29. ]
  30. },
  31. {
  32. "id": 5,
  33. "name": "1-2",
  34. "parentId": 1,
  35. "createTime": "2022-09-13 14:01:58",
  36. "children": []
  37. },
  38. {
  39. "id": 6,
  40. "name": "1-3",
  41. "parentId": 1,
  42. "createTime": "2022-09-13 14:02:11",
  43. "children": [
  44. {
  45. "id": 15,
  46. "name": "1-3-1",
  47. "parentId": 6,
  48. "createTime": "2022-09-29 14:04:34",
  49. "children": []
  50. },
  51. {
  52. "id": 16,
  53. "name": "1-3-2",
  54. "parentId": 6,
  55. "createTime": "2022-09-13 14:04:45",
  56. "children": []
  57. }
  58. ]
  59. }
  60. ]
  61. },
  62. {
  63. "id": 2,
  64. "name": "0-2",
  65. "parentId": 0,
  66. "createTime": "2022-09-13 11:59:28",
  67. "children": [
  68. {
  69. "id": 7,
  70. "name": "2-1",
  71. "parentId": 2,
  72. "createTime": "2022-09-13 14:02:24",
  73. "children": []
  74. },
  75. {
  76. "id": 8,
  77. "name": "2-2",
  78. "parentId": 2,
  79. "createTime": "2022-09-13 14:02:49",
  80. "children": []
  81. },
  82. {
  83. "id": 9,
  84. "name": "2-3",
  85. "parentId": 2,
  86. "createTime": "2022-09-13 14:03:03",
  87. "children": []
  88. }
  89. ]
  90. },
  91. {
  92. "id": 3,
  93. "name": "0-3",
  94. "parentId": 0,
  95. "createTime": "2022-09-13 14:01:20",
  96. "children": [
  97. {
  98. "id": 10,
  99. "name": "3-1",
  100. "parentId": 3,
  101. "createTime": "2022-09-13 14:03:16",
  102. "children": []
  103. },
  104. {
  105. "id": 11,
  106. "name": "3-2",
  107. "parentId": 3,
  108. "createTime": "2022-09-13 14:03:28",
  109. "children": [
  110. {
  111. "id": 17,
  112. "name": "3-2-1",
  113. "parentId": 11,
  114. "createTime": "2022-09-13 14:05:07",
  115. "children": [
  116. {
  117. "id": 20,
  118. "name": "3-2-1-1",
  119. "parentId": 17,
  120. "createTime": "2022-09-30 14:06:02",
  121. "children": [
  122. {
  123. "id": 21,
  124. "name": "3-2-1-1-1",
  125. "parentId": 20,
  126. "createTime": "2022-09-13 14:06:19",
  127. "children": []
  128. },
  129. {
  130. "id": 22,
  131. "name": "3-2-1-1-2",
  132. "parentId": 20,
  133. "createTime": "2022-09-09 14:06:36",
  134. "children": []
  135. }
  136. ]
  137. }
  138. ]
  139. },
  140. {
  141. "id": 18,
  142. "name": "3-2-2",
  143. "parentId": 11,
  144. "createTime": "2022-09-13 14:05:22",
  145. "children": []
  146. }
  147. ]
  148. },
  149. {
  150. "id": 12,
  151. "name": "3-3",
  152. "parentId": 3,
  153. "createTime": "2022-09-13 14:03:39",
  154. "children": [
  155. {
  156. "id": 19,
  157. "name": "3-3-1",
  158. "parentId": 12,
  159. "createTime": "2022-09-13 14:05:37",
  160. "children": []
  161. }
  162. ]
  163. }
  164. ]
  165. }
  166. ],
  167. "message": "ok",
  168. "code": 0,
  169. "count": null,
  170. "token": null
  171. }

4.常见报错

服务处理异常:

  1. Type definition error: [simple type, class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl];
  2. nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
  3. (through reference chain: com.dataojo.docloud.model.vo.ResultMap[\"data\"]->java.util.ArrayList[0]->com.dataojo.docloud.model.vo.DirectoryGroupTreeVo_$$_jvstb9_0[\"children\"]->java.util.ArrayList[0]->com.dataojo.docloud.model.vo.DirectoryGroupTreeVo_$$_jvstb9_0[\"children\"]->java.util.ArrayList[0]->com.dataojo.docloud.model.vo.DirectoryGroupTreeVo_$$_jvstb9_0[\"handler\"])
  • 报错原因:在mabatis中sql查询时,使用了进行一对多的查询,而collection默认的为懒加载,所以导致return 给前端时报错, 加上 fetchType=“eager” 即可。

完整示例代码

发表评论

表情:
评论列表 (有 0 条评论,201人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MyBatis树形结构查询

    前言 对于树形结构的数据库设计通常是基于继承关系设计的,也就是通过父ID关联来实现的,还有就是基于左右值编码设计。本文以继承关系设计的树形结构来讨论下MyBatis树形结