SpringBoot开发十九-添加评论

需求介绍

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

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

业务层:处理添加评论的业务,先增加评论再更新帖子的评论数量(因为用到了两个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;
    }
}

  

最后就是处理页面了。

posted @ 2021-07-06 16:54  宋同学shl  阅读(379)  评论(1)    收藏  举报