本文共 2750 字,大约阅读时间需要 9 分钟。
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}
DiscussPostService:
public int updateCommentCount(int id, int commentCount) { return discussPostMapper.updateCommentCount(id, commentCount); }
CommentService:
事务管理,声明式配置(整个),编程式(部分),这里采用声明式配置。@Transactional(隔离级别、传播机制)
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
@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 }
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,三个表单要提交(回帖,回复评论,回复评论下的特定人)
回帖:
回复评论:
评论下评论,回复某人:
转载地址:http://ssnlf.baihongyu.com/