V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nofaith
V2EX  ›  问与答

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

  •  1
     
  •   nofaith · 2020-09-08 10:34:56 +08:00 · 878 次点击
    这是一个创建于 1349 天前的主题,其中的信息可能已经有所发展或是发生改变。
    @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)
    4 条回复    2020-09-08 11:31:51 +08:00
    nofaith
        1
    nofaith  
    OP
       2020-09-08 10:38:12 +08:00
    麻烦熟悉 Mybatis 的人不吝赐教下,谢谢
    wysnylc
        2
    wysnylc  
       2020-09-08 10:48:21 +08:00   ❤️ 2
    最终的答案就是不在 ORM 层或者说数据库做操作而是查询出来后使用代码拼接,其他的数据库或者 ORM 骚操作到了要扩展更新的时候你就会狠狠地给自己一巴掌
    NPC666
        3
    NPC666  
       2020-09-08 11:27:54 +08:00   ❤️ 1
    <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
        4
    0xC000009F  
       2020-09-08 11:31:51 +08:00   ❤️ 1
    查询结果实体类中嵌套 List 需要在 xml 中定义 collection 。
    https://www.cnblogs.com/iwenwen/p/11082972.html

    但你这种又有些不太一样,因为层级不确定。应该先查出所有一级,然后通过递归查找所有的子节点。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1110 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:04 · PVG 02:04 · LAX 11:04 · JFK 14:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.