본문 바로가기

⭐ SpringBoot/build (Gradle, Dependency 설정 등)

Springboot에서 DB를 변경하기 (H2 DB application.properties 수정 및 Dependency 추가)

# Springboot에서 기존에 사용하던 MySQL DB를 H2 DB로 변경한다.

1. application.properties 파일에 존재하는 MySQL 설정을 주석처리 한다.

- 주석처리

##### DB Connection Information Mysql #####
#spring.datasource.url=jdbc:mysql://localhost:3306/board-back?useSSL=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
#spring.datasource.username=root
#spring.datasource.password=1234

#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect

2. H2 DB 정보를 application.properties에 추가한다.

- 내용을 추가

##### DB Connection Information H2 DB ####
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.datasource.generate-unique-name=false
spring.datasource.url=jdbc:h2:mem:testdb

3. build.gradle 파일에 H2 DB 를 추가한다.

- H2 DB 라이브러리 추가

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-web'
// 라이브러리 추가 --
	runtimeOnly 'com.h2database:h2'
// 라이브러리 추가 --
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	runtimeOnly 'mysql:mysql-connector-java'
	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}