본문 바로가기
STUDY/JPA

스프링부트2 + 그래들 + JPA 환경설정

by Anne of Green Galbes 2020. 1. 22.

1. 프로젝트 생성

1) Spring Initializr 프로젝트 생성

 

2) 프로젝트 기본 설정

 ■ Gradle로 빌드하므로 Type을 Gradle Project로 설정한다.

 - Group : 기본 패키지명

 - Artifact : 프로젝트 명

 - Type : 빌드 타입

 - Language : 사용 언어

 - Packaging : 패키징 방법

 - Java Version : 자바 버전

 

3) 의존성 추가

 

4) 프로젝트 이름 및 경로 설정

 

5) 그래들 임포트

 

 

2. 빌드 오류

[error 메시지]

Plugin [id: 'org.springframework.boot', version: '2.1.7.RELEASE'] was not found in any of the following sources:

 

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)

- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.1.7.RELEASE')

  Searched in the following repositories:

    Gradle Central Plugin Repository

 

[해결]

build.gradle 파일에서

id 'org.springframework.boot' version '2.2.4.RELEASE' ▶ id 'org.springframework.boot' version '2.2.2.RELEASE' 변경

 

 

3. 프로젝트 빌드 완료

build.gradle

 

 

4. MySql 연동

프로젝트 생성시 SQL > MySql Driver 의존성 추가

build.gradle 의존성 추가

implementation 'org.springframework.boot:spring-boot-starter'
runtimeOnly 'mysql:mysql-connector-java:5.1.47'
testImplementation('org.springframework.boot:spring-boot-starter-test') {   
     exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

1) yml 파일 생성

기존에 있던 src > main > resources > application > property 파일 삭제

위치 : src > main > resources > config > application.yml

yml 파일 경로

 

2) yml 작성

spring:
   jpa:   
      database-platform: org.hibernate.dialect.MySQL5InnoDBDialect   
      open-in-view: false   
      show-sql: true   
      hibernate:     
         format_sql: true     
         ddl-auto: update

   datasource:   
      driver-class-name: com.mysql.jdbc.Driver   
      url: jdbc:mysql://IP:포트번호/DB명?useUnicode=true&characterEncoding=utf-8   
      username: 사용자   
      password: 비밀번호

logging:
   config: classpath:logback.xml

log4j:
   logger:   
      jdbc:     
         sqltiming: DEBUG   
      package:     
         path: TRACE   
      java:     
         sql:       
            Connection: DEBUG         
            Statement: DEBUG       
            PreparedStatement: DEBUG       
            ResultSet: DEBUG

① spring.datasource

  - datasouce는 MySQL 설정과 관련된 것이다.

② spring.jpa.database-platform

  - JPA 데이터베이스 플랫폼을 지정한다.

  - 예제에서는 MySQL InnoDB를 사용하도록 설정한다.

③ spring.jpa.open-in-view

  - OSIV(Open Session In View)는 웹 요청이 완료될 때까지 동일한 EntityManager를 갖도록 해줍니다.

  - 스프링부트에서 OSIV가 기본값으로 true인데, 성능과 확장상 면에서 안좋다고 해서 false로 설정을 껏습니다.

④ spring.jpa.show-sql

  - 콘솔에 JPA 실행 쿼리를 출력합니다.

⑤ spring.jpa.hibernate.format_sql

  - 콘솔에 출력되는 JPA 실행 쿼리를 가독성있게 표현합니다.

⑥ spring.jpa.hibernate.ddl_auto

  - 데이터베이스 초기화 전략을 설정합니다.

  - none : 아무것도 실행하지 않습니다.

  - create : SessionFactory가 시작될 때 기존테이블을 삭제 후 다시 생성합니다.

  - create-drop : create와 같으나 SessionFactory가 종료될 때 drop을 실행합니다.

  - update : 변경된 스키마만 반영합니다.

  - validate : 엔티티와 테이블이 정상적으로 매핑되었는지만 확인합니다.

 

 

5. 처음 프로젝트 loccal로 창 띄울 경우(window)

경로 : .idea > workspace.xml

property 검색 후 <property name="dynamic.classpath" value="true" /> 추가 

 

 

6. 프로젝트 실행

 

7. yml 파일 추가 작성

1) 서버 정보 추가

server:
   port: 8080
   servlet:   
      session:     
         timeout: 38000     
         persistent: true

 

2) 메일 설정

spring:
   mail:   
      default-encoding: UTF-8   
      host: smtp.gmail.com   
      port: 587   
      protocol: smtp   
      username: gmail 이메일   
      password: 메일 비민번호   
      properties.mail.smtp:     
         auth: true     
         starttls.enable: true     
         ssl.trust: smtp.gmail.com

yml 파일에서 STARTTLS command first 설정

https://stackoverflow.com/questions/41351540/configure-smtp-host-using-yaml-file-in-spring-boot

'STUDY > JPA' 카테고리의 다른 글

엔티티 매핑 #1  (0) 2020.01.28
영속성 관리  (0) 2020.01.28
엔티티 매니저 설정, 트랜잭션 관리, JPQL  (0) 2020.01.23
JPA 객체 매핑  (0) 2020.01.23
JPA 소개  (0) 2020.01.23

댓글