这篇文章主要讲讲帖子详情功能。其实帖子详情功能简单来说就是你点进去可以看到文章,这就叫帖子详情功能。那接下来我讲讲我的这个项目是如何实现这个功能的。

首先写DAO层。
- @Mapper
- public interface DiscussPostMapper {
-
- List
selectDiscussPosts(int userId, int offset, int limit, int orderMode); - //查询讨论帖子列表的方法,根据给定的用户ID、偏移量、限制数量和排序方式来查询讨论帖子
-
- int selectDiscussPostRows(@Param("userId") int userId);
- //查询讨论帖子的总行数的方法,根据给定的用户ID来查询该用户发表的讨论帖子总数
-
- int insertDiscussPost(DiscussPost discussPost);
- //插入一条讨论帖子记录的方法,将给定的讨论帖子对象插入到数据库中
-
- DiscussPost selectDiscussPostById(int id);
- //根据给定的帖子ID查询讨论帖子的方法,返回对应ID的讨论帖子对象
-
- int updateCommentCount(int id, int commentCount);
- //更新讨论帖子的评论数量的方法,根据给定的帖子ID更新评论数量
-
- int updateType(int id, int type);
- //更新讨论帖子的类型的方法,根据给定的帖子ID更新类型
-
- int updateStatus(int id, int status);
- //更新讨论帖子的状态的方法,根据给定的帖子ID更新状态
-
- int updateScore(int id, double score);
- //更新讨论帖子的评分的方法,根据给定的帖子ID更新评分
-
- }
再来写Service层。
- @PostConstruct
- public List
findDiscussPosts(int userId, int offset, int limit, int orderMode) { - if (userId == 0 && orderMode == 1) {
- return postListCache.get(offset + ":" + limit);
- }
-
- logger.debug("load post list from DB.");
- return discussPostMapper.selectDiscussPosts(userId, offset, limit, orderMode);
- }
-
- public int findDiscussPostRows(int userId) {
- if (userId == 0) {
- return postRowsCache.get(userId);
- }
-
- logger.debug("load post rows from DB.");
- return discussPostMapper.selectDiscussPostRows(userId);
- }
-
- public int addDiscussPost(DiscussPost post) {
- if (post == null) {
- throw new IllegalArgumentException("参数不能为空!");
- }
-
- // 转义HTML标记
- post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
- post.setContent(HtmlUtils.htmlEscape(post.getContent()));
- // 过滤敏感词
- post.setTitle(sensitiveFilter.filter(post.getTitle()));
- post.setContent(sensitiveFilter.filter(post.getContent()));
-
- return discussPostMapper.insertDiscussPost(post);
- }
-
- public DiscussPost findDiscussPostById(int id) {
- return discussPostMapper.selectDiscussPostById(id);
- }
-
- public int updateCommentCount(int id, int commentCount) {
- return discussPostMapper.updateCommentCount(id, commentCount);
- }
-
- public int updateType(int id, int type) {
- return discussPostMapper.updateType(id, type);
- }
-
- public int updateStatus(int id, int status) {
- return discussPostMapper.updateStatus(id, status);
- }
-
- public int updateScore(int id, double score) {
- return discussPostMapper.updateScore(id, score);
- }
service层的代码是一个帖子(DiscussPost)的服务类,提供了一些操作帖子的方法,并使用了缓存来提高性能。这段代码有点长,所以读起来有点费力,我讲一下我对这些代码的理解吧。
findDiscussPosts() 方法用于查询帖子列表。如果传入的用户ID为0且排序方式为1(orderMode == 1),则直接从缓存中获取对应的结果;否则,调用数据库查询方法从数据库中获取结果。findDiscussPostRows() 方法用于查询帖子总数。如果传入的用户ID为0,则直接从缓存中获取对应的结果;否则,调用数据库查询方法从数据库中获取结果。addDiscussPost() 方法用于添加帖子。在添加之前,会对帖子的标题和内容进行转义HTML标记和敏感词过滤的处理,然后调用数据库操作方法将帖子插入到数据库中,并返回插入的结果。findDiscussPostById()、updateCommentCount()、updateType()、updateStatus()、updateScore() 都是调用数据库操作方法来更新或查询帖子的相关信息。service层大概就是这些,虽然说比较长,但是还是很容易懂的,controller层更加长。
最后写controller层。
- @Controller
- @RequestMapping("/discuss")
- public class DiscussPostController implements CommunityConstant {
-
- @RequestMapping(path = "/add", method = RequestMethod.POST)
- @ResponseBody
- public String addDiscussPost(String title, String content) {
- User user = hostHolder.getUser();
- if (user == null) {
- return CommunityUtil.getJSONString(403, "你还没有登录哦!");
- }
-
- DiscussPost post = new DiscussPost();
- post.setUserId(user.getId());
- post.setTitle(title);
- post.setContent(content);
- post.setCreateTime(new Date());
- discussPostService.addDiscussPost(post);
-
- // 触发发帖事件
- Event event = new Event()
- .setTopic(TOPIC_PUBLISH)
- .setUserId(user.getId())
- .setEntityType(ENTITY_TYPE_POST)
- .setEntityId(post.getId());
- eventProducer.fireEvent(event);
-
- // 计算帖子分数
- String redisKey = RedisKeyUtil.getPostScoreKey();
- redisTemplate.opsForSet().add(redisKey, post.getId());
-
- // 报错的情况,将来统一处理.
- return CommunityUtil.getJSONString(0, "发布成功!");
- }
-
- @RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)
- public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {
- // 帖子
- DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
- model.addAttribute("post", post);
- // 作者
- User user = userService.findUserById(post.getUserId());
- model.addAttribute("user", user);
- // 点赞数量
- long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId);
- model.addAttribute("likeCount", likeCount);
- // 点赞状态
- int likeStatus = hostHolder.getUser() == null ? 0 :
- likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, discussPostId);
- model.addAttribute("likeStatus", likeStatus);
-
- // 评论分页信息
- page.setLimit(5);
- page.setPath("/discuss/detail/" + discussPostId);
- page.setRows(post.getCommentCount());
-
- // 评论: 给帖子的评论
- // 回复: 给评论的评论
- // 评论列表
- List
commentList = commentService.findCommentsByEntity( - ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
- // 评论VO列表
- List
- if (commentList != null) {
- for (Comment comment : commentList) {
- // 评论VO
- Map
commentVo = new HashMap<>(); - // 评论
- commentVo.put("comment", comment);
- // 作者
- commentVo.put("user", userService.findUserById(comment.getUserId()));
- // 点赞数量
- likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, comment.getId());
- commentVo.put("likeCount", likeCount);
- // 点赞状态
- likeStatus = hostHolder.getUser() == null ? 0 :
- likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId());
- commentVo.put("likeStatus", likeStatus);
-
- // 回复列表
- List
replyList = commentService.findCommentsByEntity( - ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
- // 回复VO列表
- List
- if (replyList != null) {
- for (Comment reply : replyList) {
- Map
replyVo = new HashMap<>(); - // 回复
- replyVo.put("reply", reply);
- // 作者
- replyVo.put("user", userService.findUserById(reply.getUserId()));
- // 回复目标
- User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());
- replyVo.put("target", target);
- // 点赞数量
- likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, reply.getId());
- replyVo.put("likeCount", likeCount);
- // 点赞状态
- likeStatus = hostHolder.getUser() == null ? 0 :
- likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId());
- replyVo.put("likeStatus", likeStatus);
-
- replyVoList.add(replyVo);
- }
- }
- commentVo.put("replys", replyVoList);
-
- // 回复数量
- int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());
- commentVo.put("replyCount", replyCount);
-
- commentVoList.add(commentVo);
- }
- }
-
- model.addAttribute("comments", commentVoList);
-
- return "/site/discuss-detail";
- }
-
- // 置顶
- @RequestMapping(path = "/top", method = RequestMethod.POST)
- @ResponseBody
- public String setTop(int id) {
- discussPostService.updateType(id, 1);
-
- // 触发发帖事件
- Event event = new Event()
- .setTopic(TOPIC_PUBLISH)
- .setUserId(hostHolder.getUser().getId())
- .setEntityType(ENTITY_TYPE_POST)
- .setEntityId(id);
- eventProducer.fireEvent(event);
-
- return CommunityUtil.getJSONString(0);
- }
-
- // 加精
- @RequestMapping(path = "/wonderful", method = RequestMethod.POST)
- @ResponseBody
- public String setWonderful(int id) {
- discussPostService.updateStatus(id, 1);
-
- // 触发发帖事件
- Event event = new Event()
- .setTopic(TOPIC_PUBLISH)
- .setUserId(hostHolder.getUser().getId())
- .setEntityType(ENTITY_TYPE_POST)
- .setEntityId(id);
- eventProducer.fireEvent(event);
-
- // 计算帖子分数
- String redisKey = RedisKeyUtil.getPostScoreKey();
- redisTemplate.opsForSet().add(redisKey, id);
-
- return CommunityUtil.getJSONString(0);
- }
-
- // 删除
- @RequestMapping(path = "/delete", method = RequestMethod.POST)
- @ResponseBody
- public String setDelete(int id) {
- discussPostService.updateStatus(id, 2);
-
- // 触发删帖事件
- Event event = new Event()
- .setTopic(TOPIC_DELETE)
- .setUserId(hostHolder.getUser().getId())
- .setEntityType(ENTITY_TYPE_POST)
- .setEntityId(id);
- eventProducer.fireEvent(event);
-
- return CommunityUtil.getJSONString(0);
- }
-
- }
这段代码是一个控制器类,处理与帖子相关的请求。
首先是 addDiscussPost() 方法,用于处理发布帖子的请求。在方法中首先判断用户是否登录,如果未登录则返回相应的提示信息。然后根据传入的参数创建一个 DiscussPost 对象,并设置相应的属性。接下来调用 discussPostService.addDiscussPost(post) 方法将帖子插入到数据库中。之后触发一个发帖事件,并计算帖子的分数。最后返回一个 JSON 格式的发布成功信息。
接下来是 getDiscussPost() 方法,用于获取帖子详情的请求。首先根据传入的帖子ID调用 discussPostService.findDiscussPostById(discussPostId) 方法获取帖子对象,并将帖子对象添加到 Model 中。然后获取帖子的作者、点赞数量和点赞状态,并将它们添加到 Model 中。接着设置评论的分页信息,并调用 commentService.findCommentsByEntity() 方法获取帖子的评论列表。通过遍历评论列表,将每个评论及其相关信息封装成一个评论VO(Map
接下来是 setTop() 方法,用于置顶帖子的请求。在方法中调用 discussPostService.updateType(id, 1) 方法将帖子的类型设置为置顶。然后触发一个发帖事件,并返回一个 JSON 格式的成功信息。
最后是 setDelete() 方法,用于删除帖子的请求。在方法中调用 discussPostService.updateStatus(id, 2) 方法将帖子的状态设置为删除。然后触发一个删帖事件,并返回一个 JSON 格式的成功信息。
其实对于帖子详情这里并不算太重要,我觉得好好看看,知道有这么一回事儿就行,不需要刻意的去背,背的话其实也用不到,但是你确实需要知道这么一回事儿。