JeongJin's Blog

06. 데이터베이스 연동 (4) 본문

Book Study/스프링 부트 핵심 가이드

06. 데이터베이스 연동 (4)

정진킴 2023. 10. 30. 09:03

6.6 데이터베이스 연동

6.6.1 프로젝트 생성

  • 자바 : Java11
  • 스프링 부트 : 2.x.x (툴에서 조회는 최신버전)
  • groupId : com.springboot
  • name: jpa
  • 라이브러리
    • Developer Tools: Lombok, Spring Configuration Processor
    • Web: Spring Web
    • SQL: Spring Data JPA, MariaDB Driver
  • 소스 코드
    • com.springboot.jpa/config/SwaggerConfiguration.java
    • build.bundle 에 swagger dependencies 추가
    • 데이터 베이스 연동을 위해 application.properties 작성
    • Swagger 설치 링크
# application.properties

spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:23306/springboot
spring.datasource.username=[dbms user id]
spring.datasource.password=[dbms user password]
spring.datasource.hikari.maximum-pool-size=20

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

spring.mvc.pathmatch.matching-strategy=ant_path_matcher
  • spring.jap.hibernate.ddl-auto 옵션
    • create: 애플리케이션이 기동되고 SessionFactory가 실행될 때 기존 테이블을 삭제하고 새로 생성한다.
    • create-drop: create 와 동일한 기능이지만 Application 을 종료하는 시점에 테이블을 삭제한다.
    • update: SessionFactory가 실행될 때 객체를 검사해서 변경된 스키마를 갱신한다. 기존에 저장된 데이터는 유지된다.
    • validate: update 처럼 객체를 검사하지만 스키마는 건드리지 않는다. 검사 과정에서 데이터베이스의 테이블 정보와 객체의 정보가 다르면 에러를 발생, 실무에서는 해당 옵션을 사용한다.
    • none: ddl-auto 기능을 사용하지 않는다. 실무에서는 해당 옵션을 사용한다.
  • spring.jpa.show-sql : 하이버네이트가 생성한 쿼리 출력하지만 한 줄로 출력된다.
  • spring.jpa.properties.hibernate.format_sql : 출력된 쿼리를 사람이 보기 좋게 포맷팅
# build.bundle

dependencies {
	implementation 'io.springfox:springfox-boot-starter:3.0.0'
	implementation 'io.springfox:springfox-swagger-ui:3.0.0'
}

 

swagger

  •  리소스
    • resources/logback-spring.xml