https://start.spring.io 에서 다음 항목 설정
Project: Maven
Language: Java
Spring Boot: 3.x 이상
Dependencies:
Spring Web
Spring Data JPA
Thymeleaf
H2 Database
Lombok
다운로드 후 압축 해제 및 IDE에서 열기
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
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;
}
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> {
}
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:/";
}
}
src/main/resources/templates/ 폴더에 아래 파일 생성
<!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>
<!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>
<!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>
./mvnw spring-boot:run
브라우저에서 http://localhost:8080 접속 시 게시판 동작 확인 가능
이 예제는 Spring Boot를 기반으로 한 기본 게시판 CRUD 기능 구현입니다.
필요 시 댓글 기능, 수정 기능, 사용자 인증(Spring Security), MySQL 연동 등 확장 가능합니다.
요청하시면 위 확장 기능도 예제로 만들어 드릴 수 있습니다.