我们接着上次的代码开发
和之前做其他功能的流程类似,新建comment实体类:
@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;
}
之后新建CommentService接口和CommentServiceImpl实现类。
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
public List<Comment> listCommentByNewId(Long newId) {
Sort sort = Sort.by("createTime");
List<Comment> comments =commentRepository.findByNewsIdAndParentCommentNull(newId,sort);
return eachComment(comments);
}
private List<Comment> eachComment(List<Comment