SpringBoot項目實現評論功能、分類頁面與標簽頁面


1、評論功能

①、創建Comment實體類,其中包括ID,nickname等元素

    @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;

②、在News實體中添加List<Comment>元素,並將其注解為@OneToMany(mappedBy = "news")

③、創建CommentService接口與CommentRepository接口

public interface CommentService {
    List<Comment> listCommentByNewId(Long newId);
    Comment saveComment(Comment comment);
}
public interface CommentRepository extends JpaRepository<Comment,Long> {
    List<Comment> findByNewsIdAndParentCommentNull(Long newId, Sort sort);
}

④、繼承這個接口並進行實現;CommentServiceImpl中還應該實現展示,合並評論等功能函數

     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);

    }
    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);
            //清除temp
            tempReplys=new ArrayList<>();
        }
    }

⑤、創建CommentController類,用於實現前后端數據交互;以保存操作為例

    @PostMapping("/comments")
    private String post(Comment comment, HttpSession session){
        Long newId=comment.getNews().getId();
        comment.setNews(newService.getNew(newId));
        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/"+newId;
    }

2、分類與標簽頁面

以TagShowController控制類為例,Type的相應控制類類似

@Controller
public class TagShowController {
    @Autowired
    private NewService newService;
    @Autowired
    private TagService tagService;
    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size=8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                       @PathVariable Long id, Model model){
        List<Tag> tags=tagService.listTagTop(20);
        if(id==-1){
            id=tags.get(0).getId();
        }
        NewQuery newQuery=new NewQuery();
        newQuery.setTypeId(id);
        model.addAttribute("tags",tags);
        model.addAttribute("page",newService.listNew(id,pageable));
        model.addAttribute("activeTagId",id);
        return "tags";
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM