1. data.sql
INSERT INTO board_tb (title, content, created_at) VALUES ('제목1', '내용1', now());
INSERT INTO board_tb (title, content, created_at) VALUES ('제목2', '내용2', now());
INSERT INTO board_tb (title, content, created_at) VALUES ('제목3', '내용3', now());
INSERT INTO board_tb (title, content, created_at) VALUES ('제목4', '내용4', now());
INSERT INTO board_tb (title, content, created_at) VALUES ('제목5', '내용5', now());src/main/resources폴더 안에db폴더를 생성한 후, 그 안에data.sql파일을 생성한다.
data.sql파일 안에board_tb테이블에 데이터를 넣는 SQL 쿼리를 작성한다.
- Spring Boot가 애플리케이션 실행 시 자동으로
data.sql파일을 실행하여 초기 데이터를 삽입한다.
2. application.properties
# 3. 더미데이터 세팅
spring.sql.init.data-locations=classpath:db/data.sql
spring.jpa.defer-datasource-initialization=truespring.sql.init.data-locations=classpath:db/data.sql: Spring Boot가 애플리케이션 시작 시 자동으로 실행할 SQL 파일의 위치를 지정classpath는src/main/resources/폴더를 의미한다.
spring.jpa.defer-datasource-initialization=true: JPA 초기화 시점을 지연- 이 설정을 통해
DataSource초기화가 완료된 후에 JPA 초기화 및 데이터베이스 관련 작업이 수행되도록 하여,DataSource가 초기화되기 전에sql파일이 실행되어 발생할 수 있는 에러를 방지한다.
3. 테이블 확인

- H2 콘솔에 접속하여 더미 데이터가 올바르게 삽입되었는지 확인한다.
Share article