数据库表结构
-- auto-generated definition
create table group_post_comments
(
comment_id int auto_increment
primary key,
post_id int not null comment '帖子id',
user_id varchar(60) not null comment '用户id',
content text null comment '帖子内容(富文本)',
created_at timestamp default CURRENT_TIMESTAMP null comment '创建时间',
parent_post_id int null comment '父级评论id',
level_flag int null comment '评论等级:1主评论、2子评论',
whether_top int null comment '是否置顶评论',
)
comment '小组帖子评论表';
1、两层楼
/**
* 查看帖子评论
*
* @param page
* @param pageSize
* @param postId
* @return
*/
@Override
public PageResponse getComments(Integer page, Integer pageSize, Integer postId) {
PageHelper.startPage(page, pageSize);
List<GroupPostCommentsVo> list = groupPostCommentsMapper.findComment(postId);
PageInfo<GroupPostCommentsVo> pageInfo = new PageInfo<>(list);
List<GroupPostCommentsVo> pageInfoList = pageInfo.getList();
for (GroupPostCommentsVo groupPostCommentsVo : pageInfoList) {
// 调用deepSearch方法处理评论树形结构
deepSearch(groupPostCommentsVo);
}
// 返回分页信息结果
return PageResponse.of(pageInfo);
}
/**
* 递归进行求树形节点
*
* @param groupPostCommentsVo 评论树
*/
private void deepSearch(GroupPostCommentsVo groupPostCommentsVo) {
log.info("数据1:{}",groupPostCommentsVo);
if (groupPostCommentsVo != null) {
// 查询子评论集合
List<GroupPostCommentsVo> commentNodeTreeVos = groupPostCommentsMapper.findChildComment(groupPostCommentsVo.getPostId(),groupPostCommentsVo.getCommentId());
if (CollectionUtils.isNotEmpty(commentNodeTreeVos)) {
for (GroupPostCommentsVo vo : commentNodeTreeVos) {
if(groupPostCommentsVo.getGroupPostCommentsVos() == null){
groupPostCommentsVo.setGroupPostCommentsVos(new ArrayList<>());
}
groupPostCommentsVo.setGroupPostCommentsVos(commentNodeTreeVos);
log.info("数据2:{}",groupPostCommentsVo);
deepSearch(vo);
}
}
}
}
mapper.xml
<!-- //查看帖子评论-->
<select id="findComment" resultType="com.sdx.vo.GroupPostCommentsVo">
select
gpc.comment_id,
gpc.user_id,
gpc.content,
gpc.created_at,
gpc.post_id,
sfu.nickname,
sfu.avatar
from group_post_comments gpc
left join sdx_fd_user sfu on gpc.user_id = sfu.user_id
where gpc.post_id = #{postId}
and gpc.level_flag = 1
order by gpc.created_at desc
</select>
<!-- //查询子评论集合-->
<select id="findChildComment" resultType="com.sdx.vo.GroupPostCommentsVo">
select
gpc.comment_id,
gpc.user_id,
gpc.content,
gpc.created_at,
gpc.post_id,
sfu.nickname,
sfu.avatar
from group_post_comments gpc
left join sdx_fd_user sfu on gpc.user_id = sfu.user_id
where gpc.post_id = #{postId}
and gpc.parent_post_id = #{commentId}
and gpc.level_flag = 2
order by gpc.created_at desc
</select>
2、递归盖楼
server层
/**
* 查看帖子评论
*
* @param page
* @param pageSize
* @param postId
* @return
*/
@Override
public PageResponse getComments(Integer page, Integer pageSize, Integer postId) {
PageHelper.startPage(page, pageSize);
List<GroupPostCommentsVo> list = groupPostCommentsMapper.findComment(postId);
PageInfo<GroupPostCommentsVo> pageInfo = new PageInfo<>(list);
List<GroupPostCommentsVo> pageInfoList = pageInfo.getList();
for (GroupPostCommentsVo groupPostCommentsVo : pageInfoList) {
// 调用deepSearch方法处理评论树形结构
deepSearch(groupPostCommentsVo);
}
// 返回分页信息结果
return PageResponse.of(pageInfo);
}
/**
* 第二层评论
*
* @param groupPostCommentsVo 评论树
*/
private void deepSearch(GroupPostCommentsVo groupPostCommentsVo) {
log.info("数据1:{}", groupPostCommentsVo);
if (groupPostCommentsVo != null) {
// 查询子评论集合
List<GroupPostCommentsVo> commentNodeTreeVos = groupPostCommentsMapper.findChildComment(groupPostCommentsVo.getPostId(), groupPostCommentsVo.getCommentId());
if (groupPostCommentsVo.getGroupPostCommentsVos() == null) {
groupPostCommentsVo.setGroupPostCommentsVos(new ArrayList<>());
}
groupPostCommentsVo.setGroupPostCommentsVos(commentNodeTreeVos);
log.info("数据2:{}", groupPostCommentsVo);
}
}
mapper.xml
<!-- //查看帖子评论-->
<select id="findComment" resultType="com.sdx.vo.GroupPostCommentsVo">
select
gpc.comment_id,
gpc.user_id,
gpc.content,
gpc.created_at,
gpc.post_id,
gpc.parent_post_id,
gpc.level_flag,
sfu.nickname,
sfu.avatar
from group_post_comments gpc
left join sdx_fd_user sfu on gpc.user_id = sfu.user_id
where gpc.post_id = #{postId}
and gpc.level_flag = 1
order by gpc.created_at desc
</select>
<!-- //查询子评论集合-->
<select id="findChildComment" resultType="com.sdx.vo.GroupPostCommentsVo">
select
gpc.comment_id,
gpc.user_id,
gpc.content,
gpc.created_at,
gpc.post_id,
gpc.parent_post_id,
gpc.level_flag,
sfu.nickname,
sfu.avatar
from group_post_comments gpc
left join sdx_fd_user sfu on gpc.user_id = sfu.user_id
where gpc.post_id = #{postId}
and gpc.parent_post_id = #{commentId}
and gpc.level_flag = 2
order by gpc.created_at desc
</select>
3、评论表的CRUD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lwc.mapper.GroupPostCommentsMapper">
<!-- 新增评论-->
<insert id="addComments">
INSERT INTO group_post_comments (post_id, user_id, content, parent_post_id, level_flag, created_at)
VALUES (#{postId}, #{userId}, #{content}, #{levelFlag}, #{parentPostId},#{createdAt})
</insert>
<!-- //查看帖子评论-->
<select id="findComment" resultType="com.lwc.vo.GroupPostCommentsVo">
select
gpc.comment_id,
gpc.user_id,
gpc.content,
gpc.created_at,
gpc.post_id,
gpc.parent_post_id,
gpc.level_flag,
sfu.nickname,
sfu.avatar
from group_post_comments gpc
left join sdx_fd_user sfu on gpc.user_id = sfu.user_id
where gpc.post_id = #{postId}
and gpc.level_flag = 1
order by gpc.created_at desc
</select>
<!-- //查询子评论集合-->
<select id="findChildComment" resultType="com.lwc.vo.GroupPostCommentsVo">
select
gpc.comment_id,
gpc.user_id,
gpc.content,
gpc.created_at,
gpc.post_id,
gpc.parent_post_id,
gpc.level_flag,
sfu.nickname,
sfu.avatar
from group_post_comments gpc
left join sdx_fd_user sfu on gpc.user_id = sfu.user_id
where gpc.post_id = #{postId}
and gpc.parent_post_id = #{commentId}
and gpc.level_flag = 2
order by gpc.created_at desc
</select>
<!-- // 删除子评论-->
<delete id="deleteParentId">
delete
from group_post_comments
where parent_post_id = #{commentId}
</delete>
<!-- //根据评论id和帖子id查询评论-->
<select id="selectByIds" resultType="com.lwc.entity.GroupPostComments">
select comment_id, user_id, content, created_at, parent_post_id, level_flag
from group_post_comments
where comment_id = #{commentId}
and post_id = #{postId}
</select>
</mapper>