ysk(0soo)
Lifealong
ysk(0soo)
전체 방문자
오늘
어제
  • 분류 전체보기 (238)
    • Java (50)
      • whiteship-java-study (11)
      • Java (28)
      • time (6)
    • Spring (68)
      • JPA (15)
      • Spring (1)
      • SpringBoot (1)
      • SpringMVC (6)
      • Spring Security (22)
      • Jdbc (1)
      • RestDocs (14)
      • log (6)
    • Kotlin (3)
    • Web (2)
      • nginx (1)
    • Database (14)
      • MySQL (5)
      • PostgreSQL (1)
      • SQL (1)
      • Redis (4)
    • C, C++ (0)
    • Git (1)
    • Docker (2)
    • Cloud (3)
      • AWS (3)
    • 도서, 강의 (0)
      • t5 (0)
    • 기타 (7)
      • 프로그래밍 (1)
    • 끄적끄적 (0)
    • CS (14)
      • 운영체제(OS) (2)
      • 자료구조(Data Structure) (9)
    • 하루한개 (12)
      • 우아한 테크코스-10분테코톡 (12)
    • 스터디 (12)
      • 클린 아키텍처- 로버트마틴 (2)
      • JPA 프로그래밍 스터디 (10)
    • 테스트 (34)
      • JUnit (19)
      • nGrinder (2)
      • JMeter (0)
    • Infra (3)
    • 프로그래머스 백엔드 데브코스 3기 (0)
    • 디자인 패턴 (3)
    • Issue (4)
    • system (1)
      • grafana (0)
      • Prometheus (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • github

공지사항

인기 글

태그

  • 구조화된 동시성
  • restdocs enum
  • FilterSecurityInterceptor
  • 인가(Authorization) 처리
  • AccessDecisionManager
  • java
  • 동등성
  • nGrinder
  • UserDetailsService
  • restdocs custom
  • StructuredConcorrency
  • querydsl
  • 트랜잭션
  • AuthenticationException
  • mysql
  • 가상 스레드 예외 핸들링
  • tree
  • DataJpaTest
  • AccessDecisionVoter 커스텀
  • 동시성 제어
  • junit5
  • nginx basic auth
  • VirtualThread Springboot
  • LocalDateTime
  • 정규표현식
  • 가상 스레드
  • node exporter basic auth
  • 동일성
  • jpa
  • scope value

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ysk(0soo)

Lifealong

Spring/JPA

QueryDSL 설정 - SpringBoot 2.7.X & SpringBoot 3.x 버전

2023. 1. 18. 03:11

SpringBoot 2.x대와 SpringBoot 3.x대의 버전 설정 방법이 다르므로 둘에 나눠서 정리 하도록 한다.

SpringBoot 2.x버전대 설정 방법

1. Querydsl 버전을 plugins 위에 추가하고 plugins에 querydsl 플러그인을 추가한다.

buildscript {
    ext {
        queryDslVersion = "5.0.0"
    }
}

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.7'
    id 'io.spring.dependency-management' version '1.1.0'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10" // querydsl
}
  • buildscript는 project의 플러그인 의존성(라이브러리) 관리를 위한 설정이며 ext는 build.gradle 에서 사용하는 전역 변수를 설정하겠다는 의미이다.

2 dependencies에 querydsl 관련 의존성 추가

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    ...
    implementation 'mysql:mysql-connector-java'
        ...    

    // querydsl 디펜던시 추가
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    implementation "com.querydsl:querydsl-apt:${queryDslVersion}"
}
  • 위에서 설정한 전역 변수를 통한 버전 을 명시한다.

3. querydsl 추가 설정

// querydsl 사용할 경로를 지정한다. 지정한 부분은 .gitignore에 포함되므로 git에 올라가지 않는다. 
def querydslDir = "$buildDir/generated/'querydsl'"

// JPA 사용여부 및 사용 경로 설정
querydsl {
    jpa = true
    querydslSourcesDir = querydslDir
}

// build시 사용할 sourceSet을 추가하도록 설정
sourceSets {
    main.java.srcDir querydslDir
}

// querydsl 컴파일 시 사용할 옵션 설정
compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

// querydsl이 compileClassPath를 상속하도록 설정
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    querydsl.extendsFrom compileClasspath
}
  • SourceSets은 Java 소스와 리소스 파일의 논리적인 그룹
  • 하나 이상의 Source 디렉터리를 Gradle에서 처리를 하기 위해서 SourceSets에 Source 디렉터리를 등록

최종 스크립트

buildscript {
    ext {
        queryDslVersion = "5.0.0"
    }
}

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.7'
    id 'io.spring.dependency-management' version '1.1.0'
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10" // querydsl
}

group = 'com.ys'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }

}

repositories {
    mavenCentral()
}

ext {
    set('snippetsDir', file("build/generated-snippets"))
    queryDslVersion = "5.0.0"
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    // querydsl
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    implementation "com.querydsl:querydsl-apt:${queryDslVersion}"
}

// querydsl 사용할 경로 지정. 현재 지정한 부분은 .gitignore 에 포함되므로 git 에 올라가지 않는다.
def querydslDir = "$buildDir/generated/'querydsl'"

querydsl { // JPA 사용여부 및 사용 경로 설정
    jpa = true
    querydslSourcesDir = querydslDir
}

sourceSets { // build시 사용할 sourceSet 추가 설정
    main.java.srcDir querydslDir
}

compileQuerydsl { // querydsl 컴파일 시 사용할 옵션 설정
    options.annotationProcessorPath = configurations.querydsl
}

// querydsl이 compileClassPath를 상속하도록 설정
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    querydsl.extendsFrom compileClasspath
}

compileQuerydsl.doFirst {
    if(file(querydslDir).exists() )
        delete(file(querydslDir))
}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl
}

// gradle clean 시에 QClass 디렉토리 삭제
clean {
    delete file(generated)
}

// ---- querydsl 설정 끝

IDE를 이용해서 gradle task 중 compileQuerydsl을 실행시키거나

./gradlew compileQuerydsl

명령어를 실행시켜 build해야 한다.

Entity가 변경되었다면 이 과정을 다시 수행해야 한다.

SpringBoot 3.x 버전대 설정

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.0'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.ys'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

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

      // querydsl 설정    
    implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
      annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
      annotationProcessor "jakarta.annotation:jakarta.annotation-api"
      annotationProcessor "jakarta.persistence:jakarta.persistence-api"

}

tasks.named('test') {
    useJUnitPlatform()
}

// Querydsl 설정부
def querydslDir = 'src/main/generated'

// querydsl QClass 파일 생성 위치를 지정
tasks.withType(JavaCompile) {
    options.getGeneratedSourceOutputDirectory().set(file(generated))
}

// java source set 에 querydsl QClass 위치 추가
sourceSets {
    main.java.srcDirs += [ generated ]
}

// gradle clean 시에 QClass 디렉토리 삭제
clean {
    delete file(querydslDir)
}

스프링 부트 3.0 부터는 javax 패키지가 jakarta로 변경되었으므로 의존성을 jakarta로 가져와야 한다.

rc/main/generated에 QClass가 Git에 올라가지 않도록 .gitignore에 추가해야 한다

**/generated
저작자표시 비영리 (새창열림)

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

Querydsl Projection - with 1:N DTO 매핑 (프로젝션)  (0) 2023.01.31
QueryDSL 에러 - Execution failed for task ':compileQuerydsl', ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain  (0) 2023.01.18
Jpa Insert, update delete 시 select query를 통한 먼저 조회하지 않는 방법.md  (1) 2023.01.18
Jpa Id 생성전략에 따른 동작 방식  (0) 2023.01.18
JPA Hibernate Id 생성 전략과 UUID 전략  (0) 2023.01.18
    'Spring/JPA' 카테고리의 다른 글
    • Querydsl Projection - with 1:N DTO 매핑 (프로젝션)
    • QueryDSL 에러 - Execution failed for task ':compileQuerydsl', ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
    • Jpa Insert, update delete 시 select query를 통한 먼저 조회하지 않는 방법.md
    • Jpa Id 생성전략에 따른 동작 방식
    ysk(0soo)
    ysk(0soo)
    백엔드 개발을 좋아합니다. java kotlin spring, infra 에 관심이 많습니다. email : kim206gh@naver.com github : https://github.com/devysk

    티스토리툴바