[关闭]
@jikeytang 2025-02-19T13:10:25.000000Z 字数 4913 阅读 23

Spring Boot 3实现回复评论功能

2025-blog


在Spring Boot 2中实现回复评论的功能与Spring Boot 3的实现非常相似,主要区别在于Spring Boot 2的依赖管理和一些API的细微差异。以下是基于Spring Boot 2的实现步骤和代码示例。


1. 后端实现(Spring Boot 2)

1.1 创建实体类

创建一个Comment实体类来表示评论。

  1. import javax.persistence.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. @Entity
  5. public class Comment {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private Long id;
  9. private String content;
  10. @ManyToOne
  11. @JoinColumn(name = "parent_id")
  12. private Comment parent;
  13. @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
  14. private List<Comment> replies = new ArrayList<>();
  15. // Getters and Setters
  16. public Long getId() {
  17. return id;
  18. }
  19. public void setId(Long id) {
  20. this.id = id;
  21. }
  22. public String getContent() {
  23. return content;
  24. }
  25. public void setContent(String content) {
  26. this.content = content;
  27. }
  28. public Comment getParent() {
  29. return parent;
  30. }
  31. public void setParent(Comment parent) {
  32. this.parent = parent;
  33. }
  34. public List<Comment> getReplies() {
  35. return replies;
  36. }
  37. public void setReplies(List<Comment> replies) {
  38. this.replies = replies;
  39. }
  40. }

1.2 创建Repository

创建一个CommentRepository接口来操作数据库。

  1. import org.springframework.data.jpa.repository.JpaRepository;
  2. import java.util.List;
  3. public interface CommentRepository extends JpaRepository<Comment, Long> {
  4. List<Comment> findByParentIsNull();
  5. }

1.3 创建Service

创建一个CommentService来处理业务逻辑。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Service;
  3. import java.util.List;
  4. @Service
  5. public class CommentService {
  6. @Autowired
  7. private CommentRepository commentRepository;
  8. public List<Comment> getTopLevelComments() {
  9. return commentRepository.findByParentIsNull();
  10. }
  11. public Comment addComment(Comment comment) {
  12. return commentRepository.save(comment);
  13. }
  14. public Comment addReply(Long parentId, Comment reply) {
  15. Comment parent = commentRepository.findById(parentId)
  16. .orElseThrow(() -> new RuntimeException("Parent comment not found"));
  17. reply.setParent(parent);
  18. return commentRepository.save(reply);
  19. }
  20. }

1.4 创建Controller

创建一个CommentController来处理HTTP请求。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.*;
  3. import java.util.List;
  4. @RestController
  5. @RequestMapping("/api/comments")
  6. public class CommentController {
  7. @Autowired
  8. private CommentService commentService;
  9. @GetMapping
  10. public List<Comment> getComments() {
  11. return commentService.getTopLevelComments();
  12. }
  13. @PostMapping
  14. public Comment addComment(@RequestBody Comment comment) {
  15. return commentService.addComment(comment);
  16. }
  17. @PostMapping("/{parentId}/reply")
  18. public Comment addReply(@PathVariable Long parentId, @RequestBody Comment reply) {
  19. return commentService.addReply(parentId, reply);
  20. }
  21. }

2. 前端实现(Vue 3 + Element Plus)

前端的实现与Spring Boot 3的完全一致,因为前端不依赖于后端的具体版本。以下是简要步骤:

2.1 安装Element Plus

  1. npm install element-plus

2.2 创建评论组件

创建Comment.vue组件。

  1. <template>
  2. <div class="comment">
  3. <div class="comment-content">{{ comment.content }}</div>
  4. <el-button type="text" @click="showReplyForm = !showReplyForm">回复</el-button>
  5. <div v-if="showReplyForm">
  6. <el-input v-model="replyContent" placeholder="输入回复内容"></el-input>
  7. <el-button type="primary" @click="submitReply">提交</el-button>
  8. </div>
  9. <div v-if="comment.replies && comment.replies.length > 0" class="replies">
  10. <Comment v-for="reply in comment.replies" :key="reply.id" :comment="reply"></Comment>
  11. </div>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'Comment',
  17. props: {
  18. comment: {
  19. type: Object,
  20. required: true
  21. }
  22. },
  23. data() {
  24. return {
  25. showReplyForm: false,
  26. replyContent: ''
  27. };
  28. },
  29. methods: {
  30. async submitReply() {
  31. const reply = {
  32. content: this.replyContent,
  33. parentId: this.comment.id
  34. };
  35. await this.$http.post(`/api/comments/${this.comment.id}/reply`, reply);
  36. this.showReplyForm = false;
  37. this.replyContent = '';
  38. this.$emit('reload');
  39. }
  40. }
  41. };
  42. </script>
  43. <style scoped>
  44. .comment {
  45. margin-left: 20px;
  46. margin-top: 10px;
  47. }
  48. .replies {
  49. margin-left: 20px;
  50. }
  51. </style>

2.3 创建评论列表组件

创建CommentList.vue组件。

  1. <template>
  2. <div>
  3. <Comment v-for="comment in comments" :key="comment.id" :comment="comment" @reload="loadComments"></Comment>
  4. <el-input v-model="newComment" placeholder="输入新评论"></el-input>
  5. <el-button type="primary" @click="submitComment">提交</el-button>
  6. </div>
  7. </template>
  8. <script>
  9. import Comment from './Comment.vue';
  10. export default {
  11. components: {
  12. Comment
  13. },
  14. data() {
  15. return {
  16. comments: [],
  17. newComment: ''
  18. };
  19. },
  20. async created() {
  21. this.loadComments();
  22. },
  23. methods: {
  24. async loadComments() {
  25. const response = await this.$http.get('/api/comments');
  26. this.comments = response.data;
  27. },
  28. async submitComment() {
  29. const comment = {
  30. content: this.newComment
  31. };
  32. await this.$http.post('/api/comments', comment);
  33. this.newComment = '';
  34. this.loadComments();
  35. }
  36. }
  37. };
  38. </script>

2.4 在App.vue中使用CommentList组件

  1. <template>
  2. <div id="app">
  3. <CommentList></CommentList>
  4. </div>
  5. </template>
  6. <script>
  7. import CommentList from './components/CommentList.vue';
  8. export default {
  9. components: {
  10. CommentList
  11. }
  12. };
  13. </script>

3. 配置HTTP请求

main.js中配置HTTP请求。

  1. import { createApp } from 'vue';
  2. import App from './App.vue';
  3. import ElementPlus from 'element-plus';
  4. import 'element-plus/lib/theme-chalk/index.css';
  5. import axios from 'axios';
  6. import VueAxios from 'vue-axios';
  7. const app = createApp(App);
  8. app.use(ElementPlus);
  9. app.use(VueAxios, axios);
  10. app.mount('#app');

4. 运行项目

确保后端Spring Boot 2应用已经启动,然后运行前端Vue项目。

  1. npm run serve

5. 总结

在Spring Boot 2中实现回复评论的功能与Spring Boot 3的实现几乎一致,主要区别在于Spring Boot 2的依赖管理和一些API的细微差异。前端的实现完全通用,不依赖于后端的具体版本。你可以根据需要进一步扩展和优化这个功能。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注