有熟悉 MyBatisPlus 的吗?我写一个文件树状结构的类,其中有一个类是 TreeNode 类,里面有一个 Children 子类,但是没有办法赋值,需要对数据库或者其他地方做什么改动吗

2020-09-08 10:34:56 +08:00
 nofaith
@Data
@TableName("s_tree")
public class TreeNode {
// 树节点 ID
@TableId("id")
private Long id;
// 树节点名称
@TableField("label")
private String label;
// 父节点 ID
@TableField("parentId")
private Long parentId;
private List<TreeNode> children;
}
数据库 TreeNode 就是 Id Label parentId 三列
遍历文件转换成 TreeNode
TreeNode(id=28, label=新建文件夹, parentId=2, children=null)
TreeNode(id=32, label=新建文件夹, parentId=0, children=null)
TreeNode(id=34, label=新建文件夹, parentId=32, children=null)
889 次点击
所在节点    问与答
4 条回复
nofaith
2020-09-08 10:38:12 +08:00
麻烦熟悉 Mybatis 的人不吝赐教下,谢谢
wysnylc
2020-09-08 10:48:21 +08:00
最终的答案就是不在 ORM 层或者说数据库做操作而是查询出来后使用代码拼接,其他的数据库或者 ORM 骚操作到了要扩展更新的时候你就会狠狠地给自己一巴掌
NPC666
2020-09-08 11:27:54 +08:00
<resultMap id="TreeNodeResultMap" type="xxx.xxx.TreeNode" >
<id column="id" property="id"/>
<result column="label" property="label"/>
<result column="parent_id" property="parentId"/>
<association property="children"
select="getTreeNodeByParentId"
column="id"
fetchType="eager">
</association>
</resultMap>

<select id="getTreeNode" resultMap="TreeNodeResultMap">
SELECT * FROM `s_tree` WHERE id = #{id}
</select>

<select id="getTreeNodeByParentId" parameterType="long" resultType="java.util.List">
SELECT * FROM `s_tree` WHERE parent_id = #{parentId}
</select>

大概是这样?不过没办法让 children 里面的 TreeNode.children 进行查询,那种复杂操作不应当在 orm 层进行。
0xC000009F
2020-09-08 11:31:51 +08:00
查询结果实体类中嵌套 List 需要在 xml 中定义 collection 。
https://www.cnblogs.com/iwenwen/p/11082972.html

但你这种又有些不太一样,因为层级不确定。应该先查出所有一级,然后通过递归查找所有的子节点。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/705095

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX