基于springboot的论坛系统

创建三张表:帖子表:post 评论表:comment 贴子的热度表:post_hot 评论表: post表: 帖子热度表:

@ApiOperation("根据帖子ID查询评论信息")
    @GetMapping("/detailComment/{postId}")
    public ApiResult datailComment(@ApiParam(value = "帖子id",required = true)@PathVariable("postId")Integer postId,
                                   @ApiParam(value = "页码",required = true)@RequestParam("pageNum") Integer pageNum,
                                   @ApiParam(value = "页码",required = true)@RequestParam("pageSize")Integer pageSize){
          
   
        return commentService.datailComment(postId,pageNum,pageSize);
    }
@Override
    public ApiResult datailComment(Integer postId,Integer pageNum,Integer pageSize) {
          
   
        PageHelper.startPage(pageNum,pageSize);
        //查询一级评论
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("post_id",postId).eq("parent_id",0).eq("is_delete",0).orderByDesc("comment_time");
        List<CommentVO> commentVOList = list(qw).stream().map(CommentVO::of).collect(Collectors.toList());
        //查询二级评论信息
        commentVOList.forEach(item->{
          
   
            qw.clear();
            qw.eq("parent_id",item.getId()).eq("post_id",postId)
              .eq("is_delete",0).orderByDesc("comment_time");
            item.setCommentVOList(list(qw).stream().map(CommentVO::of).collect(Collectors.toList()));
        });
        PageInfo<CommentVO> pageInfo = new PageInfo<>(commentVOList);
        return ApiResult.success(pageInfo);
    }
@Data
@Builder
public class CommentVO {
          
   
    @ApiModelProperty("id")
    private Integer id;
    @ApiModelProperty("用户id")
    private Integer userId;
    @ApiModelProperty("帖子id")
    private Integer postId;
    @ApiModelProperty("父评论的用户id")
    private Integer parentUserId;
    @ApiModelProperty("父评论id")
    private Integer parentId;
    @ApiModelProperty("评论内容")
    private String content;
    @ApiModelProperty("评论时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date commentTime;
    @ApiModelProperty("所有的二级评论")
    private List<CommentVO> commentVOList;

    public static CommentVO of(Comment comment){
          
   
        CommentVO commentVO = CommentVO.builder().build();
        BeanUtils.copyProperties(comment,commentVO);
        return commentVO;
    }
}//注意了CommentVO不能直接new对象。

具体代码在:https://gitee.com/yzq1831914/forum-system.git

经验分享 程序员 微信小程序 职场和发展