1.1对于数据层 增加评论数量
在CommentMapper下,新增
int insertComment(Comment comment);//返回行数
然后在CommentMapper中,补充
<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>
1.2对于数据层 更新帖子数量
该字段是帖子的数量,为了查询时方便
当增加评论时,需要更新 该字段。
在DiscussPostMapper中,
int updateCommentCount(int id, int commentCount);//根据id更新commentCount
在DiscussPostMapper.xml
<update id="updateCommentCount">
update discuss_post set comment_count = #{commentCount} where id = #{id}
</update>
在DiscussPostService中
public int updateCommentCount(int id, int commentCount) {
return discussPostMapper.updateCommentCount(id, commentCount);
}
2处理添加评论的业务:
在CommentService中
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)//因为包含两次dml操作,所以需要事务管理,保证两次操作在一次事务之内,要不全成功,要么全失败。设置隔离级别与传播机制。
public int addComment(Comment comment) {
if (comment == null) {
throw new IllegalArgumentException("参数不能为空!");
}
// 添加评论
comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));//过滤标签工具 HtmlUtils.htmlEscape。
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());//根据帖子id,查到帖子评论数量count
discussPostService.updateCommentCount(comment.getEntityId(), count);
}
return rows;
}
3.最后处理表现层
在controller里处理浏览器的请求,其次对页面做出一些改进。
新建CommentController
package com.nowcoder.community.controller;
import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.service.CommentService;
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 {
@Autowired
private CommentService commentService;
@Autowired
private HostHolder hostHolder;
@RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)//添加评论后要返回原贴,所以需要添加帖子id discussPostId
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {//因为要提交数据,需要一个实体comment来方便接收
comment.setUserId(hostHolder.getUser().getId());//根据hostHolder得到当前id
comment.setStatus(0);//表示有效状态
comment.setCreateTime(new Date());//当前时间
commentService.addComment(comment);//添加评论
return "redirect:/discuss/detail/" + discussPostId;//跳回帖子详情页面
}
}
接下来处理 帖子详情页面模板
有三处地方可以回帖:
其对应代码在207行
<!-- 回帖输入 -->
<div class="container mt-3">
<form class="replyform" method="post" th:action="@{|/comment/add/${post.id}|}"><!--提交方式post,提交路径里面包含动态的参数(帖子id) th:action="@{|/comment/add/${post.id} -->
<p class="mt-3">
<a name="replyform"></a>
<textarea placeholder="在这里畅所欲言你的看法吧!" name="content"></textarea>
<input type="hidden" name="entityType" value="1"> <!-- input是隐藏框,因为是回复给帖子的,所以类型为1-->
<input type="hidden" name="entityId" th:value="${post.id}"> <!-- entityId是个变量,为帖子id -->
</p>
<p class="text-right">
<button type="submit" class="btn btn-primary btn-sm"> 回 帖 </button>
</p>
</form>
</div>
</div>
其对应代码:
<!-- 回复输入框 -->
<li class="pb-3 pt-3">
<form method="post" th:action="@{|/comment/add/${post.id}|}">
<div>
<input type="text" class="input-size" name="content" placeholder="请输入你的观点"/>
<input type="hidden" name="entityType" value="2"><!-- 回复给评论的,因此Type为2 -->
<input type="hidden" name="entityId" th:value="${cvo.comment.id}">
</div>
<div class="text-right mt-2">
<button type="submit" class="btn btn-primary btn-sm" onclick="#"> 回 复 </button>
</div>
</form>
</li>
其对应代码在162行:
<form method="post" th:action="@{|/comment/add/${post.id}|}">
<div>
<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}">
</div>
<div class="text-right mt-2">
<button type="submit" class="btn btn-primary btn-sm" onclick="#"> 回 复 </button>
</div>
</form>
测试:
打开首页:
随便点开一个帖子,发现里面有16个回帖
当我在下面输入
回帖后,跳回页面
发现回帖数变为17
在最后可以看到我们发的帖子
接下来给评论写一个回复:
回复成功