需求介绍

显示评论,还是我们之前做的流程。

数据层:根据实体查询一页的评论数据,以及根据实体查询评论的数量

业务层:处理查询评论的业务,处理查询评论数量的业务

表现层:同时显示帖子详情数据时显示该帖子的所有的评论的数量和数据

代码介绍

首先新增一个实体类 Comment

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

  

同理新建数据层 CommentMapper

package com.nowcoder.community.dao;

import com.nowcoder.community.entity.Comment;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper
public interface CommentMapper {
// 根据实体来查询到的评论,要分页
List<Comment> selectCommentsByEntity(int entityType, int entityId, int offset, int limit); int selectCountByEntity(int entityType, int entityId);
}

  

然后编写对应的 comment-mapper.xml 实现对应的 sql 语句

<?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>

  

然后写业务层 CommentService 调用数据层的方法

package com.nowcoder.community.service;

import com.nowcoder.community.dao.CommentMapper;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils; import java.util.List; @Service
public class CommentService implements CommunityConstant{
@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);
}
}

  

然后在帖子详情的页面,重构一下 getDiscussPost 方法,因为之前只是显示了帖子内容的详情,现在要增加对应的评论,所以重写一下对应的方法。

// 因为熟悉了 mysql 的表,我们知道我的评论是有着对应的实体类型评论,它是一个常量代表,所以要在我们确定好的常量接口里面声明这些常量,然后再去 CommentController 里面重构 getDiscussPost 方法
/**
* 实体类型:帖子
*/
int ENTITY_TYPE_POST = 1; /**
* 实体类型:评论
*/
int ENTITY_TYPE_COMMENT = 2; /**
* 实体类型: 用户
*/
int ENTITY_TYPE_USER = 3; // 补充对应的评论的 commentService
@Autowired
private CommentService commentService; @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);
page.setPath("/discuss/detail/" + discussPostId);
page.setRows(discussPost.getCommentCount()); // 评论:给帖子的评论
// 回复:给评论的评论
// 评论列表
List<Comment> commentList = commentService.findCommentsByEntity(
1, 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.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";
}

  

SpringBoot开发十八-显示评论的更多相关文章

  1. SpringBoot开发十九-添加评论

    需求介绍 熟悉事务管理,并且应用到添加评论的功能. 数据层:增加评论数据,修改帖子的评论数量 业务层:处理添加评论的业务,先增加评论再更新帖子的评论数量(因为用到了两个DML操作所以要用到事务管理) ...

  2. python运维开发(十八)----Django(二)

    内容目录 路由系统 模版 Ajax model数据库操作,ORM 路由系统 django中的路由系统和其他语言的框架有所不同,在django中每一个请求的url都要有一条路由映射,这样才能将请求交给对 ...

  3. [转]Angular开发(十八)-路由的基本认识

    angular router https://angular.io/guide/router 本文转自:https://blog.csdn.net/kuangshp128/article/detail ...

  4. SpringBoot开发十-开发登录,退出功能

    需求介绍-开发登录,退出功能 访问登录页面:点击头部区域的链接打开登录页面 登录: 验证账号,密码,验证码 成功时生成登录凭证发放给客户端,失败时跳转回登录页面 退出: 将登录状态修改为失效的状态 跳 ...

  5. SpringBoot第十八篇:异步任务

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11095891.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   系统中的异 ...

  6. SpringBoot开发十六-帖子详情

    需求介绍 实现帖子详情,在帖子标题上增加访问详情页面的链接. 代码实现 开发流程: 首先在数据访问层新增一个方法 实现查看帖子的方法 业务层同理增加查询方法 最后在表现层处理查询请求 数据访问层增加根 ...

  7. SpringBoot开发十五-发布帖子

    需求介绍 使用 AJAX 异步通信实现网页能够增量的更新呈现到页面上而不需要刷新整个页面. 现在基本上都是服务器返回 JSON 字符串来解析 代码实现 使用 JQuery 发送 AJAX 请求. 首先 ...

  8. unity3D游戏开发十八之NGUI动画

    我们先来看下帧动画,顾名思义,就是一帧帧的图片组成的动画,我们须要用到UISprite Animation组件,它的属性例如以下: Framerate:播放速率,也就是每秒钟播放的帧数 Name Pr ...

  9. SpringBoot开发十四-过滤敏感词

    项目需求-过滤敏感词 利用 Tire 树实现过滤敏感词 定义前缀树,根据敏感词初始化前缀树,编写过滤敏感词的方法 代码实现 我们首先把敏感词存到一个文件 sensitive.txt: 赌博 嫖娼 吸毒 ...

随机推荐

  1. Ubuntu创建图标

    起因 安装一些软件时,总是没有图标,导致无法固定到docky栏,所以极为不方便,所以需要自己创建图标. 操作 以创建微信图标为例 [Desktop Entry] Name=Wecaht #名字 Com ...

  2. 26 bash shell中的信号

    当没有任何捕获时,一个交互式 Bash Shell 会忽略 SIGTERM(发送到进程的 TERM 信号用于要求进程终止) 和 SIGQUIT(当用户要求进程执行 core dump 时,QUIT 信 ...

  3. ABP Framework 为什么好上手,不好深入?探讨最佳学习姿势!

    离写上一篇经验总结 ABP Framework 研习社经验总结(6.28-7.2) ,已经过去两周. ABP Framework 研习社(QQ群:726299208) 最近一周,又迎来了很多新伙伴,成 ...

  4. Exception 和Error异常大部分人都犯过的错。

    先看再点赞,给自己一点思考的时间,如果对自己有帮助,微信搜索[程序职场]关注这个执着的职场程序员. 我有什么:职场规划指导,技能提升方法,讲不完的职场故事,个人成长经验. 1,简介 Exception ...

  5. STM32笔记三

    1.单片机有两种存储器,程序存储器用来存储编写的程序,数据存储器用来存储单片机工作时的临时数据.内部存储器分为工作寄存器区.位寻址区.数据缓存区和特殊功能寄存器区. 2.位:数据存储的最小单位.在计算 ...

  6. echarts堆叠柱状图在最上面的柱子显示总和

    需求 柱子需设置barMinHeight 在堆叠柱状图的最上面显示当前堆叠的总和 直接上代码吧 需要注意:设置barMinHeight时为了让0不显示,只能将0设置为null; 设置为null的柱子l ...

  7. python pyinsane应用

    import sys from PIL import Image try: import src.abstract as pyinsane except ImportError: import pyi ...

  8. ELK多索引配置(filebeat)

     nginx日志收集: ​ ​ #-------------------------输入 filebeat.inputs: ​ #----------json日志---------- ​ - type ...

  9. 1.在配置XML文件时出现reference file contains errors (http://www.springframework.org/schema/beans/...解决方案

    解决方案: 第一步:将 Preferences > XML > XML Files > Validation中"Honour all XML schema location ...

  10. Python 创建一个Django项目

    1 环境搭建及创建 1) 安装Django 方法一:pip install django 方法二:Pycharm File--settings--Project--Python Interpreter ...