H2 Database 사용하기
1. 의존성 추가
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.h2database:h2'
}
2. applicatoin.yml 예시
**spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
jpa:
hibernate:
ddl-auto: update
database-platform: org.hibernate.dialect.H2Dialect**
spring.datasource.url : H2 데이터베이스의 URL을 지정
jdbc:h2:mem:testdb : 메모리 모드로 실행되는 H2 데이터베이스
spring.datasource.driver-class-name: H2 데이터베이스 드라이버 클래스
spring.h2.console.path: H2 콘솔에 접근할 경로를 지정, default(/h2-console)
spring.jpa.hibernate.ddl-auto: 데이터베이스 테이블 생성 및 업데이트 전략
- **
update**로 설정하면 애플리케이션 시작 시 데이터베이스 스키마가 자동으로 업데이트
spring.jpa.database-platform: Hibernate가 사용할 데이터베이스 방언을 설정합니다.
- H2 데이터베이스의 경우 **
org.hibernate.dialect.H2Dialect**를 사용
- JDBC URL을 **
jdbc:h2:mem:testdb**로 설정하고, 사용자 이름을 **sa**로 설정하여 접속 가능
3. 애플리케이션 실행
- Spring Boot 애플리케이션을 실행하여 H2 데이터베이스를 사용
- H2 콘솔에 접근하려면 브라우저에서 **
http://localhost:8080/h2-console**로 접속
참고자료
스프링 부트(Spring Boot) - 5분 안에 H2 Database와 JPA 연동해보기