H2 DB ์„ค์ •
H2 DB๋Š” ์ปดํ“จํ„ฐ์— ๋‚ด์žฅ๋œ ๋žจ(RAM) ๋ฉ”๋ชจ๋ฆฌ์— ์˜์กดํ•˜๋Š” ์ž๋ฐ” ๊ธฐ๋ฐ˜์˜ RDBMS์ด๋‹ค. ์šฉ๋Ÿ‰์ด ์ ๊ณ , ๋ธŒ๋ผ์šฐ์ € ๊ธฐ๋ฐ˜์˜ ์ฝ˜์†” ๋“ฑ์„ ์ง€์›ํ•ด ์žฅ์ ์ด ๋งŽ๋‹ค. ๋˜ํ•œ, SpringBoot์—์„œ ๋ณ„๋„ DB๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋„ ์žฅ์ ์ด๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ๋ณดํ†ต ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ๋งŒ ์“ฐ์ธ๋‹ค.

Dependency

Gradle

1
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
2
runtimeOnly 'com.h2database:h2'
Copied!
H2 ์˜์กด์„ฑ์„ build.gradle์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. runtimeOnly ๋กœ ๋Ÿฐํƒ€์ž„์‹œ์ ์—๋งŒ ์˜์กดํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ๋‹ค.

Maven

1
# pom.xml
2
<dependency>
3
<groupId>com.h2database</groupId>
4
<artifactId>h2</artifactId>
5
<scope>runtime</scope>
6
</dependency>
7
โ€‹
8
<dependency>
9
<groupId>org.springframework.boot</groupId>
10
<artifactId>spring-boot-starter-data-jpa</artifactId>
11
</dependency>
Copied!

์Šคํ”„๋ง ๋ถ€ํŠธ ์ž๋™ ์„ค์ •

๋นˆ์„ ๋“ฑ๋กํ•ด H2 ์ฝ˜์†”์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ์Šคํ”„๋ง ๋ถ€ํŠธ ์ž๋™์„ค์ •์œผ๋กœ H2๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
์Šคํ”„๋ง ๋ถ€ํŠธ ์ž๋™ ์„ค์ • ํ”„๋กœํผํ‹ฐ ๋ฌธ์„œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋‹ค.
image-20210216215948595
spring.h2.console.enabled๋Š” ์ฝ˜์†” ์‚ฌ์šฉ์—ฌ๋ถ€ ๊ฐ’์œผ๋กœ false๊ฐ€ ๋””ํดํŠธ ๊ฐ’์ด๋ฉฐ, ์ด ๊ฐ’์„ true๋กœ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ๋ณ„๋„ ๋นˆ ์„ค์ •์„ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์„ค์ •(application.yml)

1
spring:
2
h2:
3
console:
4
enabled: true # ์ฝ˜์†” ์‚ฌ์šฉ์—ฌ๋ถ€
5
profiles:
6
active: local
7
# H2 ๋ฉ”๋ชจ๋ฆฌ DB๋ฅผ ์œ„ํ•œ ์„ค์ •
8
datasource:
9
driver-class-name: org.h2.Driver
10
url: jdbc:h2:mem:userdb;DB_CLOSE_DELAY=-1
11
username: sa
12
password:
Copied!
โ€‹http://localhost:8080/h2-console๋กœ ๋“ค์–ด๊ฐ€๋ฉด ์•„๋ž˜ ์ฐฝ์ด ๋œจ๊ณ , ์œ„์—์„œ ์„ค์ •ํ•œ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ๋กœ๊ทธ์ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
โ€‹
โ€‹

ํ…Œ์ด๋ธ”, ๋ฐ์ดํ„ฐ ์ƒ์„ฑ

spring์€ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ classpath ๋ฃจํŠธ์— schema.sql๊ณผ data.sql ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ์„œ๋ฒ„ ์‹œ์ž‘ ์‹œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • src/main/java/resources/schema.sql : ํ…Œ์ด๋ธ” ์Šคํ‚ค๋งˆ(DDL)
    1
    --
    2
    -- ํšŒ์›
    3
    --
    4
    DROP TABLE IF EXISTS MEMBER;
    5
    โ€‹
    6
    CREATE TABLE MEMBER COMMENT 'ํšŒ์›' (
    7
    MBR_ID VARCHAR(10) NOT NULL COMMENT 'ํšŒ์›ID'
    8
    , NAME VARCHAR(100) COMMENT 'ํšŒ์›๋ช…'
    9
    , PRIMARY KEY (MBR_ID)
    10
    );
    Copied!
  • src/main/java/resources/data.sql : DML
    1
    INSERT INTO MEMBER
    2
    (MBR_ID, NAME)
    3
    VALUES ('0000000010', 'ํšŒ์›_0010')
    4
    , ('0000000098', 'ํšŒ์›_0098')
    5
    , ('0000000101', 'ํšŒ์›_0101')
    6
    ;
    Copied!
์œ„ ๋‘๊ฐœ ํŒŒ์ผ์„ ์ƒ์„ฑํ›„ ๋‹ค์‹œ ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•˜๋ฉด schema.sql์˜ MEMBER ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜์—ˆ๊ณ , data.sql์— ์„ ์–ธ๋œ ๋ฐ์ดํ„ฐ๋“ค๋„ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
โ€‹
โ€‹
ํŠน์ • ํ™˜๊ฒฝ์— ๋งž๋Š” SQL์„ ์‹คํ–‰์‹œํ‚ค๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ํ”„๋กœํผํ‹ฐ ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด๋œ๋‹ค.
1
spring.datasource.platfrom= {platform}
Copied!
๊ทธ๋Ÿฐํ›„ ํ”Œ๋žซํผ์— ๋งž๊ฒŒ schema-{platform}.sql , data-{platform}.sql ์„ ์ƒ์„ฑํ•˜๋ฉด ํ•ด๋‹น ํ”Œ๋žซํผ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ๋ฉ”๋ชจ๋ฆฌ DB๊ฐ€ ์•„๋‹Œ๊ฒฝ์šฐ ์Šคํฌ๋ฆฝ๋“œ ๋ฌธ์„ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.
๋ฉ”๋ชจ๋ฆฌ DB๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋„ ์‹คํ–‰์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜ ํ”„๋กœํผํ‹ฐ๋ฅผ always๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
1
spring.datasource.initialization-mode=always
Copied!
์ดˆ๊ธฐํ™” ์„ค์ •์„ ํ•˜๊ณ ์‹ถ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” never ์„ค์ •์„ ํ•˜๋ฉด๋œ๋‹ค.
1
spring.datasource.initialization-mode=never
Copied!

์ฐธ๊ณ