๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BE/๐Ÿƒ Spring

[Spring Boot JPA ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ] 1. ํ™˜๊ฒฝ์„ค์ •

by ํ‹ด๋”” 2024. 4. 18.
๋ฐ˜์‘ํ˜•

 Intro

 

Spring Boot์™€ JPA๋ฅผ ํ™œ์šฉํ•ด์„œ ๊ฐ„๋‹จํ•œ ๊ฒŒ์‹œํŒ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ธ€์€ ์‹œ๋ฆฌ์ฆˆ๋กœ ์ž‘์„ฑ๋  ์˜ˆ์ •์ด๋ฉฐ ํ˜„์žฌ ๊ธ€์—์„œ๋Š” ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ, build.gradle๊ณผ dependencies, applicaiton.yaml ํŒŒ์ผ ์„ค์ •์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ๊ฒŒ์š”.

- IDE : IntelliJ

- java version : 17

 

 ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

์ผ์ผ์ด ์ˆ˜๋™์œผ๋กœ Spring ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— IntelliJ์™€ Spring Initializer๋ฅผ ์‚ฌ์šฉํ•ด์„œ Spring Boot ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค!

 

IntelliJ IDEA  ์‚ฌ์šฉ

 

 

์ธํ…”๋ฆฌ ์ œ์ด ์œ ๋ฃŒ ๋ฒ„์ „(IntelliJ IDEA)์„ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ Spring Boot Generators๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”. 

 

 

dependencies๋Š” ์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•ด ์ฃผ์„ธ์š”. ๊ฐ ์˜์กด์„ฑ์ด ์˜๋ฏธํ•˜๋Š” ๊ฑด ์•„๋ž˜์— ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

 

Spring Initializer ์‚ฌ์šฉ

 

https://start.spring.io/

 

 

์œ„ ๋งํฌ์˜ ์›น์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด Spring Boot ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด์š”. IntelliJ์˜ ๊ฒฝ์šฐ ๋ฌด๋ฃŒ ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” Generator์„ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด๋ฃŒ ๋ฒ„์ „์˜ IntelliJ๋ฅผ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด ์ด ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

 

IntelliJ์—์„œ ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. Gradle๊ณผ Maven์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ , ์–ธ์–ด๋กœ๋Š” Java, Kotlin, Groovy๋ฅผ ์„ ํƒํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

SNAPSHOT ์˜ ๊ฒฝ์šฐ ์•„์ง ์•ˆ์ •ํ™”๊ฐ€ ๋˜์ง€ ์•Š์€ ๋ฒ„์ „์ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ ๊ธ€ ์ž‘์„ฑ ๊ธฐ์ค€ 3.2.4๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Project์˜ ์ด๋ฆ„๊ณผ Package name๋“ฑ์„ ์„ค์ •ํ•ด ์ฃผ๊ณ  Java ๋ฒ„์ „๋„ ์„ ํƒํ•ด ์ค๋‹ˆ๋‹ค. 

 

 

์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ADD DEPENDENCIES ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด dependencies๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์–ด์š”. 

 

๊ฐ„๋‹จํ•œ Spring Boot ๊ฒŒ์‹œํŒ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด ์ฃผ์„ธ์š”

 

ํ•˜๋‹จ์˜ GENERATE ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์••์ถ•์„ ํ•ด์ œํ•˜๊ณ  IntelliJ์—์„œ ํ”„๋กœ์ ํŠธ ํด๋”๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”. 

์ƒ๋‹จ ์ƒํƒœ๋ฐ”์— File > Open์„ ๋ˆŒ๋Ÿฌ ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ํด๋”๋กœ ์ด๋™ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

 Build.gradle

 

Build.gradle์ด๋ž€?

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

 

[Gradle] build.gradle์˜ ๋™์ž‘์›๋ฆฌ ํ•œ ๋ฒˆ์— ์ •๋ฆฌํ•˜๊ธฐ

build.gradle์€ ๋ฌด์—‡์ธ๊ฐ€? build.gradle์€ ํŒŒ์ผ ์ž์ฒด๊ฐ€ Project ์˜ค๋ธŒ์ ํŠธ(๊ฐ์ฒด)๋กœ, Project ์˜ค๋ธŒ์ ํŠธ๋Š” Project ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌํ˜„์ฒด์ด๋‹ค. Project ์˜ค๋ธŒ์ ํŠธ๋Š” Project ๋‹จ์œ„์—์„œ ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ

kotlinworld.com

 

public interface Project extends Comparable<Project>, ExtensionAware, PluginAware { }

 

ํ”„๋กœ์ ํŠธ ๋‹จ์œ„์—์„œ ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ชจ์•„๋†“์€ ์Šˆํผ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. plugins, repositories, dependencies ๋ฉ”์„œ๋“œ๋“ฑ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. 

 

build.gradle ์„ค์ •ํ•˜๊ธฐ

 

 

build.gradle ํŒŒ์ผ์„ ๋“ค์–ด๊ฐ€๋ณด๋ฉด ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ์— ์ง€์ •ํ•ด ๋‘” dependencies ๊ฐ€ ์ด๋ฏธ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

 

 

 

๊ฐ dependency๊ฐ€ ํ•˜๋Š” ์—ญํ™œ์„ ์•Œ์•„๋ณผ๊ฒŒ์š”!

 

lombok

 

๋กฌ๋ณต์€ Getter, Setter, NoArgs, NoArgsConstructor์™€ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ง์ ‘ getter, setter, ์ƒ์„ฑ์ž ๋“ฑ์˜ ์ฝ”๋“œ ๋Œ€์‹  ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณต์žกํ•˜๊ณ  ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ•์†Œ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

Builder๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

    @Builder
    public Board(String title, String content, String author) {
        this.title = title;
        this.content = content;
        this.author = author;
    }

 

        Board board = Board.builder()
                .title(title)
                .content(content)
                .author("admin")
                .build();

 

Spring Web

https://mvnrepository.com/artifact/org.springframework/spring-web

 

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๊ธฐ ์œ„ํ•ด Spring์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์›น ์š”์ฒญ ๋ฐ ์‘๋‹ต ์ฒ˜๋ฆฌ, HTTP ์š”์ฒญ์„ java ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜, Java ๊ฐ์ฒด๋ฅผ HTTP ์‘๋‹ต์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด์š”.

 

 Thymeleaf

 

 

Thymeleaf

Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati

www.thymeleaf.org

 

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

 

Thymeleaf(ํƒ€์ž„๋ฆฌํ”„)๋ž€ ? ํƒ€์ž„๋ฆฌํ”„์˜ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์•Œ์•„๋ณด๊ธฐ

Thymeleaf(ํƒ€์ž„๋ฆฌํ”„)๋ž€ ? ํƒ€์ž„๋ฆฌํ”„๋Š” JSP, Freemarker์™€ ๊ฐ™์€ ํ…œํ”Œ๋ฆฟ ์—”์ง„์˜ ์ผ์ข…์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„ ์‚ฌ์ด๋“œ HTML ๋ Œ๋”๋ง (SSR) ๋ฐฑ์—”๋“œ ์„œ๋ฒ„์—์„œ HTML์„ ๋™์ ์œผ๋กœ ๋ Œ๋”๋ง ํ•˜๋Š” ์šฉ

hstory0208.tistory.com

 

๋ฐฑ์—”๋“œ ์„œ๋ฒ„์—์„œ HTML์„ ๋™์ ์œผ๋กœ ๋ Œ๋”๋ง ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์—”์ง„์œผ๋กœ๋Š” JSP๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„์— ๋‚ด์šฉ์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ html์— ๋‚ด์šฉ์„ ๋‹ด์•„ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”. 

์ด๋ฒˆ ๊ฒŒ์‹œํŒ ํ”„๋กœ์ ํŠธ์—๋Š” ํƒ€์ž„๋ฆฌํ”„๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ”„๋ก ํŠธ ๊ฐœ๋ฐœ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

 Spring JDBC

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

https://mininkorea.tistory.com/39

 

JDBC(Spring JDBC)๋ž€? / ์Šคํ”„๋ง JDBC ์•Œ๊ณ ๋„˜์–ด๊ฐ€๊ธฐ! ^^

JDBC(Spring JDBC)๋ž€? JDBC (Java Database Connectivity)๋Š” ์ž๋ฐ”์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” API(์‘์šฉ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ธํ„ฐํŽ˜์ด์Šค)์ž…๋‹ˆ๋‹ค. JDBC๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ํ†ต์‹ ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—

mininkorea.tistory.com

 

JDBC(Java Database Connectivity)๋Š” Java ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰, ์‚ฝ์ž…, ๊ฐฑ์‹ , ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

JPA๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ์ž‘์šฉ์„ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ JDBC๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 MariaDB Driver

 

database๋กœ๋Š” ๋งˆ๋ฆฌ์•„ db๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

application.yaml

 

spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true
  jpa:
    hibernate:
      ddl-auto: update
    properties:
        hibernate:
            show_sql: true
            format_sql: true
  datasource:
    url: jdbc:mariadb://localhost:3306/dev
    driver-class-name: org.mariadb.jdbc.Driver
    username: dev
    password: 0000
logging:
  level:
    root: info

 

์ตœ์ข… application.yaml์€ ์ด๋ ‡์Šต๋‹ˆ๋‹ค! ๋งŒ์•ฝ application.yaml ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒ์„ฑํ•ด ์ฃผ์„ธ์š”.

 

์œ„์น˜๋Š” resources ํด๋”์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค!

 

 

spring.mvc.hiddenmethod.filter.enabled: true

 

HTTP Post ์š”์ฒญ์„ ํ†ตํ•ด ์˜ค๋Š” ํผ ๋ฐ์ดํ„ฐ์— ์ˆจ๊ฒจ์ง„ HTTP ๋ฉ”์„œ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์–ด์š”. HTML ํผ์—์„œ put์ด๋‚˜ delete์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š”๋ฐ ์ด๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

spring.jpa.hibernate.ddl-auto: update

 

 

Hibernate๋ฅผ ์‚ฌ์šฉํ•ด์„œ JPA ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ์— ์ž๋™ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ์ „๋žต์ž…๋‹ˆ๋‹ค.

 

spring.jpa.properties.hibernate.show_sql: true

 

 

Hibernate๊ฐ€ ์ƒ์„ฑํ•˜๋Š” SQL ์ฟผ๋ฆฌ๋ฅผ ๋กœ๊น…ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. 

 

spring.jpa.properties.hibernate.format_sql: true

 

Hibernate๊ฐ€ ์ƒ์„ฑํ•˜๋Š” SQL ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ์ฟผ๋ฆฌ๋กœ ๋–จ๊ถˆ์ฃผ๋Š” ๊ฑด ์•„๋‹ˆ๋ผ ํฌ๋ฉง ์„ค์ •์„ ํ•ด์ค๋‹ˆ๋‹ค. SQL ์ฟผ๋ฆฌ๋ฅผ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ํฌ๋ฉง ํ•ด์ค๋‹ˆ๋‹ค.

 

spring.datasource.url: jdbc:mariadb://localhost:3306/dev

 

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•  JDBC ๋“œ๋ผ์ด๋ฒ„ ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์œ„์— ๊ฒฝ์šฐ MariaDB์˜ dev ์Šคํ‚ค๋งˆ์— ์—ฐ๊ฒฐ๋˜์š”. mariadb์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ํฌํŠธ๋Š” 3306์ž…๋‹ˆ๋‹ค.

 

spring.datasource.driver-class-name: org.mariadb.jdbc.Driver

 

MariaDB JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

logging.level.root: info

 

๋ฃจํŠธ ๋กœ๊ฑฐ์˜ ๋กœ๊น… ๋ ˆ๋ฒจ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋กœ๊ทธ๋ฅผ ๋ณด์ง€ ์•Š๊ณ  INFO ์ด์ƒ์˜ ๋กœ๊ทธ๋งŒ ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.

 

 ๋งˆ๋ฌด๋ฆฌ

 

๊ธฐ๋ณธ์ ์ธ ์„ค์ •์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์š”!

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ž ์‹œ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~

 

 

Ron Lach ๋‹˜์˜ ์‚ฌ์ง„: https://www.pexels.com/ko-kr/photo/9903145/

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€