JPA

[ JPA ] JPA와 MySQL 연동하기

숑숑이~~ 2022. 1. 4. 21:37

 

 

 

 

 

 

 

 

 

JPA 정리내용 전체목록

 

 

 

 

 

 

 

 

 

 

JPA와 MySQL 라이브러리 추가

 

설정 할 파일명

프로젝트에 있는 build.gradle 파일에 설정해 준다.  

 

Gradle에 의존성 주입

build.gradle 파일의 dependentcies부분에 아래의 내용을 추가한다. 

// MySQL과 JPA
runtimeOnly 'mysql:mysql-connector-java'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

추가한 결과

 

 

 

 

 

 

 

 

 

 

데이터베이스 접속정보 설정하기

 

설정할 파일명

프로젝트에 있는 application.properties 파일에 설정해 준다. 

경로는 src > main > resources > application.properties 이다. 

 

파일에 데이터베이스 접속 정보 입력하기.

아래 데이터베이스 접속 정보는 MySQL 기준이다. 

 

// 데이터베이스 접속 정보 
spring.datasource.driver=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://아이피주소:3306/데이터베이스스키마?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=아이디
spring.datasource.password=비밀번호

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

위 코드 중 아래의 항목들을 자신의 환경에 맞게 입력해준다. 

아이피주소 :  접속할 DB의 IP주소

                  ( ※ datasource.url이 맞고 datasource.jdbc-url는 잘못됐다. )

데이터베이스스키마 :  접속할 DB의 스키마명

아이디 :  DB접속 아이디

비밀번호 :  DB접속 비밀번호

spring.jpa.show-sql=true :  쿼리를 보여주는 설정이다. 
spring.jpa.hibernate.ddl-auto=none :  테이블을 자동으로 생성하지 않게 하는 설정이다. 

 

 

 

 

 

 

 

 

 

 

반응형