需求介绍

熟悉事务管理,并且应用到添加评论的功能。

数据层:增加评论数据,修改帖子的评论数量

业务层:处理添加评论的业务,先增加评论再更新帖子的评论数量(因为用到了两个DML操作所以要用到事务管理)

表现层:处理添加评论数据的请求,设置添加评论的表单

事务管理熟悉了之后,我们开发添加评论的需求。

先在数据层 CommentMapper 写 insertComment 方法增加评论数据

int insertComment(Comment comment);

  

然后去 comment-mapper.xml 写具体实现

<sql id="insertFields">
user_id, entity_type, entity_id, target_id, content, status, create_time
</sql> <insert id="insertComment" parameterType="Comment">
insert into comment(<include refid="insertFields"></include>)
values(#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})
</insert>

  

然后写更新帖子评论数量的 sql 语句,在 DiscussPostMapper 里面增加 updateCommentCount 方法

int updateCommentCount(int id, int commentCount);

  

在 discusspost-mapper.xml 里面写实现

<update id="updateCommentCount">
update discuss_post set comment_count = #{commentCount} where id = #{id}
</update>

  

至此数据层就结束了,去开发业务层。

首先我们帖子的业务组件 DiscussPostService 中增加一个方法 updateCommentCount 更新帖子数量,使得在开发评论的时候可以依赖这个业务组件

public int updateCommentCount(int id, int commentCount) {
return discussPostMapper.updateCommentCount(id, commentCount);
}

  

然后在 CommentService 里面增加处理添加评论的业务方法 addComment 了

// 因为要使用敏感词过滤把该工具注入进来
@Autowired
private SensitiveFilter sensitiveFilter; @Autowired
private SensitiveFilter sensitiveFilter; @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public int addComment(Comment comment) {
if (comment == null) {
throw new IllegalArgumentException("参数不能为空");
}
// 添加评论
comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
comment.setContent(sensitiveFilter.filter(comment.getContent()));
int rows = commentMapper.insertComment(comment); // 更新帖子评论数量
if (comment.getEntityType() == ENTITY_TYPE_POST) {
// 查到实体对应的评论数量
int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
discussPostService.updateCommentCount(comment.getEntityId(), count);
} return rows;
}

  

业务方法解决,最后去表现层 CommentController 里写 addComment 方法:

package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import java.util.Date; @Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {
@Autowired
private CommentService commentService; @Autowired
private HostHolder hostHolder; @Autowired
private EventProducer eventProducer; @Autowired
private DiscussPostService discussPostService; // 页面会提交内容和两个内容(实体类型和实体 ID)
@RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
// 得到当前用户的 ID
comment.setUserId(hostHolder.getUser().getId());
comment.setStatus(0);
comment.setCreateTime(new Date());
commentService.addComment(comment);
return "redirect:/discuss/detail/" + discussPostId;
}
}

  

最后就是处理页面了。

SpringBoot开发十九-添加评论的更多相关文章

  1. SpringBoot开发十八-显示评论

    需求介绍 显示评论,还是我们之前做的流程. 数据层:根据实体查询一页的评论数据,以及根据实体查询评论的数量 业务层:处理查询评论的业务,处理查询评论数量的业务 表现层:同时显示帖子详情数据时显示该帖子 ...

  2. 使用Typescript重构axios(二十九)——添加baseURL

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  3. SpringBoot | 第十九章:web应用开发之WebSocket

    前言 web开发也讲解了三章了,这章节开始讲解关于与前端通信相关知识.实现一个在线聊天室类似的功能或者后端推送消息到前端,在没有WebSocket时,读大学那伙还有接触过DWR(Direct Web ...

  4. SpringBoot | 第二十九章:Dubbo的集成和使用

    前言 今年年初时,阿里巴巴开源的高性能服务框架dubbo又开始了新一轮的更新,还加入了Apache孵化器.原先项目使用了spring cloud之后,已经比较少用dubbo.目前又抽调回原来的行业应用 ...

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

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

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

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

  7. python运维开发(十九)----Django后台表单验证、session、cookie、model操作

    内容目录: Django后台表单验证 CSRF加密传输 session.cookie model数据库操作 Django后台Form表单验证 Django中Form一般有2种功能: 1.用于做用户提交 ...

  8. SpringBoot第十九篇:邮件服务

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11118340.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   邮件的重要 ...

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

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

随机推荐

  1. 《计算机组成与体系结构:性能设计》读后小记 12、CPU的结构和功能

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

  2. GKCTF X DASCTF 2021_babycat复现学习

    17解的一道题,涉及到了java反序列化的知识,学习了. 看了下积分榜,如果做出来可能能进前20了哈哈哈,加油吧,这次就搞了两个misc签到,菜的扣脚. 打开后是个登录框,sign up提示不让注册, ...

  3. [Kong] key-auth实现对API请求的密钥认证

    目录 1. 配置密钥验证插件 2. 确认插件配置正确 3. 创建cunsumer 4. 给cunsumer提供关键凭证 5. 验证 6. 小结 [前言]: 下面我们将配置key-auth插件以向服务添 ...

  4. Linux常用基础命令(二)

    Linux常用基础命令 一.-ls--列表显示目录内容 二.-alias--设置别名 三.-du--统计目录及文件空间占用情况 四.-mkdir--创建新目录 五.-touch--创建空文件 六.-l ...

  5. 备战-Java 并发

    备战-Java 并发 谁念西风独自凉,萧萧黄叶闭疏窗 简介:备战-Java 并发. 一.线程的使用 有三种使用线程的方法: 实现 Runnable 接口: 实现 Callable 接口: 继承 Thr ...

  6. HTML5-CSS(二)

    一. CSS  文本样式 1.font-size p { font-size: 50px;}解释:设置文本的大小. xx-small.x-small.small.medium.large.x-larg ...

  7. war项目依赖war项目

    还没有看,立个flag:https://my.oschina.net/u/588379/blog/1817077

  8. Leetcode:637. 二叉树的层平均值

    Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition fo ...

  9. Oracle19c 如何用rman duplicate 克隆一个数据库。(Backup-Based, achive log)

    Oracle19c 如何用rman duplicate 克隆一个数据库.(Backup-Based, achive log) 首先克隆有两种方法,一种是Backup-Based,一种是Active方式 ...

  10. Linux核心目录结构

    ------------恢复内容开始------------ 目录 含义及作用 /usr/bin 普通用户二进制命令目录 /usr/sbin root管理员用户使用的二进制命令目录 /boot 内核程 ...