博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
仿牛客社区项目3.6——增加评论,同时更新评论数【事务】
阅读量:2055 次
发布时间:2019-04-28

本文共 2750 字,大约阅读时间需要 9 分钟。

在这里插入图片描述

1、数据访问层

CommentMapper

int insertComment(Comment comment);
insert into comment(
) values(#{
userId},#{
entityType},#{
entityId},#{
targetId},#{
content},#{
status},#{
createTime})

DiscussPostMapper

int updateCommentCount(int id, int commentCount);//返回更新的行数
update discuss_post set comment_count = #{
commentCount} where id = #{
id}

2、业务层

DiscussPostService:

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

CommentService:

事务管理,声明式配置(整个),编程式(部分),这里采用声明式配置。@Transactional(隔离级别、传播机制)

@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
  • 判空
  • 添加评论
    • 内容过滤(标签、敏感词)
    • 存到数据库comment表中,返回增加成功行数rows
  • 更新评论数量
    • 查数据库comment表中帖子数量
    • 帖子数量更新到discuss_post表中
@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);// 存到数据库comment表中,返回增加成功行数rows // 更新帖子评论数量 if (comment.getEntityType() == ENTITY_TYPE_POST) {
// 查数据库comment表中帖子数量 int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId()); // 帖子数量更新到discuss_post表中 discussPostService.updateCommentCount(comment.getEntityId(), count); } return rows;// 返回增加成功行数rows }

3、表现层

CommentController

@Controller@RequestMapping("/comment")public class CommentController {
@Autowired private CommentService commentService; @Autowired private HostHolder hostHolder; @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)// 为了重定向用 public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
comment.setUserId(hostHolder.getUser().getId()); comment.setStatus(0); comment.setCreateTime(new Date()); commentService.addComment(comment); return "redirect:/discuss/detail/" + discussPostId; }}

discuss-detail.html,三个表单要提交(回帖,回复评论,回复评论下的特定人)

name="content"

回帖:

在这里插入图片描述

回复评论:

在这里插入图片描述

  • 评论下评论,回复某人:

    在这里插入图片描述

    >

    转载地址:http://ssnlf.baihongyu.com/

    你可能感兴趣的文章
    [Kick Start 2020] Round A 1.Allocation
    查看>>
    [Kick Start 2020] Round A 2.Plates
    查看>>
    Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
    查看>>
    Leetcode C++ 《第181场周赛-2》 1390. 四因数
    查看>>
    阿里云《云原生》公开课笔记 第一章 云原生启蒙
    查看>>
    阿里云《云原生》公开课笔记 第二章 容器基本概念
    查看>>
    阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
    查看>>
    阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
    查看>>
    阿里云《云原生》公开课笔记 第五章 应用编排与管理
    查看>>
    阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
    查看>>
    阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
    查看>>
    阿里云《云原生》公开课笔记 第八章 应用配置管理
    查看>>
    阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
    查看>>
    linux系统 阿里云源
    查看>>
    国内外helm源记录
    查看>>
    牛客网题目1:最大数
    查看>>
    散落人间知识点记录one
    查看>>
    Leetcode C++ 随手刷 547.朋友圈
    查看>>
    手抄笔记:深入理解linux内核-1
    查看>>
    内存堆与栈
    查看>>