Let's say I have following model
public class ExampleComment {
private int id;
private Integer commentOf;
private List<ExampleComment> comments;
...
getters, setters
...
}
And database:
id | commentOf
1 | null
2 | 1
3 | 1
4 | 2
If I use some nasty sql procedure (which is probably not correct solution?), I still have no clue how to transfer it into java object in mybatis.
I know that when I was doing the same in .net, it was made by entity framework, so probably it should be possible to make in mybatis, but I cant find any solution on google.
Assuming the comments property will have all comments for which current comment is in COMMENTOF column, following should work.
Main Comment ResultMap:
<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 selectChildComments:
<select id="selectChildComments" resultMap="commentMap">
SELECT ID, COMMENTOF FROM COMMENT_TABLE WHERE COMMENTOF=#{id}
</select>