Springboot 2.5 이후 data init script 초기화에 대한 변경

아래 소스 코드를 관찰해 보자

위치는 : org.springframework.boot.autoconfigure.jdbc.DataSourceProperties 이다

/**
 * Mode to apply when determining if DataSource initialization should be performed
 * using the available DDL and DML scripts.
 */
@Deprecated
private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;

/**
 * Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or
 * data-${platform}.sql).
 */
@Deprecated
private String platform = "all";

/**
 * Schema (DDL) script resource references.
 */
private List<String> schema;

/**
 * Username of the database to execute DDL scripts (if different).
 */
@Deprecated
private String schemaUsername;

/**
 * Password of the database to execute DDL scripts (if different).
 */
@Deprecated
private String schemaPassword;

/**
 * Data (DML) script resource references.
 */
@Deprecated
private List<String> data;

/**
 * Username of the database to execute DML scripts (if different).
 */
@Deprecated
private String dataUsername;

/**
 * Password of the database to execute DML scripts (if different).
 */
@Deprecated
private String dataPassword;

/**
 * Whether to stop if an error occurs while initializing the database.
 */
@Deprecated
private boolean continueOnError = false;

/**
 * Statement separator in SQL initialization scripts.
 */
@Deprecated
private String separator = ";";

/**
 * SQL scripts encoding.
 */
@Deprecated
private Charset sqlScriptEncoding;

기존것(2.5 이전 버전)

새로운 설정 방법 (2.5 이후 버전)

확인 해보니 org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties 클래스를 적용하였더라. 그래서 설정도 바뀐거다.

pom.xml 에 mysql 디펜던시를 추가한다.

application.properties

resource 폴더에 schema-all.sql 생성

설정후 springboot 구동하면Database에 jpa를 통하여 생성 된 기본 테이블 및 데이터가 생성된걸 확인 할수 있다.

spring.sql.init.enabled : 데이터를 초기화 할 것인지 선택 기본 true , false 를 선택하여 disable 상태로 만들어 놓는다. -D 명령어 사용 가능.

spring.sql.init.username和spring.sql.init.password : 스크립트 수행 사용자와 패스워드 . 해당 설정은 중요하다. 보안에서 db 계정마다 권한이 다르므로 해당 설정을 꼭 해줘야 생성, 삭제 명령 등 위험한 명령어 및 스크립트를 수행하는걸 방지한다.

spring.sql.init.schema-locations : schema 변경 관련 sql 스크립트, 다중 설정가능 기본값은 ; 임. spring.sql.init.data-locations : data 관련 스크립트설정, 다중설정가능 , 기본값은 ; 임. spring.sql.init.encoding:스크립트 인코딩

spring.sql.init.separator : 다중 설정 .sql 파일명 구분자 기본 값 ; 임

spring.sql.init.continue-on-error : 스크립트 실행 중 오류발생시 계속 진행할것인지 결정하는 값.기본값은 false.

Last updated