MySQL
password : 1111
create DATABASE
DB Name : testdb
install LOMBOK
0) ํ๋ก์ ํธ ๊ฐ์
์คํ: Spring Boot 3.x, Spring Web, Spring Data JPA, Thymeleaf(๋ทฐ), MySQL
ํ๋ฆ: index.html ํผ โ POST /messages (Controller) โ Service save() โ Repository save() โ JPA๊ฐ ํ ์ด๋ธ ์๋ ์์ฑ
DB๋ ๋จผ์ ์์ฑ(์คํค๋ง๋ง). ํ ์ด๋ธ์ ์ํฐํฐ๋ก ์๋ ์์ฑ
build.gradle - ํ๋ก์ ํธ์ ๊ฐ์ฅ ๊ธฐ๋ณธ ์ค์
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
application.properties - ์์ฑ๊ฐ ์ค์
๋๋น๋ช - testdb
์์ด๋ - root
ํจ์ค์๋ - 1111
spring.datasource.url=jdbc:mysql://localhost:3306/๋๋น๋ช ?useSSL=false&serverTimezone=UTC
spring.datasource.username=์์ด๋
spring.datasource.password=ํจ์ค์๋
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1111
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Entity - ํ ์ด๋ธ ์ ์
Message.java
package com.example.demo;
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String content;
}
Repository - ๋๋น์ฐ๊ฒฐ
MessageRepository.java
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MessageRepository extends JpaRepository<Message, Integer> {
}
Service - ์ฒ๋ฆฌ
MessageService.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
@Autowired
private MessageRepository messageRepository;
public void save(String content) {
Message m = new Message();
m.setContent(content);
messageRepository.save(m);
}
}
Controller - ํ๋ฆ ์ ์ด
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class MessageController {
@Autowired
private MessageService messageService; // ์๋น์ค ์๋ ์ฐ๊ฒฐ
@GetMapping("/")
public String index() {
return "index"; // index.html ๋ณด์ฌ์ฃผ๊ธฐ
}
@PostMapping("/messages")
public String create(@RequestParam("content") String content, Model model) {
messageService.save(content); // ๊ธ์ ์ ์ฅ
model.addAttribute("content", content); // ๊ฒฐ๊ณผ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ธฐ
return "index";
}
}
html - View
์ต์ด ํ ๋ฒ ์ค์น
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple</title>
</head>
<body>
<!-- ์ ๋ ฅ ์์ + ๋ฒํผ -->
<form method="post" action="/messages">
<input type="text" name="content" placeholder="Type here">
<button type="submit">Save</button>
</form>
<!-- ์ ์ฅ ํ ๊ฒฐ๊ณผ ๋ณด์ฌ์ฃผ๊ธฐ -->
<p th:if="${content != null}">
Saved Content: <span th:text="${content}"></span>
</p>
</body>
</html>
๋๋น์ ์ ์ฅํ ๋ฐ์ดํฐ๋ฅผ ํ๋ฉด์ ์ถ๋ ฅ
service
package com.example.demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
@Autowired
private MessageRepository messageRepository;
public void save(String content) {
Message m = new Message();
m.setContent(content);
messageRepository.save(m);
}
// 1) ์ ์ฒด ๋ชฉ๋ก (๊ธฐ๋ณธ)
public List<Message> findAll() {
return messageRepository.findAll();
}
}
controller
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class MessageController {
@Autowired
private MessageService messageService; // ์๋น์ค ์๋ ์ฐ๊ฒฐ
@GetMapping("/")
public String index(Model model) {
model.addAttribute("messages", messageService.findAll());
return "index"; // index.html ๋ณด์ฌ์ฃผ๊ธฐ
}
@PostMapping("/messages")
public String create(@RequestParam("content") String content, Model model) {
messageService.save(content); // ๊ธ์ ์ ์ฅ
return "redirect:/";
}
}
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple</title>
</head>
<body>
<!-- ์ ๋ ฅ ์์ + ๋ฒํผ -->
<form method="post" action="/messages">
<input type="text" name="content" placeholder="Type here">
<button type="submit">Save</button>
</form>
<!-- ์ ์ฅ ํ ๊ฒฐ๊ณผ ๋ณด์ฌ์ฃผ๊ธฐ
<p th:if="${content != null}">
Saved Content: <span th:text="${content}"></span>
</p>
-->
<ul>
<li th:each="m : ${messages}" th:text="${m.id + ' - ' + m.content}"></li>
</ul>
</body>
</html>