2021-01-24 16:55:16 +08:00
|
|
|
# 添加评论
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
事务管理(在添加评论失败的时候进行回滚)
|
|
|
|
|
|
|
|
- 声明式事务(推荐,简单)
|
|
|
|
- 编程式事务
|
|
|
|
|
|
|
|
<img src="https://gitee.com/veal98/images/raw/master/img/20210123142151.png" style="zoom: 50%;" />
|
|
|
|
|
|
|
|
## DAO
|
|
|
|
|
|
|
|
`DiscussPostMapper`
|
|
|
|
|
|
|
|
```java
|
|
|
|
/**
|
|
|
|
* 修改帖子的评论数量
|
|
|
|
* @param id 帖子 id
|
|
|
|
* @param commentCount
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int updateCommentCount(int id, int commentCount);
|
|
|
|
```
|
|
|
|
|
|
|
|
对应的 xml
|
|
|
|
|
|
|
|
```xml
|
|
|
|
<!--修改帖子的评论数量-->
|
|
|
|
<update id="updateCommentCount">
|
|
|
|
update discuss_post
|
|
|
|
set comment_count = #{commentCount}
|
|
|
|
where id = #{id}
|
|
|
|
</update>
|
|
|
|
```
|
|
|
|
|
|
|
|
`CommentMapper`
|
|
|
|
|
|
|
|
```java
|
|
|
|
/**
|
|
|
|
* 添加评论
|
|
|
|
* @param comment
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int insertComment(Comment comment);
|
|
|
|
```
|
|
|
|
|
|
|
|
对应的 xml
|
|
|
|
|
|
|
|
```xml
|
|
|
|
<!--添加评论-->
|
|
|
|
<insert id = "insertComment" parameterType="Comment">
|
|
|
|
insert into comment(<include refid="insertFields"></include>)
|
|
|
|
values(#{userId}, #{entityType}, #{entityId}, #{targetId}, #{content}, #{status}, #{createTime})
|
|
|
|
</insert>
|
|
|
|
```
|
|
|
|
|
|
|
|
## Service
|
|
|
|
|
|
|
|
`DiscussPostService`
|
|
|
|
|
|
|
|
```java
|
|
|
|
/**
|
|
|
|
* 修改帖子的评论数量
|
|
|
|
* @param id 帖子 id
|
|
|
|
* @param commentCount
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int updateCommentCount(int id, int commentCount) {
|
|
|
|
return discussPostMapper.updateCommentCount(id, commentCount);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
`CommentService`
|
|
|
|
|
|
|
|
```java
|
|
|
|
/**
|
|
|
|
* 添加评论(需要事务管理)
|
|
|
|
* @param comment
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
|
|
|
|
public int addComment(Comment comment) {
|
|
|
|
if (comment == null) {
|
|
|
|
throw new IllegalArgumentException("参数不能为空");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Html 标签转义
|
|
|
|
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());
|
|
|
|
discussPostSerivce.updateCommentCount(comment.getEntityId(), count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Controller
|
|
|
|
|
2021-01-28 22:09:02 +08:00
|
|
|
**前端的 name = "xxx" 和 `public String addComment(Comment comment) {` Comment 实体类中的字段要一一对应**,这样即可直接从前端传值。
|
2021-01-24 16:55:16 +08:00
|
|
|
|
|
|
|
```java
|
|
|
|
@Controller
|
|
|
|
@RequestMapping("/comment")
|
|
|
|
public class CommentController {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private HostHolder hostHolder;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CommentService commentService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加评论
|
|
|
|
* @param discussPostId
|
|
|
|
* @param comment
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@PostMapping("/add/{discussPostId}")
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## 前端
|
|
|
|
|
|
|
|
```html
|
|
|
|
<form method="post" th:action="@{|/comment/add/${post.id}|}">
|
|
|
|
<input type="text" class="input-size" name="content" th:placeholder="|回复${rvo.user.username}|"/>
|
|
|
|
<input type="hidden" name="entityType" value="2">
|
|
|
|
<input type="hidden" name="entityId" th:value="${cvo.comment.id}">
|
|
|
|
<input type="hidden" name="targetId" th:value="${rvo.user.id}">
|
|
|
|
</form>
|
|
|
|
```
|