仿牛客社区——3.22添加评论

该文章描述了一个Web应用中实现用户给帖子添加评论的流程,包括Controller层处理请求,Service层处理业务逻辑,如事务管理确保评论添加和更新帖子评论数的一致性,以及DAO层的数据库操作。同时,涉及HTML表单提交和敏感信息过滤,保证用户交互的安全性。
摘要由CSDN通过智能技术生成

实现功能

用户能够给帖子回复

controller

- 处理添加评论数据的请求。

- 设置添加评论的表单。

@Controller
@RequestMapping("/comment")
public class CommentController {
	@Autowired
	private HostHolder hostHolder;
	@Autowired
	private CommentService commentService;

	@RequestMapping(value = "/add/{discussPostId}",method = RequestMethod.POST)
	public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
		comment.setStatus(0);
		comment.setCreateTime(new Date());
		comment.setUserId(hostHolder.getUser().getId());
		commentService.addComment(comment);

		return "redirect:/discuss/detail/" + discussPostId;

	}
}

service

- 处理添加评论的业务: 先增加评论、再更新帖子的评论数量。

@Service
public class CommentService implements CommunityConstant {
	@Autowired
	private CommentMapper commentMapper;

	@Autowired
	private SensitiveFilter sensitiveFilter;

	@Autowired
	private DiscussPostMapper discussPostMapper;

	//先添加帖子评论,再添加评论数量(开启事务:要么都做,要么都不做)
	@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);

		//更新帖子评论数量
		//判断评论帖子类型
		if(comment.getEntityType()==ENTITY_TYPE_POST){//ENTITY_TYPE_POST:1
			int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
			//entityId:给哪个帖子的评论
			discussPostMapper.updateCommentCount(comment.getEntityId(),count);
		}

		return rows;
	}


}

dao

- 增加评论数据。

//插入评论
	int insertComment(Comment comment);

mapper.xml

<!--提取插入字段-->
    <sql id="insertFields">
        user_id,entity_type,entity_id,target_id,content,status,create_time
    </sql>

<!-- int insertComment(Comment comment); -->
    <insert id="insertComment" parameterType="Comment">
        insert into comment(<include refid="insertFields"></include>)
        values(#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})
    </insert>

- 修改帖子的评论数量。

 //增加帖子评论数量
    int updateCommentCount(int id,int commentCount);

mapper.xml

 <!-- int updateCommentCount(int id,int commentCount); -->
    <update id="updateCommentCount" >
        update discuss_post
        set comment_count=#{commentCount}
        where id=#{id}
    </update>

html

	<!-- 回复列表 -->
							<ul class="list-unstyled mt-4 bg-gray p-3 font-size-12 text-muted">

								<li class="pb-3 pt-3 mb-3 border-bottom" th:each="rvo:${cvo.replys}">
									<div>
										<span th:if="${rvo.target==null}">
											<b class="text-info" th:text="${rvo.user.userName}">寒江雪</b>:&nbsp;&nbsp;
										</span>
										<span th:if="${rvo.target!=null}">
											<i class="text-info" th:text="${rvo.user.userName}">Sissi</i>回复
											<b class="text-info" th:text="${rvo.target.userName}">寒江雪</b>:&nbsp;&nbsp;
										</span>
										<span th:utext="${rvo.reply.content}">这个是直播时间哈,觉得晚的话可以直接看之前的完整录播的~</span>
									</div>
									<div class="mt-3">
										<span  th:text="${#dates.format(rvo.reply.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</span>
										<ul class="d-inline float-right">
											<li class="d-inline ml-2"><a href="#" class="text-primary">赞(1)</a></li>
											<li class="d-inline ml-2">|</li>
											<li class="d-inline ml-2"><a th:href="|#huifu-${rvoStat.count}|" data-toggle="collapse" class="text-primary">回复</a></li>
										</ul>
										<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="#">&nbsp;&nbsp;回&nbsp;&nbsp;复&nbsp;&nbsp;</button>
												</div>
											</form>

										</div>
									</div>								
								</li>

								<!-- 回复输入框 -->
								<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="#">&nbsp;&nbsp;回&nbsp;&nbsp;复&nbsp;&nbsp;</button>
										</div>
									</form>

								</li>
							</ul>
<!-- 回帖输入 -->
			<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">&nbsp;&nbsp;回&nbsp;&nbsp;帖&nbsp;&nbsp;</button>
					</p>
				</form>
			</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值