๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

Chp3. ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ JPA๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ค„๋ณด์ž

by coderSohyun 2023. 8. 19.

์›น ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ์šด์˜ํ•˜๋‹ค ๋ณด๋ฉด ํ”ผํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃจ๋Š” ์ผ์ž„

 

iBatis์™€ ๊ฐ™์€ SQL ๋งคํผ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•จ

๊ทธ๋Ÿฌ๋‹ค ๋ณด๋‹ˆ ์‹ค์ œ๋กœ ๊ฐœ๋ฐœํ•˜๋Š” ์‹œ๊ฐ„๋ณด๋‹ค SQL์„ ๋‹ค๋ฃจ๋Š” ์‹œ๊ฐ„์ด ๋” ๋งŽ์•„์กŒ์Œ

์ด๋Ÿฌ๋‹ค๋ณด๋‹ˆ ๊ฐ์ฒด ๋ชจ๋ธ๋ง๋ณด๋‹ค๋Š” ํ…Œ์ด๋ธ” ๋ชจ๋ธ๋ง์—๋งŒ ์ง‘์ค‘ํ•˜๊ณ , ๊ฐ์ฒด๋ฅผ ๋‹จ์ˆœํžˆ ํ…Œ์ด๋ธ”์— ๋งž์ถ”์–ด ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ์—ญํ• ๋งŒ ํ•˜๋Š” ๊ฐœ๋ฐœ์€ ๋ถ„๋ช… ๊ธฐํ˜•์ ์ธ ํ˜•ํƒœ์ž„

์–ด๋ฏ๊ฒŒ ํ•˜๋ฉด ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•จ

๊ทธ๋Ÿฌ๋˜ ์ค‘, ์ด๋ฏธ ๊ธฐ์กด์˜ ์ž๋ฐ” ๊ฐœ๋ฐœ์ž๋ถ„๋“ค ์—ญ์‹œ ์ด ๊ณ ๋ฏผ์„ ํ•˜๊ณ  ์žˆ์—ˆ์Œ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Œ

๋ฌธ์ œ์˜ ํ•ด๊ฒฐ์ฑ…์œผ๋กœ JPA๋ผ๋Š” ์ž๋ฐ” ํ‘œ์ค€ ORM (Object Relational Mapping) ๊ธฐ์ˆ ์„ ๋งŒ๋‚˜๊ฒŒ ๋จ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” JPA๋ฅผ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•ด ๋ณด๊ฒ ์Œ. 

MyBatis, iBatis๋Š” ORM์ด ์•„๋‹˜. SQL Mapper์ž„. ๊ฐ€๋” ORM์— ๋Œ€ํ•ด MyBatis, iBatis๋ฅผ ์–˜๊ธฐํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๋‘˜์€ ORM์ด ์•„๋‹˜. ORM์€ ๊ฐ์ฒด๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ์ด๊ณ  SQL Mapper๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋งคํ•‘ํ•จ

 

3.1 JPA ์†Œ๊ฐœ

์š”๊ตฌ์‚ฌํ•ญ ๋ถ„์„

์ด ๊ต์žฌ์—์„œ 3~6์žฅ๊นŒ์ง€๋Š” ํ•˜๋‚˜์˜ ๊ฒŒ์‹œํŒ์„ ๋งŒ๋“ค์–ด ๋ณด๊ณ ,

7~10์žฅ๊นŒ์ง€๋Š” ์ด ์„œ๋น„์Šค๋ฅผ AWS์— ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ ํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ์ง„ํ–‰ํ•  ๊ฒƒ์ž„

 

  • ๊ฒŒ์‹œํŒ ๊ธฐ๋Šฅ
    • ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ
    • ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก
    • ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •
    • ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ
  • ํšŒ์› ๊ธฐ๋Šฅ
    • ๊ตฌ๊ธ€ / ๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ
    • ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž ๊ธ€ ์ž‘์„ฑ ๊ถŒํ•œ
    • ๋ณธ์ธ ์ž‘์„ฑ ๊ธ€์— ๋Œ€ํ•œ ๊ถŒํ•œ ๊ด€๋ฆฌ

 

3.2 ํ”„๋กœ์ ํŠธ์— Spring Data Jpa ์ ์šฉํ•˜๊ธฐ

๋จผ์ € build.gradle์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜์กด์„ฑ๋“ค์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค (spring-boot-starter-data-jpa, h2)

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'com.h2database:h2'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

โ‘  spring-boot-starter-data-jpa

 

โ‘ก h2

 

์˜์กด์„ฑ์ด ๋“ฑ๋ก๋์œผ๋ฉด, ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ JPA ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด ๋ณด์ž

domain ํŒจํ‚ค์ง€

domain ํŒจํ‚ค์ง€๋Š” ๋„๋ฉ”์ธ์„ ๋‹ด์„ ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. 

์—ฌ๊ธฐ์„œ ๋„๋ฉ”์ธ์ด๋ž€ ๊ฒŒ์‹œ๊ธ€, ๋Œ“๊ธ€, ํšŒ์›, ์ •์‚ฐ, ๊ฒฐ์ œ ๋“ฑ ์†Œํ”„ํŠธ์›จ์–ด์— ๋Œ€ํ•œ ์š”๊ตฌ์‚ฌํ•ญ ํ˜น์€ ๋ฌธ์ œ ์˜์—ญ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

posts ํŒจํ‚ค์ง€, Posts ํด๋ž˜์Šค

domain ํŒจํ‚ค์ง€์— posts ํŒจํ‚ค์ง€๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์•ˆ์— Posts ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋ด…๋‹ˆ๋‹ค. 

package study.springboot.domain.posts;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@Entity
public class Posts {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 500, nullable = false)
    private String title;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    private String author;

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

    public void update(String title, String content) {
        this.title = title;
        this.content = content;
    }
}