仿牛客网社区项目(十六)添加评论
新增评论
- 数据层
- 增加评论数据。
- 修改帖子的评论数量。
- 业务层
- 处理添加评论的业务:先增加评论、再更新帖子的评论数量。
- 表现层
- 处理添加评论数据的请求。
1、数据访问层#
CommentMapper
int insertComment(Comment comment);
1
<insert id="insertComment" parameterType="Comment">
insert into comment(<include refid="insertFields"></include>)
values(#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})
</insert>
DiscussPostMapper
int updateCommentCount(int id, int commentCount);//返回更新的行数
1
<update id="updateCommentCount">
update discuss_post set comment_count = #{commentCount}
where id = #{id}
</update>
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,三个表单要提交(回帖,回复评论,回复评论下的特定人)
<form class="replyform" method="post" th:action="@{|/comment/add/${post.id}|}">
name="content"
<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}">
<button type="submit"
回帖:
<!-- 回帖输入 -->
<div class="container mt-3">
<form class="replyform" method="post" 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 type="hidden" name="entityId" th:value="${post.id}">
</p>
<p class="text-right">
<button type="submit" class="btn btn-primary btn-sm"> 回 帖 </button>
</p>
</form>
</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">
<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>
评论下评论,回复某人:
<div th:id="|huifu-${rvoStat.count}|" class="mt-4 collapse">
<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>
</div>
分类:
仿牛客网社区项目
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署