지금 사용하는 DB 설정 알고 쓰기 위한 기록
의존성
# h2 데이터베이스
runtimeOnly 'com.h2database:h2'
# MySQL
runtimeOnly 'com.mysql:mysql-connector-j'
application.properties 파일에서 db관련 설정
# DataBase
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.format_sql=true
# MySQL DataBase
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://
spring.datasource.username=
spring.datasource.password=
## h2 DataBase
#spring.datasource.driver-class-name=org.h2.Driver
#spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL;
#spring.h2.console.enabled=true
#spring.h2.console.settings.web-allow-others= true
#spring.datasource.path=/h2-console
1. spring.jpa.database=mysql
작동할 대상 데이터베이스 기본적으로 자동 감지됨 또는 "databasePlatform" 속성을 사용하여 설정할 수 있음
2 spring.jpa.hibernate.ddl-auto
DDL 모드. 이것은 실제로 "hibernate.hbm2ddl.auto" 속성에 대한 바로 가기입니다. 임베디드 데이터베이스를 사용하고 스키마 관리자가 감지되지 않은 경우 기본값은 "create-drop"입니다. 그렇지 않으면 기본값은 "none"
-> 데이터베이스 초기화 전략 설정
1) none : 실행 하지 않음
2) create-drop : SessionFactory가 시작될 때 drop 및 생성을 실행하고, SesisonFactory가 종료될 때 drop을 실행
3) create : SessionFactory가 시작될 때 drop을 실행하고 생성된 DDL 실행
4) update: 변경된 부분만 반영
5) validate : 변경된 스키마가 있다면 변경점을 출력하여 정상 매핑되었는지 확인 후 어플리케이션을 종료
3. spring.jpa.show-sql
SQL 문법 로깅을 활성화할지 여부
-> true로 설정 시 sql문을 로그에서 확인 가능
4. spring.jpa.open-in-view
OpenEntityManagerInViewInterceptor를 등록. 요청의 전체 처리를 위해 JPA EntityManager를 스레드에 바인딩
5. spring.jpa.properties.hibernate.format_sql
-> true 설정 시 만들어진 sql문을 줄바꿈을 해서 보기 편하게 출력
cf) spring.jpa.properties.* : JPA 공급자에 설정할 추가 기본 속성
# MySQL DataBase
#1
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#2 접속할 URL
spring.datasource.url=jdbc:mysql://
#3 데이터베이스 계정
spring.datasource.username=
spring.datasource.password=
#h2 DataBase
#1
spring.datasource.driver-class-name=org.h2.Driver
#2 h2로 접속할 URL, mem(메모리) db명: testdb
# 메모리가 아닌 파일로 DB 관리 시 spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL;
#3 h2 콘솔 사용 'true'
spring.h2.console.enabled=true
#4 콘솔 접속 경로
spring.datasource.path=/h2-console
#5 접속 경로
spring.h2.console.settings.web-allow-others= true
레퍼런스
Common Application Properties
docs.spring.io
JPA - OSIV(Open Session In View) 정리
OSIV(Open Session In View) OSIV(Open Session In View)는 영속성 컨텍스트를 뷰까지 열어두는 기능이다. 영속성 컨텍스트가 유지되면 엔티티도 영속 상태로 유지된다. 뷰까지 영속성 컨텍스트가 살아있다면
ykh6242.tistory.com
'Spring > JPA' 카테고리의 다른 글
📝 ORM, JPA, Spring Data JPA 정리 (1) | 2022.11.18 |
---|---|
📝 JPA 가 무엇인가요? (0) | 2022.11.16 |
Spring JPA - 리포지토리 인스턴스 생성 (0) | 2022.10.14 |
Spring JPA - 비동기 쿼리 결과 (0) | 2022.10.14 |
Spring JPA - 스트리밍 쿼리 결과 (0) | 2022.10.14 |