Mybatis使用collection标签进行树形结构数据查询时如何携带外部参数查询

1. 背景

最近更新博客的评论功能,想实现这么一个需求:

评论使用树形结构展示,评论提交后,需要后台审核后才展示到前台,但是用户自己可以显示自己提交的未审核的评论

2. 实施

最初的实现方法是想使用collection进行树形结构查询

为了实现树形查询,需要两个resultMap,一个为最外层的查询结果,另一个是集合里的查询结果,也就是对象中的children对应的List,因为是树形结构,所以外层和里层的结构基本一样,下方代码为两个resultMap代码(示例):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<resultMap type="com.stonewu.blog.core.entity.custom.ReplyTree" id="replyTree">
<id column="id" property="id"/>
<result column="content" property="content"/>
<result column="article_id" property="articleId"/>
<result column="author_id" property="authorId"/>
<result column="parent_reply_id" property="parentReplyId"/>
<result column="check_reply" property="checkReply"/>
<collection column="id=id,authorId=author_id" property="children" ofType="com.stonewu.blog.core.entity.custom.ReplyTree" javaType="java.util.ArrayList" select="getReplyChildren">

</collection>
</resultMap>

<resultMap type="com.stonewu.blog.core.entity.custom.ReplyTree" id="replyTreeChild">
<id column="id" property="id"/>
<result column="content" property="content"/>
<result column="article_id" property="articleId"/>
<result column="author_id" property="authorId"/>
<result column="parent_reply_id" property="parentReplyId"/>
<result column="check_reply" property="checkReply"/>
<collection column="id=id,authorId=author_id" property="children" ofType="com.stonewu.blog.core.entity.custom.ReplyTree" javaType="java.util.ArrayList" select="getReplyChildren">

</collection>
</resultMap>

因为父查询中,需要查出顶层的评论,所以parent_reply_id应该为null,同时为了查询出自己评论的,但是未审核的,就需要下方<if test="param.authorId != null">的代码

下方代码为父查询代码(示例):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<select id="findReplyTreeByParam" parameterType="com.stonewu.blog.core.entity.custom.ReplyTree"
resultMap="replyTree">
SELECT
r.*, a.title article_title,
m.nick_name author_name
FROM
reply r
LEFT JOIN article a ON r.article_id = a.id
LEFT JOIN menber m ON m.id = r.author_id
WHERE 1 = 1
<if test="param.id != null">
AND a.id = #{param.id}
</if>
<if test="param.articleId != null">
AND article_id = #{param.articleId}
</if>
<if test="param.topReplyId != null">
AND top_reply_id = #{param.topReplyId}
</if>
AND parent_reply_id is null
<if test="param.checkReply != null">
AND ( check_reply = #{param.checkReply}
<if test="param.authorId != null">
or m.id = #{param.authorId}
</if>
)
</if>
<if test="param.replyType != null">
AND reply_type = #{param.replyType}
</if>

</select>

子查询是通过上面resultMap中的collection标签关联的getReplyChildren查询,查询语句最初写的如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<select id="getReplyChildren" resultMap="replyTreeChild">
SELECT
r.*, a.title article_title,
m.nick_name author_name
FROM
reply r
LEFT JOIN article a ON r.article_id = a.id
LEFT JOIN menber m ON m.id = r.author_id
WHERE 1 = 1
AND parent_reply_id = #{id}
AND (
check_reply = 1
<if test="authorId != null">
or m.id = #{authorId}
</if>
)
</select>

最下方的if,里面的authorId并非是查询的时候传入的param.authorId,而是父查询中的查询结果的authorId,完全无法达到想要的效果

墙内墙外搜了半天,最终发现唯一的解决办法只有这种,在父查询的时候,把当前传的authorId参数作为查询结果,放到select xxx from中,然后在collection标签中用column属性关联,可是这样子resultMap就又要多一个列,实在是影响代码维护,最终选择放弃了这个方式

3、解决办法
把所有的子对象都通过java进行拼装,使用递归,手动执行多次查询,每次查询即可携带自己想要的参数进行查询,代码示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 父查询
* @param page
* @param param
* @return
*/
@Override
@Cacheable(cacheResolver = BlogCacheConfig.CACHE_RESOLVER_NAME, key = "'findReplyTreeByParam_'+#param.articleId+'_'+#param.checkReply+'_'+#param.authorId+'_page_'+#page.current+'_'+#page.size")
public Page<ReplyTree> findReplyTreeByParam(Page<ReplyTree> page, ReplyTree param) {
Page<ReplyTree> result = mapper.findReplyResultByParam(page, param);
getChildReply(new Page<>(1, 9999), param, result.getRecords());
return result;
}

/**
* 子查询
* @param page
* @param param
* @param result
*/
private void getChildReply(Page<ReplyTree> page, ReplyTree param, List<ReplyTree> result) {
result.forEach(replyTree -> {
Integer id = replyTree.getId();
param.setParentReplyId(id);
Page<ReplyTree> childReply = mapper.findReplyResultByParam(page, param);
replyTree.setChildren(childReply.getRecords());
getChildReply(page, param, childReply.getRecords());
});
}

里面的findReplyResultByParam方法就跟普通的mybatis查询一致,只需要传入上级评论ID以及authorId即可,主要的逻辑就是在中间的递归,需要把每次查询的结果放入上级评论的children中,搞定!

Mybatis使用collection标签进行树形结构数据查询时如何携带外部参数查询

https://www.stonewu.com/post/mybatis-tree-query-param.html

作者

StoneWu

发布于

2019-11-01

更新于

2023-02-17

许可协议

评论