将递归连接转换为 java 对象

Transform recursive join into java object

假设我有以下模型

public class ExampleComment {
    private int id;
    private Integer commentOf;
    private List<ExampleComment> comments;
    ...
    getters, setters
    ...
}

和数据库:

id | commentOf 
1  | null 
2  | 1  
3  | 1  
4  | 2  

如果我使用一些讨厌的 sql 程序(这可能不是正确的解决方案?),我仍然不知道如何将它传输到 mybatis 中的 java 对象。

我知道当我在.net 中做同样的事情时,它是由 entity framework 制作的,所以可能应该可以在 mybatis 中制作,但我在 [=23= 上找不到任何解决方案].

假设评论 属性 将包含当前评论在 COMMENTOF 列中的所有评论,以下应该有效。

主要评论结果图:

<resultMap id="commentMap" type="ExampleComment">
    <id property="id" column="id" />
    <result property="commentOf" column="commentOf" />
    <collection property="comments" javaType="ArrayList" column="COMMENTOF" ofType="ExampleComment" select="selectChildComments"
</resultMap>

Select 选择儿童评论:

<select id="selectChildComments" resultMap="commentMap">
  SELECT ID, COMMENTOF FROM COMMENT_TABLE WHERE COMMENTOF=#{id}
</select>