国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Spring Boot】Spring Boot實現(xiàn)完整論壇功能示例代碼

這篇具有很好參考價值的文章主要介紹了【Spring Boot】Spring Boot實現(xiàn)完整論壇功能示例代碼。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

以下是一個簡單的Spring Boot論壇系統(tǒng)示例代碼:

  1. 首先是數(shù)據(jù)庫設(shè)計,我們創(chuàng)建以下幾張表:
  • 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. 創(chuàng)建實體類

2.1 User實體類:

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String username;

    @Column
    private String password;

    @Column
    private String email;

    @Column(name = "create_time")
    private Date createTime;

    // getter/setter省略
}

2.2 Topic實體類:

@Entity
@Table(name = "topic")
public class Topic {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String title;

    @Column
    private String content;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @Column(name = "create_time")
    private Date createTime;

    // getter/setter省略
}

2.3 Comment實體類:

@Entity
@Table(name = "comment")
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String content;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "topic_id")
    private Topic topic;

    @Column(name = "create_time")
    private Date createTime;

    // getter/setter省略
}
  1. 創(chuàng)建Repository

3.1 UserRepository:

public interface UserRepository extends JpaRepository<User, Long> {

    User findByUsername(String username);

}

3.2 TopicRepository:

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

3.3 CommentRepository:

public interface CommentRepository extends JpaRepository<Comment, Long> {
}
  1. 創(chuàng)建Service

4.1 UserService:

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public boolean checkUser(User user) {
        User userInDb = userRepository.findByUsername(user.getUsername());
        if (userInDb != null && userInDb.getPassword().equals(user.getPassword())) {
            return true;
        } else {
            return false;
        }
    }

    public void saveUser(User user) {
        user.setCreateTime(new Date());
        userRepository.save(user);
    }

    public User getUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }
}

4.2 TopicService:

@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepository;

    public List<Topic> getAllTopics() {
        return topicRepository.findAll();
    }

    public Topic getTopicById(Long id) {
        return topicRepository.findById(id).orElse(null);
    }

    public void saveTopic(Topic topic) {
        topic.setCreateTime(new Date());
        topicRepository.save(topic);
    }

}

4.3 CommentService:

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    public void saveComment(Comment comment) {
        comment.setCreateTime(new Date());
        commentRepository.save(comment);
    }

    public List<Comment> getCommentsByTopicId(Long topicId) {
        return commentRepository.findAllByTopicId(topicId);
    }
}
  1. 創(chuàng)建Controller

5.1 UserController:

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(User user, HttpSession session) {
        boolean result = userService.checkUser(user);
        if (result) {
            session.setAttribute("user", user);
            return "redirect:/";
        } else {
            return "login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String doRegister(User user) {
        userService.saveUser(user);
        return "redirect:/login";
    }
}

5.2 TopicController:

@Controller
public class TopicController {

    @Autowired
    private TopicService topicService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/")
    public String index(Model model) {
        List<Topic> topics = topicService.getAllTopics();
        model.addAttribute("topics", topics);
        return "index";
    }

    @GetMapping("/topic/{id}")
    public String topic(@PathVariable Long id, Model model) {
        Topic topic = topicService.getTopicById(id);
        List<Comment> comments = commentService.getCommentsByTopicId(id);
        model.addAttribute("topic", topic);
        model.addAttribute("comments", comments);
        return "topic";
    }

    @GetMapping("/new-topic")
    public String newTopic() {
        return "new-topic";
    }

    @PostMapping("/new-topic")
    public String doNewTopic(Topic topic, HttpSession session) {
        User user = (User) session.getAttribute("user");
        topic.setUser(user);
        topicService.saveTopic(topic);
        return "redirect:/";
    }

}

5.3 CommentController:

 
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/new-comment")
    public String doNewComment(Comment comment, HttpSession session) {
        User user = (User) session.getAttribute("user");
        comment.setUser(user);
        commentService.saveComment(comment);
        return "redirect:/topic/" + comment.getTopic().getId();
    }

}
  1. 創(chuàng)建視圖頁面

6.1 index.html:

 
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>論壇首頁</title>
</head>
<body>
    <h1>論壇首頁</h1>
    <div>
        <a href="/new-topic">發(fā)表新帖</a>
    </div>
    <ul>
        <li th:each="topic : ${topics}">
            <a th:href="@{'/topic/' + ${topic.id}}"><h3 th:text="${topic.title}"></h3></a>
            <p th:text="${topic.content}"></p>
            <p>
                <span th:text="${topic.user.username}"></span>
                <span th:text="${#dates.format(topic.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
            </p>
        </li>
    </ul>
</body>
</html>

6.2 topic.html:

 
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>話題詳情</title>
</head>
<body>
    <h1 th:text="${topic.title}"></h1>
    <p th:text="${topic.content}"></p>
    <p>
        <span th:text="${topic.user.username}"></span>
        <span th:text="${#dates.format(topic.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
    </p>
    <hr>
    <h2>評論</h2>
    <ul>
        <li th:each="comment : ${comments}">
            <p th:text="${comment.content}"></p>
            <p>
                <span th:text="${comment.user.username}"></span>
                <span th:text="${#dates.format(comment.createTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
            </p>
        </li>
    </ul>
    <hr>
    <form action="/new-comment" method="post">
        <input type="hidden" name="topic.id" th:value="${topic.id}">
        <textarea name="content"></textarea>
        <br>
        <button type="submit">提交評論</button>
    </form>
</body>
</html>

6.3 new-topic.html:

 
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>發(fā)表新帖</title>
</head>
<body>
    <h1>發(fā)表新帖</h1>
    <form action="/new-topic" method="post">
        <input type="text" name="title">
        <br>
        <textarea name="content"></textarea>
        <br>
        <button type="submit">發(fā)布帖子</button>
    </form>
</body>
</html>

6.4 login.html:文章來源地址http://www.zghlxwxcb.cn/news/detail-671001.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登錄</title>
</head>
<body>
    <h1>登錄</h1>
    <form action="/login" method="post">
        <input type="text" name="username">

到了這里,關(guān)于【Spring Boot】Spring Boot實現(xiàn)完整論壇功能示例代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包