본문 바로가기
Spring/boot

[SpringBoot] SpringBoot 에서 Jpa, H2 사용 (application.yml, application.properties)

by 무대포 개발자 2020. 7. 10.
728x90
반응형

SpringBoot 에서 Jpa, H2 사용법 (application.yml, application.properties 둘 다 있음)

  • application.properties 에 아래 설정해주기.
#DB
spring.datasource.url=jdbc:h2:file:./h2db/board
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username= 아이디
spring.datasource.password= 패스워드

#H2
spring.h2.console.path=/h2console
spring.h2.console.enabled=true

H2 console 화면 접속

  • 위와 같이 h2.console 설정해주고 SpringBoot 를 실행시킨 후, localhost:8080/h2console 로 접속하면 h2 console 화면으로 접속 가능
  • username, url 은 아래 application.yml 참조

build.gradle


plugins {  
  id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'  
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'  
  id 'java'  
}  

group = 'com.example'  
version = '0.0.1-SNAPSHOT'  
sourceCompatibility = '1.8'  

configurations {  
  compileOnly {  
  extendsFrom annotationProcessor  
   }  
}  

repositories {  
  mavenCentral()  
   maven { url 'https://repo.spring.io/milestone' }  
  maven { url 'https://repo.spring.io/snapshot' }  
}  

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

  annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'  
  annotationProcessor 'org.projectlombok:lombok'  
  testImplementation('org.springframework.boot:spring-boot-starter-test') {  
  exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'  
  }  
  implementation 'junit:junit:4.12'  
}  

test {  
  useJUnitPlatform()  
}

application.yml 설정

spring:  
  datasource:  
    hikari:  
      jdbc-url: jdbc:h2:file:./h2db/sample  
      driver-class-name: org.h2.Driver  
      username: sa  
      maximum-pool-size: 5  
      minimum-idle: 1  
      pool-name: hikari  


  h2:  
    console:  
      path: /h2console  
      enabled: true  
  jpa:  
    hibernate:  
      ddl-auto: update  
    show-sql: true  
    properties:  
      hibernate:  
        format_sql: true

댓글