CREATE TABLE `comment` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL,
`entity_type` int DEFAULT NULL,
`entity_id` int DEFAULT NULL,
`target_id` int DEFAULT NULL,
`content` text,
`status` int DEFAULT NULL,
`create_time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_user_id` (`user_id`) /*!80000 INVISIBLE */,
KEY `index_entity_id` (`entity_id`)
) ENGINE=InnoDB AUTO_INCREMENT=232 DEFAULT CHARSET=utf8mb3
entity_type=1
时,该实体是一个帖子的评论;
entity_type=2
时,该实体是一个帖子中评论的回复;
package com.nowcoder.community.entity;
import java.util.Date;
public class Comment {
private int id;
private int userId;
private int entityType;
private int entityId;
private int targetId;
private String Content;
private int status;
private Date createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getEntityType() {
return entityType;
}
public void setEntityType(int entityType) {
this.entityType = entityType;
}
public int getEntityId() {
return entityId;
}
public void setEntityId(int entityId) {
this.entityId = entityId;
}
public int getTargetId() {
return targetId;
}
public void setTargetId(int targetId) {
this.targetId = targetId;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getcreateTime() {
return createTime;
}
public void setcreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", userId=" + userId +
", entityType=" + entityType +
", entityId=" + entityId +
", targetId=" + targetId +
", Content='" + Content + '\'' +
", status=" + status +
", createTime=" + createTime +
'}';
}
}
package com.nowcoder.community.service;
import com.nowcoder.community.dao.CommentMapper;
import com.nowcoder.community.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
public List<Comment> findCommentsByEntity(int entityType, int entityId, int offset, int limit){
return commentMapper.selectCommentsByEntity(entityType,entityId,offset,limit);
}
public int findCommentCount(int entityType, int entityId){
return commentMapper.selectCountByEntity(entityType,entityId);
}
}
selectCommentsByEntity
:从common数据库中选出某帖子的评论(entity_type=1)或回复(entity_type=2);selectCountByEntity
:从common数据库中计算某帖子评论数或某帖子的回复数;<?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.nowcoder.community.dao.CommentMapper">
<sql id="selectFields">
id, user_id, entity_type, entity_id, target_id, content, status, create_time
</sql>
<select id="selectCommentsByEntity" resultType="Comment">
select <include refid="selectFields"></include>
from comment
where status = 0
and entity_type = #{entityType}
and entity_id = #{entityId}
order by create_time asc
limit #{offset}, #{limit}
</select>
<select id="selectCountByEntity">
select count(id)
from comment
where status = 0
and entity_type = #{entityType}
and entity_id = #{entityId}
</select>
</mapper>
@RequestMapping(path="/detail/{discussPostId}", method = RequestMethod.GET)
public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){
// 帖子信息
DiscussPost discussPost = discussPostService.findDiscussPostById(discussPostId);
model.addAttribute("post",discussPost);
// 帖子作者
User user = userService.findUserById(discussPost.getUserId());
model.addAttribute("user",user);
//评论的分页信息
page.setLimit(5); //每页显示5条评论
page.setPath("/discuss/detail/"+discussPostId);
page.setRows(discussPost.getCommentCount());
// 评论:给帖子的回复
// 回复:给评论的评论
// 评论列表
List<Comment> commentList = commentService.findCommentsByEntity(
ENTITY_TYPE_POST, discussPost.getId(), page.getOffset(), page.getLimit());
// 评论的VO列表
// 统一封装要显示的数据
List<Map<String,Object>> commentVoList = new ArrayList<>();
if(commentList != null){ // 评论列表不为空
for(Comment comment: commentList){
// 一个评论VO
Map<String, Object> commentVo = new HashMap<>();
// 评论
commentVo.put("comment",comment);
// 作者
commentVo.put("user", userService.findUserById(comment.getUserId()));
// 回复列表
List<Comment> replyList = commentService.findCommentsByEntity(
ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
// 回复VO列表
List<Map<String,Object>> replyVoList = new ArrayList<>();
if(replyList != null){
for(Comment reply:replyList){
Map<String,Object> replyVo = new HashMap<>();
// 回复
replyVo.put("reply",reply);
// 作者
replyVo.put("user", userService.findUserById(reply.getUserId()));
// 回复的目标
User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getUserId());
replyVo.put("target",target);
replyVoList.add(replyVo);
}
}
commentVo.put("replys", replyVoList);
// 评论回复的数量
int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());
commentVo.put("replyCount",replyCount);
commentVoList.add(commentVo);
}
}
model.addAttribute("comments",commentVoList);
return "/site/discuss-detail";
}
}