显示评论

tong11 / 2023-08-25 / 原文

显示评论

数据层

根据实体查询一页评论数据。

根据实体查询评论的数量。

业务层

处理查询评论的业务。

处理查询评论数量的业务。

表现层

显示帖子详情数据时

同时显示该帖子所有的评论数据。

帖子回复目标有变化

  • entity_type 评论目标类型

  • entity_id 该类型具体目标

  • target_id 针对某个人回复评论中的评论

  • content 评论内容

  • status 评论状态

1.entity 里创建Comment实体类

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) {
       this.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 +
               '}';
  }
}

2.创建CommentMapper接口

@Mapper
public interface CommentMapper {

   //根据实体来查询 offset分页
   List<Comment> selectCommentsByEntity(int entityType, int entityId, int offset, int limit);

   int selectCountByEntity(int entityType, int entityId);

}

3.创建comment-mapper.xml文件

<?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" resultType="int">
       select count(id)
       from comment
       where status = 0
       and entity_type = #{entityType}
       and entity_id = #{entityId}
   </select>

</mapper>

3.创建 CommentService

@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);
  }

4.DiscussPostController

@RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)
public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {
   // 帖子
   DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
   model.addAttribute("post", post);
   // 作者
   User user = userService.findUserById(post.getUserId());
   model.addAttribute("user", user);

   // 评论分页信息
   page.setLimit(5);
   page.setPath("/discuss/detail/" + discussPostId);
   page.setRows(post.getCommentCount());

   // 评论: 给帖子的评论
   // 回复: 给评论的评论
   // 评论列表
   List<Comment> commentList = commentService.findCommentsByEntity(
           ENTITY_TYPE_POST, post.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.getTargetId());
                   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";
}

5.修改discuss-detail.html模板

测试结果