[DB]/DB

[DB] 스프링 부트의 다양한 DB 연동 방법

쿠릉쿠릉 쾅쾅 2022. 3. 31. 19:45
728x90

 

Maria DB

MariaDB Java Client 최신 버전 조회 사이트

https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client

📌 build.grable

dependencies {
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // jpa
}
  • runtimeOnly :실행 시점에만 필요한 라이브러리

📌 application.yml

spring:
  datasource:
    url: jdbc:mariadb://127.0.0.1:3306/[DB이름]
#    url: jdbc:mariadb://localhost:3306/[DB이름]
    driver-class-name: org.mariadb.jdbc.Driver
    username: [DB유저]
    password: [비밀번호]

  jpa:
    database-platform: org.hibernate.dialect.MySql5InnoDBDialect  # JPA 데이터베이스 플랫폼 지정
    hibernate:
      ddl-atuo: none  # none(기본값) / create / create-drop / validate / update
    properties:
      hibernate:
#        show_dql: true  # 콘솔이 JPA 실행 쿼리를 출력
      format_sql: true

# 로깅 설정
logging:
  level:
    org.hibernate:
      type.descriptor.sql: trace #show parameter binding
      SQL: DEBUG

 

 


 

H2 연동

📌 build.gradle

dependencies {
    runtimeOnly 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // jpa
}

📌 application.yml

spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/[DB이름];
    username: sa
    password:
    driver-class-name: org.h2.Driver

  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        format_sql: true

logging:
  level:
    org.hibernate.SQL: debug

'jpa.properties.hibernate.show_sql=true' 는 쓰지 말 것. 콘솔창에 출력되기 때문이다.
loggoin을 통해 로그로 찍어야 한다.

 

 

 

 

 

 


👀 참고 자료

https://wecandev.tistory.com/71

 

[Spring Boot] MariaDB + JPA 연동하기

Spring Boot+JPA+MariaDB 시작하기 Spring Boot + JPA + mariaDB 조합 프로젝트 설정을 정리하고자 한다. https://start.spring.io/ 에서 dependency를 추가하여 프로젝트를 생성한 뒤 적절하게 DB 및 logging 설..

wecandev.tistory.com

 

728x90