需求介绍

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

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

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

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

代码介绍

首先新增一个实体类 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. 《计算机组成与体系结构:性能设计》读后小记 12、CPU的结构和功能

    一.CPU组成 1.组成: ALU:算术逻辑单元,完成数据的实际计算或处理 控制器:控制数据的移入移出CPU 寄存器:由一组存储位置极小的内部存储器组成 CPU内部总线:在各寄存器和ALU之间传送数据 ...

  2. 大数据-Hadoop虚拟机的准备以及配置(三台机子)

    虚拟机的准备 修改静态IP(克隆的虚拟机) vim /etc/udev/rules.d/70-persistent-net.rules 配置网络: Vim /etc/sysconfig/network ...

  3. javax.naming.NoInitialContextException:Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

    小弟初次用JNDI,使用数据源连接数据库,配置完相关的xml文件后,激动的我赶紧测试了一下,结果悲剧了,报出了错误: javax.naming.NoInitialContextException:Ne ...

  4. buu 相册

    一.拖入jeb,这个神器里面,感觉对jeb使用还是不熟悉,对我逆向产生了一些障碍. 抓住题目给的提示,邮箱,全局直接搜索,mail. 看下它的交叉引用 找到了发邮件的方法, C2的MAILFROME说 ...

  5. 不同版本docker修改存储位置补充

    前言:最近发现yum安装docker,安装的版本不一样,有点蛇皮,虽然存放默认位置都是/var/lib/docker,但是它的配置文件不一样,这里做个补充 对于docker版本是1.13及以下 操作如 ...

  6. 从新建文件夹开始构建UtopiaEngine(2)

    本篇序言 摸了两个月的鱼,又一次拾起了自己引擎的框架,开始完善引擎系统,如果非要用现实中的什么东西比喻的话,那么我们目前实现的框架连个脚手架都不是.把这项目这样晾着显然不符合本人的风格,而且要作为毕业 ...

  7. Pytorch的模型加速方法:Dataparallel (DP) 和 DataparallelDistributedparallel (DDP)

    Dataparallel 和 DataparallelDistributed 的区别 一.Dataparallel(DP) 1.1 Dartaparallel 的使用方式 Dataparallel 的 ...

  8. 「 题解」NOIP2021模拟赛(2021-07-19)

    小兔的话 欢迎大家在评论区留言哦~ D - 矩阵 简单题意 一个 \(i * i\) 的 \(01\) 矩阵,若满足 每一行 和 每一列 都满足 恰好 有 \(2\) 个位置是 \(1\) 时,称为 ...

  9. [.NET大牛之路 003] .NET 的发展简史

    如果你热爱某产品,你一定想了解它的历史.今天我们简单回顾一下 .NET 平台的发展历史,来看看它在历史上的里程碑,这对我们更好地了解和学习 .NET 有着重要的意义. 微软 .NET 战略初衷 20 ...

  10. File类与常用IO流第六章——使用try...catch...finally处理流中的异常

    在JDK1.7之前: 1 package com.itheima.demo06.trycatch; 2 3 import java.io.FileWriter; 4 import java.io.IO ...