allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

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>