![[블로그 만들기] 10. DB 초기화 설정하기](https://image.inblog.dev?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%255B%25EB%25B8%2594%25EB%25A1%259C%25EA%25B7%25B8%2520%25EB%25A7%258C%25EB%2593%25A4%25EA%25B8%25B0%255D%252010.%2520DB%2520%25EC%25B4%2588%25EA%25B8%25B0%25ED%2599%2594%2520%25EC%2584%25A4%25EC%25A0%2595%25ED%2595%2598%25EA%25B8%25B0%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3DCoding_study&w=2048&q=75)
1. DB 폴더 생성하고 data.sql 파일 생성하기

2. 테이블을 만드는 쿼리를 적어 놓기
→ 실행될 때마다 테이블이 만들어지고 insert가 되어 동일한 데이터가 생성됨
→ 테스트할 때 joinform이 아니라 loginform에서 바로 할 수 있음
insert into user_tb(username, password, email, created_at) values('ssar', '1234', 'ssar@nate.com', now()); insert into user_tb(username, password, email, created_at) values('cos', '1234', 'cos@nate.com', now());
3. dev에가서 설정 추가하기
server: servlet: encoding: charset: utf-8 # 문자 인코딩을 UTF-8로 설정 force: true session: # session 유지 시간 설정 timeout: 30m port: 8080 spring: mustache: servlet: expose-session-attributes: true # session에 속성 노출 설정 expose-request-attributes: true # session에 요청 속성 노출 설정 datasource: # H2 데이터베이스를 사용하는 설정 driver-class-name: org.h2.Driver url: jdbc:h2:mem:test;MODE=MySQL # 연결URL ; H2를 MySQL과 호환되도록 설정 username: sa # 사용자 이름 password: # 사용자 비밀번호 h2: console: enabled: true # 웹에서 콘솔에 접근할 수 있게 활성화 sql: init: data-locations: - classpath:db/data.sql #리소스 폴더(로케이션 파일이 여러개 일 수 있음)를 가리킴 jpa: hibernate: ddl-auto: create show-sql: true properties: hibernate: format_sql: true # 표기 방법 변경 defer-datasource-initialization: true # 데이터 소스 초기화를 지연

Share article