application.yml ?

김창모·2023년 5월 30일
0

SpringBoot

목록 보기
12/19

지난 글에서 H2 DB 에 대한 설정과 jpa 에 대한 설정을 따로 정리한다고 하였다.
SpirngBoot 에서는 기본적인 구성 세팅을 프로퍼티에서 설정할수 있다.
datasource jpa 설정등을 알아보자.

SpringBoot 의 일반적인 관행은 외부 구성을 사용하여 속성을 정의하는 것이다.
이를 통해 다른 환경에서 동일한 애플리케이션 코드를 사용할수 있다.

속성 구성

기본적으로 SpringBoot 는 Key - Value 형식을 사용하는 application.properties 파일에 구성된 설정을 엑세스 할수 있다.

.properties

spring.datasource.url=jdbc:h2:mem:mong
spring.datasource.username=mong
spring.datasource.password=null
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql=true

위의 .properties 는 아래와 같은 .yml 로 변경할수 있다.

.yml

spring:
  datasource:
    url: jdbc:h2:mem:mong
    username: mong
    password:
    driver-class-name: org.h2.Driver


  h2:
    console:
      enabled: true

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

yaml converter

각자 더 편리한 사용법을 사용하면 되고 이를 변환해주는 Yaml Converter 도 존재한다.

속성

이제 각 키 밸류의 값이 의미하는게 무엇인지 알아보자.
( yml 파일을 기준으로 하겠다. )

Spring

스프링에 대한 설정을 할것이다.

datasource

datasource 에 대한 설정을 할것이다.

url

접속 url 을 설정한다.

username

h2 DB 에 로그인 하기위한 username 을 설정한다.

password

h2 DB에 로그인 하기위한 password 를 설정한다. 값을 입력하지 않아도 된다.

driver-class-name

사용할 DB Driver 를 설정해준다.

h2 console enabled

h2 console 의 사용 여부에 대한 설정이다.
localhost:8080/h2-console 로 접근 가능하며
위에서 설정한 url,username,password 로 로그인 가능하다.

jpa

jpa 에 대한 설정을 할것이다.

hibernate ddl-auto

Entity객체를 참고하여 애플리케이션 실행 시점에 하이버네이트에서 자동으로 DDL을 만들어주는 옵션이다.

  • create: 기존테이블 삭제 후 다시 생성 + 닫을 때 삭제하지 않는다.
  • create-drop: create와 같으나 종료시점에 테이블 DROP
  • update: 하이버네이트는 주어진 엔티티 구조에 따라서 데이터베이스를 변경한다.
  • validate: 엔티티와 테이블이 정상 매핑되었는지만 확인
  • none: 어떠한 변화도 주지 않는다.mysql에서 default이다.

properties hibernate show_sql

hibernate 가 DB에 보내는 모든 쿼리를 보여준다.

Hibernate: select member0_.id as id1_0_0_, member0_.email as email2_0_0_, member0_.name as name3_0_0_, member0_.password as password4_0_0_ from member member0_ where member0_.id=?

properties hibernate format_sql

hibernate 가 DB에 보내는 모든 쿼리를 조금더 보기 쉽게 보여준다.

Hibernate: 
    insert 
    into
        member
        (id, email, name, password) 
    values
        (default, ?, ?, ?)

0개의 댓글