본문 바로가기
Programing/Spring

Spring Boot H2 Database 사용법

by 미미믹 2023. 11. 3.
H2 Database 란?

컴퓨터에 내장된 램(RAM)에 의존하는 자바 기반 RDBMS (In-Memory Database)

램에 의존하기 때문에, 서버 재부팅 시 데이터가 초기화된다.

sql 파일로 미리 작성해두면 초기화될 때마다 자동 적용되게 할 수 있다.(resource 폴더 밑에 .sql 파일 생성)

 

사용 방법

Dependency

Gradle

dependencies {
  runtimeOnly 'com.h2database:h2'
}

Maven

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

 

applicationi.yml (application.properties)

spring:
  h2:
    console:
    enabled: true		# h2 콘솔 사용
    path: h2-console	# 접속 url e.g.) localhost:8080/h2-console
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:testdb
    username: sa
    password:

서버 실행 후 http://localhost:8080/h2-console 입력

해당 화면이 출력된다.

 

* 404 Error 발생 시

https://mi2mic.tistory.com/221 참고


https://www.h2database.com/html/main.html

728x90