본문 바로가기

JPA

spring boot jpa 설정

build.gradle 에 아래 dependency 추가

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'

    // JPA
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    // lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    //MySql
    runtimeOnly 'com.mysql:mysql-connector-j'


    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

 

! java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 오류가 나는 경우

mysql dependency를 한번 더 확인하기

전에는 'com.mysql:mysql-connector-java' 였다면 이제는  'com.mysql:mysql-connector-j' 로 설정해야 함.

 

application.properties 에 DB 연결 정보 추가

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# DB Source URL
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Seoul
# DB username
spring.datasource.username=root
# DB password
spring.datasource.password=wldms

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.thymeleaf.cache=false

 

파일 생성

파일 구조

test 테이블

 

datasource.vo.Test.java

 

datasource.repository.TestRepository

 

service.TestService

 

controller.TestController

 

프로젝트 실행

 

확인

 

'JPA' 카테고리의 다른 글

[Querydsl] Connection is closed  (0) 2023.07.25
Querydsl Projection 패턴  (0) 2023.05.30
Query DSL 설정  (0) 2023.05.25