在Spring Boot框架下的web评论系统,实体有评论、子评论、用户和回复,实现检索、分页和排序功能。
时间: 2023-06-18 20:08:30 浏览: 158
首先,你需要定义实体类,如下所示:
```java
@Entity
@Table(name = "comment")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
private LocalDateTime createdTime;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@OneToMany(mappedBy = "comment", cascade = CascadeType.ALL)
private List<Reply> replies;
// getters and setters
}
@Entity
@Table(name = "reply")
public class Reply {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
private LocalDateTime createdTime;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "comment_id")
private Comment comment;
// getters and setters
}
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
// getters and setters
}
```
然后,你需要创建一个DAO层,使用Spring Data JPA实现对实体的增删改查操作,如下所示:
```java
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
Page<Comment> findAllByOrderByIdDesc(Pageable pageable);
Page<Comment> findByContentContainingIgnoreCaseOrderByIdDesc(String keyword, Pageable pageable);
}
@Repository
public interface ReplyRepository extends JpaRepository<Reply, Long> {
Page<Reply> findAllByCommentIdOrderByIdDesc(Long commentId, Pageable pageable);
}
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
最后,你需要创建一个Service层,实现对DAO层的调用和业务逻辑处理,如下所示:
```java
@Service
public class CommentService {
@Autowired
private CommentRepository commentRepository;
@Autowired
private ReplyRepository replyRepository;
public Page<Comment> findComments(Integer pageNo, Integer pageSize, String keyword) {
Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
if (StringUtils.isEmpty(keyword)) {
return commentRepository.findAllByOrderByIdDesc(pageable);
} else {
return commentRepository.findByContentContainingIgnoreCaseOrderByIdDesc(keyword, pageable);
}
}
public Page<Reply> findReplies(Long commentId, Integer pageNo, Integer pageSize) {
Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
return replyRepository.findAllByCommentIdOrderByIdDesc(commentId, pageable);
}
public Comment saveComment(Comment comment) {
comment.setCreatedTime(LocalDateTime.now());
return commentRepository.save(comment);
}
public Reply saveReply(Reply reply) {
reply.setCreatedTime(LocalDateTime.now());
return replyRepository.save(reply);
}
}
```
最后,在控制器中调用Service层方法,实现路由和响应数据的处理,如下所示:
```java
@RestController
@RequestMapping("/api")
public class CommentController {
@Autowired
private CommentService commentService;
@GetMapping("/comments")
public Page<Comment> findComments(@RequestParam(defaultValue = "1") Integer pageNo,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String keyword) {
return commentService.findComments(pageNo, pageSize, keyword);
}
@GetMapping("/comments/{commentId}/replies")
public Page<Reply> findReplies(@PathVariable Long commentId,
@RequestParam(defaultValue = "1") Integer pageNo,
@RequestParam(defaultValue = "10") Integer pageSize) {
return commentService.findReplies(commentId, pageNo, pageSize);
}
@PostMapping("/comments")
public Comment saveComment(@RequestBody Comment comment) {
return commentService.saveComment(comment);
}
@PostMapping("/replies")
public Reply saveReply(@RequestBody Reply reply) {
return commentService.saveReply(reply);
}
}
```
以上就是一个简单的评论系统的实现,其中包括了检索、分页和排序功能。
阅读全文