当前位置:   article > 正文

【Spring Boot】Spring Boot实现完整论坛功能示例代码_论坛功能怎么实现

论坛功能怎么实现

以下是一个简单的Spring Boot论坛系统示例代码:

  1. 首先是数据库设计,我们创建以下几张表:
  • user表,存储用户信息,包括id、username、password、email、create_time等字段。
  • topic表,存储帖子信息,包括id、title、content、user_id、create_time等字段。
  • comment表,存储评论信息,包括id、content、user_id、topic_id、create_time等字段。
  1. 创建实体类

2.1 User实体类

  1. @Entity
  2. @Table(name = "user")
  3. public class User {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Long id;
  7. @Column
  8. private String username;
  9. @Column
  10. private String password;
  11. @Column
  12. private String email;
  13. @Column(name = "create_time")
  14. private Date createTime;
  15. // getter/setter省略
  16. }

2.2 Topic实体类:

  1. @Entity
  2. @Table(name = "topic")
  3. public class Topic {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Long id;
  7. @Column
  8. private String title;
  9. @Column
  10. private String content;
  11. @ManyToOne
  12. @JoinColumn(name = "user_id")
  13. private User user;
  14. @Column(name = "create_time")
  15. private Date createTime;
  16. // getter/setter省略
  17. }

2.3 Comment实体类:

  1. @Entity
  2. @Table(name = "comment")
  3. public class Comment {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Long id;
  7. @Column
  8. private String content;
  9. @ManyToOne
  10. @JoinColumn(name = "user_id")
  11. private User user;
  12. @ManyToOne
  13. @JoinColumn(name = "topic_id")
  14. private Topic topic;
  15. @Column(name = "create_time")
  16. private Date createTime;
  17. // getter/setter省略
  18. }
  1. 创建Repository

3.1 UserRepository:

  1. public interface UserRepository extends JpaRepository<User, Long> {
  2. User findByUsername(String username);
  3. }

3.2 TopicRepository:

  1. public interface TopicRepository extends JpaRepository<Topic, Long> {
  2. }

3.3 CommentRepository:

  1. public interface CommentRepository extends JpaRepository<Comment, Long> {
  2. }
  1. 创建Service

4.1 UserService:

  1. @Service
  2. public class UserService {
  3. @Autowired
  4. private UserRepository userRepository;
  5. public boolean checkUser(User user) {
  6. User userInDb = userRepository.findByUsername(user.getUsername());
  7. if (userInDb != null && userInDb.getPassword().equals(user.getPassword())) {
  8. return true;
  9. } else {
  10. return false;
  11. }
  12. }
  13. public void saveUser(User user) {
  14. user.setCreateTime(new Date());
  15. userRepository.save(user);
  16. }
  17. public User getUserById(Long id) {
  18. return userRepository.findById(id).orElse(null);
  19. }
  20. }

4.2 TopicService:

  1. @Service
  2. public class TopicService {
  3. @Autowired
  4. private TopicRepository topicRepository;
  5. public List<Topic> getAllTopics() {
  6. return topicRepository.findAll();
  7. }
  8. public Topic getTopicById(Long id) {
  9. return topicRepository.findById(id).orElse(null);
  10. }
  11. public void saveTopic(Topic topic) {
  12. topic.setCreateTime(new Date());
  13. topicRepository.save(topic);
  14. }
  15. }

4.3 CommentService:

  1. @Service
  2. public class CommentService {
  3. @Autowired
  4. private CommentRepository commentRepository;
  5. public void saveComment(Comment comment) {
  6. comment.setCreateTime(new Date());
  7. commentRepository.save(comment);
  8. }
  9. public List<Comment> getCommentsByTopicId(Long topicId) {
  10. return commentRepository.findAllByTopicId(topicId);
  11. }
  12. }
  1. 创建Controller

5.1 UserController:

  1. @Controller
  2. public class UserController {
  3. @Autowired
  4. private UserService userService;
  5. @GetMapping("/login")
  6. public String login() {
  7. return "login";
  8. }
  9. @PostMapping("/login")
  10. public String doLogin(User user, HttpSession session) {
  11. boolean result = userService.checkUser(user);
  12. if (result) {
  13. session.setAttribute("user", user);
  14. return "redirect:/";
  15. } else {
  16. return "login";
  17. }
  18. }
  19. @GetMapping("/logout")
  20. public String logout(HttpSession session) {
  21. session.removeAttribute("user");
  22. return "redirect:/";
  23. }
  24. @GetMapping("/register")
  25. public String register() {
  26. return "register";
  27. }
  28. @PostMapping("/register")
  29. public String doRegister(User user) {
  30. userService.saveUser(user);
  31. return "redirect:/login";
  32. }
  33. }

5.2 TopicController:

  1. @Controller
  2. public class TopicController {
  3. @Autowired
  4. private TopicService topicService;
  5. @Autowired
  6. private CommentService commentService;
  7. @GetMapping("/")
  8. public String index(Model model) {
  9. List<Topic> topics = topicService.getAllTopics();
  10. model.addAttribute("topics", topics);
  11. return "index";
  12. }
  13. @GetMapping("/topic/{id}")
  14. public String topic(@PathVariable Long id, Model model) {
  15. Topic topic = topicService.getTopicById(id);
  16. List<Comment> comments = commentService.getCommentsByTopicId(id);
  17. model.addAttribute("topic", topic);
  18. model.addAttribute("comments", comments);
  19. return "topic";
  20. }
  21. @GetMapping("/new-topic")
  22. public String newTopic() {
  23. return "new-topic";
  24. }
  25. @PostMapping("/new-topic")
  26. public String doNewTopic(Topic topic, HttpSession session) {
  27. User user = (User) session.getAttribute("user");
  28. topic.setUser(user);
  29. topicService.saveTopic(topic);
  30. return "redirect:/";
  31. }
  32. }

5.3 CommentController:

 
  1. @Controller
  2. public class CommentController {
  3. @Autowired
  4. private CommentService commentService;
  5. @PostMapping("/new-comment")
  6. public String doNewComment(Comment comment, HttpSession session) {
  7. User user = (User) session.getAttribute("user");
  8. comment.setUser(user);
  9. commentService.saveComment(comment);
  10. return "redirect:/topic/" + comment.getTopic().getId();
  11. }
  12. }

  1. 创建视图页面

6.1 index.html:

 
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>论坛首页</title>
  6. </head>
  7. <body>
  8. <h1>论坛首页</h1>
  9. <div>
  10. <a href="/new-topic">发表新帖</a>
  11. </div>
  12. <ul>
  13. <li th:each="topic : ${topics}">
  14. <a th:href="@{'/topic/' + ${topic.id}}"><h3 th:text="${topic.title}"></h3></a>
  15. <p th:text="${topic.content}"></p>
  16. <p>
  17. <span th:text="${topic.user.username}"></span>
  18. <span th:text="${#dates.format(topic.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
  19. </p>
  20. </li>
  21. </ul>
  22. </body>
  23. </html>

6.2 topic.html:

 
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>话题详情</title>
  6. </head>
  7. <body>
  8. <h1 th:text="${topic.title}"></h1>
  9. <p th:text="${topic.content}"></p>
  10. <p>
  11. <span th:text="${topic.user.username}"></span>
  12. <span th:text="${#dates.format(topic.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
  13. </p>
  14. <hr>
  15. <h2>评论</h2>
  16. <ul>
  17. <li th:each="comment : ${comments}">
  18. <p th:text="${comment.content}"></p>
  19. <p>
  20. <span th:text="${comment.user.username}"></span>
  21. <span th:text="${#dates.format(comment.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
  22. </p>
  23. </li>
  24. </ul>
  25. <hr>
  26. <form action="/new-comment" method="post">
  27. <input type="hidden" name="topic.id" th:value="${topic.id}">
  28. <textarea name="content"></textarea>
  29. <br>
  30. <button type="submit">提交评论</button>
  31. </form>
  32. </body>
  33. </html>

6.3 new-topic.html:

 
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>发表新帖</title>
  6. </head>
  7. <body>
  8. <h1>发表新帖</h1>
  9. <form action="/new-topic" method="post">
  10. <input type="text" name="title">
  11. <br>
  12. <textarea name="content"></textarea>
  13. <br>
  14. <button type="submit">发布帖子</button>
  15. </form>
  16. </body>
  17. </html>

6.4 login.html:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登录</title>
  6. </head>
  7. <body>
  8. <h1>登录</h1>
  9. <form action="/login" method="post">
  10. <input type="text" name="username">

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/573010
推荐阅读
相关标签
  

闽ICP备14008679号