하루살이 개발자

[Spring] DB 연동 본문

Backend/Spring

[Spring] DB 연동

하루살이 2022. 5. 10. 15:04

1. 의존성 추가하기(build.gradle)

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

// mysql인 경우
implementation 'mysql:mysql-connector-java'

//h2 DB인 경우
runtimeOnly 'com.h2database:h2'

 

2. application.yml 설정

//application.yml(h2 DB인 경우)
server:
  port: 8081

spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/fyp0
    username: sa
    password:
    driver-class-name: org.h2.Driver

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

logging.level:
  org.hibernate.SQL: debug
#  org.hibernate.type: trace

 

server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/fyp_db?serverTimezone=UTC&characterEncoding=UTF-8
    username: fyp_db
    password: 1234
    driver-class-name: com.mysql.cj.jdbc.Driver

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

logging.level:
  org.hibernate.SQL: debug
#  org.hibernate.type: trace