目录
1.需求讲解
在发表评论的时候,我们需要点击发表内容的首页,然后发表情况。这里有两种情况
1.对当前发表内容的评论
2.回复评论内容
这里提供一个解决思路
评论区可以看成一个树形的结构。如果在一张表进行时,我们需要记录父级评论的id,评论首次显示为父级评论id为0的评论,并在该实体类中添加对应的子节点并返回。
2.建表
create table tb_blog_comments
(
id bigint unsigned auto_increment comment '主键'
primary key,
user_id bigint unsigned not null comment '用户id',
blog_id bigint unsigned not null comment '点评id',
parent_id bigint unsigned not null comment '关联的1级评论id,如果是一级评论,则值为0',
content varchar(255) not null comment '回复的内容',
liked int unsigned null comment '点赞数',
status tinyint default 0 null comment '0表示正常,1被举报,2禁止',
create_time timestamp default CURRENT_TIMESTAMP not null comment '创建时间',
update_time timestamp default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
is_deleted tinyint default 0 not null comment '删除标记(0:可用 1:删除)'
)
comment '点评评论表';
3.实体类
@Getter
@Setter
@TableName("tb_blog_comments")
@ApiModel(value = "BlogComments对象", description = "评论表评论")
public class BlogComments implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("主键")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty("用户id")
@TableField("user_id")
private Long userId;
@ApiModelProperty("点评id")
@TableField("blog_id")
private Long blogId;
@ApiModelProperty("关联的1级评论id,如果是一级评论,则值为0")
@TableField("parent_id")
private Long parentId;
@ApiModelProperty("回复的内容")
@TableField("content")
private String content;
@ApiModelProperty("点赞数")
@TableField("liked")
private Integer liked;
@ApiModelProperty("状态,0:正常,1:被举报,2:禁止查看")
@TableField("`status`")
private Integer status;
@ApiModelProperty("创建时间")
@TableField("create_time")
private LocalDateTime createTime;
@ApiModelProperty("更新时间")
@TableField("update_time")
private LocalDateTime updateTime;
@ApiModelProperty("删除标记(0:可用 1:删除)")
@TableField("is_deleted")
@TableLogic
private Integer isDeleted;
/*
* 下级表
* */
@TableField(exist = false)
private List<BlogComments> children;
}
3.代码
/*
* 递归方法创建目录,寻找子结点
* */
private BlogComments findChildren(BlogComments blogComment, List<BlogComments> treeNodes) {
blogComment.setChildren(new ArrayList<BlogComments>());
for (BlogComments tn : treeNodes) {
if (blogComment.getId().longValue() == tn.getParentId().longValue()) {
if (blogComment.getChildren() == null) {
blogComment.setChildren(new ArrayList<>());
} else {
blogComment.getChildren().add(findChildren(tn, treeNodes));
}
}
}
return blogComment;
}
/*
*
* 合并子结点
* */
private List queryAllComments(List<BlogComments> blogComments) {
blogComments.stream().forEach(a -> {
findChildren(a, blogComments);
});
List<BlogComments> comments = blogComments.stream().filter(bc -> bc.getParentId()==0L).collect(Collectors.toList());
return comments;
}
/*
* 查询评论表内容
* */
@Override
public Result commentByBlogId(String blogId) {
QueryWrapper<BlogComments> wrapper = new QueryWrapper<>();
wrapper.eq("blog_id", blogId);
BlogCommentsMapper baseMapper = this.getBaseMapper();
List<BlogComments> blogComments = baseMapper.selectList(wrapper);
return Result.ok(queryAllComments(blogComments));
}
4.结果集