个人博客系统开发简要分为五大模块
-
首页
-
登录功能
-
评论管理
-
文章管理
-
邮件管理
一个完善的博客系统,前端页面可以展示、发布评论,评论用户可以互相回复、后台可以管理评论内容。由于篇幅有限,本项目的文章评论会实现前端评论展示、发布评论等功能。
本期给大家介绍的是评论管理模块
1.业务处理层实现
实现查看文章详情时,评论类Comments 操作数据库的Mapper 接口文件已经编写好了,这里直接从Service业务层处理评论管理的相关业务。
(1)编写Service层接口方法
在评论业务接口文件ICommentService中编写一个发布文章评论的方法,示例代码如下。
// 用户发表评论
public void pushComment(Comment comment) {
(2)编写Service 层接口实现类方法
在评论业务层接口实现类CommentServicelmpl中实现新增的评论发布方法,示例代码如下。
// 用户发表评论
@Override
public void pushComment(Comment comment) {
commentMapper.pushComment(comment);
// 更新文章评论数据量
Statistic statistic = statisticMapper.selectStatisticWithArticleId(comment.getArticleId());
statistic.setCommentsNum(statistic.getCommentsNum() + 1);
statisticMapper.updateArticleCommentsWithId(statistic);
}
上述代码中,在CommentServicelmpl的评论发布方法中,先进行了评论数据发布操作,然
周用统计类接口文件statisticMapper的相关方法对文章评论信息数量进行了统计更新。
2.请求处理层实现
2.1在com.itheima.web.client包下创建一个用户评论管理的控制类CommentController,并编
相应的请求控制方法,内容如下所示。
package com.itheima.web.client;
import com.itheima.model.ResponseData.ArticleResponseData;
import com.itheima.model.domain.Comment;
import com.itheima.service.ICommentService;
import com.itheima.utils.MyUtils;
import com.vdurmont.emoji.EmojiParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* @version 1.0
* @Author LDX
* @Date 2023/11/3 17:02
* @注释
*/
@Controller
@RequestMapping("/comments")
public class CommentController {
private static final Logger logger = LoggerFactory.getLogger(CommentController.class);
@Autowired
private ICommentService commentServcieImpl;
// 发表评论操作
@PostMapping(value = "/publish")
@ResponseBody
public ArticleResponseData publishComment(HttpServletRequest request, @RequestParam Integer aid, @RequestParam String text) {
// 去除js脚本
text = MyUt