IMG-LOGO
공지사항 :

Spring Boot 게시판 예제 만들기

lmkfox - 2025-05-19 06:44:52 61 Views 0 Comment

1. 프로젝트 설정

1.1 Spring Initializr 설정

https://start.spring.io 에서 다음 항목 설정

  • Project: Maven

  • Language: Java

  • Spring Boot: 3.x 이상

  • Dependencies:

    • Spring Web

    • Spring Data JPA

    • Thymeleaf

    • H2 Database

    • Lombok

다운로드 후 압축 해제 및 IDE에서 열기


2. application.properties 설정

src/main/resources/application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update


3. 게시글 Entity 클래스

src/main/java/com/example/board/entity/Post.java

package com.example.board.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    @Column(length = 1000)
    private String content;
}


4. Repository 생성

src/main/java/com/example/board/repository/PostRepository.java

package com.example.board.repository;

import com.example.board.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {
}


5. Controller 생성

src/main/java/com/example/board/controller/PostController.java

package com.example.board.controller;

import com.example.board.entity.Post;
import com.example.board.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequiredArgsConstructor
public class PostController {

    private final PostRepository postRepository;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("posts", postRepository.findAll());
        return "index";
    }

    @GetMapping("/post/new")
    public String createForm(Model model) {
        model.addAttribute("post", new Post());
        return "create";
    }

    @PostMapping("/post")
    public String create(Post post) {
        postRepository.save(post);
        return "redirect:/";
    }

    @GetMapping("/post/{id}")
    public String view(@PathVariable Long id, Model model) {
        model.addAttribute("post", postRepository.findById(id).orElseThrow());
        return "view";
    }

    @PostMapping("/post/{id}/delete")
    public String delete(@PathVariable Long id) {
        postRepository.deleteById(id);
        return "redirect:/";
    }
}


6. Thymeleaf View 생성

src/main/resources/templates/ 폴더에 아래 파일 생성

6.1 

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>게시판</title></head>
<body>
    <h1>게시판 목록</h1>
    <a href="/post/new">글쓰기</a>
    <ul>
        <li th:each="post : ${posts}">
            <a th:href="@{/post/{id}(id=${post.id})}" th:text="${post.title}"></a>
        </li>
    </ul>
</body>
</html>


6.2 

create.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>글쓰기</title></head>
<body>
    <h1>새 글 작성</h1>
    <form action="/post" method="post" th:object="${post}">
        제목: <input type="text" th:field="*{title}"><br>
        내용: <textarea th:field="*{content}"></textarea><br>
        <button type="submit">등록</button>
    </form>
</body>
</html>


6.3 

view.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title th:text="${post.title}">게시글 보기</title></head>
<body>
    <h1 th:text="${post.title}"></h1>
    <p th:text="${post.content}"></p>
    <form th:action="@{/post/{id}/delete(id=${post.id})}" method="post">
        <button type="submit">삭제</button>
    </form>
    <a href="/">목록으로</a>
</body>
</html>


7. 실행

./mvnw spring-boot:run

브라우저에서 http://localhost:8080 접속 시 게시판 동작 확인 가능


마무리

이 예제는 Spring Boot를 기반으로 한 기본 게시판 CRUD 기능 구현입니다.

필요 시 댓글 기능, 수정 기능, 사용자 인증(Spring Security), MySQL 연동 등 확장 가능합니다.

요청하시면 위 확장 기능도 예제로 만들어 드릴 수 있습니다.


댓글