假设我有以下模型
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中制作,但我在google上找不到任何解决方案。
最佳答案
假设 comments 属性将包含当前评论位于 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>
选择selectChildComments:
<select id="selectChildComments" resultMap="commentMap">
SELECT ID, COMMENTOF FROM COMMENT_TABLE WHERE COMMENTOF=#{id}
</select>
关于java - 将递归连接转换为java对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43763472/