【精】3.22添加评论

在这里插入图片描述

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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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="#">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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="#">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button>
												</div>
											</form>

测试:

打开首页:
在这里插入图片描述
随便点开一个帖子,发现里面有16个回帖
在这里插入图片描述
当我在下面输入
在这里插入图片描述
回帖后,跳回页面
发现回帖数变为17

在这里插入图片描述
在最后可以看到我们发的帖子
在这里插入图片描述
接下来给评论写一个回复:
在这里插入图片描述
回复成功
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CMake是一个跨平台的构建工具,用于自动生成各种编译工具(如Makefile、Visual Studio解决方案等)的配置文件。CMake 3.22.5是CMake的一个特定版本。 CMake 3.22.5是CMake在其开发历史中的一个里程碑版本。它可能包含了一些修复和改进,以提高对各种编程语言(如C、C++、Python等)和平台(如Windows、Linux、macOS等)的支持。 CMake 3.22.5可能包含一些重要的新功能或更新的特性。这些功能可能包括新的模块、插件或命令,以及改进的语法支持。具体来说,CMake 3.22.5可能提供了更简洁、更高效的构建脚本编写方式,使开发人员能够更方便地配置和构建他们的项目。 此外,CMake 3.22.5还可能包含了一些错误修复,以解决之前版本中的一些已知问题。这些修复可能包括与特定编译器或操作系统相关的问题,或者与特定库或框架的兼容性问题有关。 总结而言,CMake 3.22.5是CMake的一个版本,有可能包含了一些新功能、更新的特性和错误修复,以提供更好的跨平台构建支持。使用CMake 3.22.5可以更方便地配置和构建各种项目,并与不同编程语言和操作系统进行更好的集成。 ### 回答2: CMake是一个开源的跨平台自动化构建工具,可以用于简化软件项目的构建过程。它使用名为CMakeLists.txt的配置文件来描述项目构建规则,并生成与目标平台相关的构建脚本(如makefile或Visual Studio项目文件)。 CMake 3.22.5是CMake软件的一个特定版本。升级到CMake 3.22.5可能会带来一些新的功能、改进和修复,以提高构建过程的效率和可靠性。一些可能的变化包括: 1. 新功能:CMake 3.22.5版本可能添加了一些新的功能,如新增了一些命令或模块,以便开发者更方便地配置和构建项目。 2. 改进:该版本可能提供了一些已有功能的改进,以提高构建的性能、可维护性或用户体验。这些改进可能包括bug修复、优化算法或改进命令的用法。 3. 修复:CMake 3.22.5可能解决了之前版本中存在的一些错误或问题。这些修复可能涉及到各种方面,如解决已知的bug、兼容性问题、性能问题等。 使用CMake 3.22.5的好处包括更容易管理项目的依赖关系、更方便地生成构建脚本、更高效地构建项目等。同时,由于CMake是跨平台的,因此可以在不同的操作系统和编译器上使用CMake 3.22.5来构建项目。 总之,CMake 3.22.5是CMake软件的一个特定版本,升级到该版本可能会带来一些新功能、改进和修复,以提高项目的构建过程。 ### 回答3: CMake是一个跨平台的开源构建工具,用于管理软件项目的构建过程。CMake的版本3.22.5是CMake 3系列中的一个版本,它是在2022年5月发布的最新稳定版本。CMake 3.22.5提供了一些功能更新和Bug修复,以提高构建过程的效率和稳定性。 在CMake 3.22.5中,可能包含一些与以前版本不兼容的更改。这些更改可能涉及CMake的语法、模块、命令或功能等方面。因此,在升级到CMake 3.22.5之前,用户应该仔细研究和了解相关文档,以确保项目能够顺利迁移到新版本。 CMake 3.22.5还可能包含一些新的功能和改进。例如,它可能为用户提供了更多的构建选项、编译器支持或库依赖等。这些新功能和改进旨在使开发人员能够更轻松地管理和构建他们的项目。 除了功能更新外,CMake 3.22.5还可能包含一些Bug修复。这些修复旨在解决以前版本中发现的问题和错误,以提高CMake在实际使用中的稳定性和可靠性。 总而言之,CMake 3.22.5是CMake构建工具的一个版本,它提供了一些功能更新和Bug修复,以提高软件项目的构建过程效率和稳定性。升级到新版本前,用户应该详细阅读相关文档,并确保其项目能够顺利迁移到新版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值