您的位置:首页 > 编程语言 > Java开发

spring boot新闻管理项目——评论功能和按标签、类型显示新闻

2020-07-31 19:13 429 查看

本次实现了新闻页面的评论功能,能够对新闻进行评论和评论互动。同时也实现了点击新闻列表首页的分类和标签,可按分类和标签显示新闻的功能。

本次设计的难点主要集中在评论的展示功能,需将所有子评论展示到一个最初的父评论下,实现利用了递归思想。本次以评论功能的实现为例。

  1. 设计

    po.Comment
    类,并在
    po.News
    类中添加相应的
    Comment
    类,并更新
    Setter
    Getter
    toString
    方法

    @Entity
    @Table(name = "t_comment")
    public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nickname;
    private String email;
    private String content;
    private String avatar;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @ManyToOne
    private News news;
    @OneToMany(mappedBy = "parentComment")
    private List<Comment> replyComments = new ArrayList<>();
    @ManyToOne
    private Comment parentComment;
    private boolean adminComment;
    
    public Comment() {
    }
    }

    添加

    List<Comment>
    数据

    @OneToMany(mappedBy = "news")
    private List<Comment> commentList = new ArrayList<>();
  2. 创建

    dao/CommentRepository.java
    类,进行数据库操作

    public interface CommentRepository extends JpaRepository<Comment, Long> {
    
    List<Comment> findByNewsIdAndParentCommentNull(Long newsId, Sort sort);
    }
  3. 创建

    service.impl.CommentServiceImpl.java
    类,提供保存评论和展示评论列表的任务

    @Service
    public class CommentServiceImpl implements CommentService {
    
    @Autowired
    private CommentRepository commentRepository;
    
    //提供展示评论的服务
    @Override
    public List<Comment> listCommentByNewsId(Long newsId) {
    Sort sort = Sort.by("createTime");
    List<Comment> comments = commentRepository.findByNewsIdAndParentCommentNull(newsId, sort);
    return eachComment(comments);
    }
    
    //遍历每条无父评论的评论
    private List<Comment> eachComment(List<Comment> comments) {
    List<Comment> commentsView = new ArrayList<>();
    for (Comment comment : comments) {
    Comment c = new Comment();
    BeanUtils.copyProperties(comment, c);
    commentsView.add(c);
    }
    //合并评论的各层子代到第一级子代集合中
    combineChildren(commentsView);
    return commentsView;
    }
    
    private void combineChildren(List<Comment> comments) {
    for (Comment comment : comments) {
    List<Comment> replys1 = comment.getReplyComments();
    for (Comment reply1 : replys1) {
    //循环迭代,找出子代,存放在临时tempReplys中
    recursively(reply1);
    }
    //将所有子孙评论放入一级子评论中
    comment.setReplyComments(tempReplys);
    //清除临时存放区
    tempReplys = new ArrayList<>();
    }
    }
    
    //定义临时存放区
    private List<Comment> tempReplys = new ArrayList<>();
    
    //递归将所有子评论存入临时存放区
    private void recursively(Comment comment) {
    tempReplys.add(comment);    //顶节点添加到临时存放区
    if (comment.getReplyComments().size() > 0) {
    List<Comment> replys = comment.getReplyComments();
    for (Comment reply : replys) {
    tempReplys.add(reply);
    if (reply.getReplyComments().size() > 0) {
    recursively(reply);
    }
    }
    }
    }
    
    //保存评论
    @Override
    public Comment saveComment(Comment comment) {
    Long parentCommentId = comment.getParentComment().getId();
    if (parentCommentId != -1) {
    comment.setParentComment(commentRepository.findById(parentCommentId).orElse(null));
    } else {
    comment.setParentComment(null);
    }
    comment.setCreateTime(new Date());
    return commentRepository.save(comment);
    }
    }
  4. 创建

    com.llanero.news.web.CommentController.java
    类,通过
    Service
    包中提供的对象方法响应web端页面的请求

    @Controller
    public class CommentController {
    @Autowired
    private CommentService commentService;
    
    @Autowired
    private NewsService newsService;
    
    private String avatar = "https://gitee.com/llanero/share/raw/master/img/20200727182846.jpeg";
    
    @GetMapping("/comments/{newId}")
    public String comments(@PathVariable Long newId, Model model) {
    model.addAttribute("comments", commentService.listCommentByNewsId(newId));
    return "new::commentList";
    }
    
    @PostMapping("/comments")
    private String post(Comment comment, HttpSession session) {
    Long newsId = comment.getNews().getId();
    comment.setNews(newsService.getNews(newsId));
    User user = (User) session.getAttribute("user");
    if (user != null) {
    comment.setAdminComment(true);
    comment.setAvatar(avatar);
    } else {
    comment.setAvatar(avatar);
    }
    commentService.saveComment(comment);
    return "redirect:/comments/" + newsId;
    }
    }
  5. 分类展示介绍

    创建

    web.TypeShowController
    类,进行分类展示新闻

    @Autowired
    private TypeService typeService;
    
    @Autowired
    private NewsService newsService;
    
    @GetMapping("/types/{id}")
    public String types(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
    @PathVariable Long id, Model model) {
    List<Type> types = typeService.listTypeTop(20);
    if (id == -1) {
    id = types.get(0).getId();
    }
    NewsQuery newsQuery = new NewsQuery();
    newsQuery.setTypeId(id);
    model.addAttribute("types", types);
    model.addAttribute("page", newsService.listNews(pageable, newsQuery));
    model.addAttribute("activeTypeId", id);
    return "types";
    }
  6. 项目运行测试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: