hanker

Spring boot - boot 실행 시 sql 문 실행 본문

SPRING

Spring boot - boot 실행 시 sql 문 실행

hanker 2021. 8. 23. 14:54

Spring boot를 실행 시 SQL문을 실행하는 법에 대해 알아보자

 

우선 Spring boot 실행 시 MEMBERS 테이블을 만들려고 한다.

 

 - 실행하려는 SQL문 

schema.sql

 

 - application.properties에 설정을 한다. (H2 사용)

## H2 Database 사용 설정
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:~/test
spring.datasource.username=sa
spring.datasource.password=

# Spring boot 시작 시 sql 문 실행
spring.sql.init.mode=always

 

 - 사용 설정이 완료되면, /resources/ 경로에 schema.sql을 넣어주고, boot를 실행시킨다.

짠!