【中软国际实习】Day 11:Spring Boot:TNews项目实现新闻评论

新闻评论

实体类

@Entity
@Table(name = "t_comment")
public class Comment {
    @Id
    @GeneratedValue
    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() {
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getAvatar() {
        return avatar;
    }
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public List<Comment> getReplyComments() {
        return replyComments;
    }
    public void setReplyComments(List<Comment> replyComments) {
        this.replyComments = replyComments;
    }
    public Comment getParentComment() {
        return parentComment;
    }
    public void setParentComment(Comment parentComment) {
        this.parentComment = parentComment;
    }
    public boolean isAdminComment() {
        return adminComment;
    }
    public void setAdminComment(boolean adminComment) {
        this.adminComment = adminComment;
    }
    public News getNews() {
        return news;
    }
    public void setNews(News news) {
        this.news = news;
    }
    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", nickname='" + nickname + '\'' +
                ", email='" + email + '\'' +
                ", content='" + content + '\'' +
                ", avatar='" + avatar + '\'' +
                ", createTime=" + createTime +
                ", news=" + news +
                ", replyComments=" + replyComments +
                ", parentComment=" + parentComment +
                ", adminComment=" + adminComment +
                '}';
    }
}

Controller层

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;
    @PostMapping("/comments")
    public String save(Comment comment, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            comment.setAdminComment(false);
        }else{
            comment.setAdminComment(true);
        }
        commentService.save(comment);
        Long newsId = comment.getNews().getId();
        return "redirect:/comments/"+newsId;
    }

    @RequestMapping("/comments/{newsId}")
    public String comments(@PathVariable Long newsId, Model model){
        List<Comment> comments = commentService.findCommentByNewsId(newsId);
        model.addAttribute("comments",comments);
        return "news::commentList";
    }
}

Service层

save()方法中:如果父评论id为-1(没有父评论),则将父评论id设为null。
通过消息的id来查找所有评论。

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Override
    public void save(Comment comment) {
        if(comment.getParentComment().getId()==-1){
            comment.setParentComment(null);
        }
        commentDao.save(comment);
    }
    @Override
    public List<Comment> findCommentByNewsId(Long newsId) {
        Sort sort = Sort.by("createTime");
        List<Comment> comments = commentDao.findByNewsIdAndParentCommentNull(newsId,sort);
        return comments;
    }
}

前端界面

<div id="comment-form" class="ui form">
  <input type="hidden" name="news.id" th:value="${news.id}">
  <input type="hidden" name="parentComment.id" value="-1">
  <div class="field">
    <textarea name="content" placeholder="请输入评论信息..."></textarea>
  </div>
  <div class="fields">
    <div class="field m-mobile-wide m-margin-bottom-small">
      <div class="ui left icon input">
        <i class="user icon"></i>
        <input type="text" name="nickname" placeholder="姓名" th:value="${session.user}!=null ? ${session.user.nickname}">
      </div>
    </div>
    <div class="field m-mobile-wide m-margin-bottom-small">
      <div class="ui left icon input">
        <i class="mail icon"></i>
        <input type="text" name="email" placeholder="邮箱" th:value="${session.user}!=null ? ${session.user.email}">
      </div>
    </div>
    <div class="field  m-margin-bottom-small m-mobile-wide">
      <button id="commentpost-btn" type="button" class="ui teal button m-mobile-wide"><i class="edit icon"></i>发布</button>
    </div>
</div>

在这里插入图片描述

小结

新闻评论功能实现后,TNews项目的基础功能已经完成,在迭代过程中,我能够清晰地感受到Spring Boot项目相较于SSM框架的简洁与方便,代码精简,框架自动生成,为Java项目开发带来了许多便利。在迭代的过程中,我已经基本熟悉了Spring Boot框架的原理与实现,收获满满。

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页