Today
-
Yesterday
-
Total
-
  • Spring boot : query dsl 설정
    Spring Boot 🍃 2023. 12. 7. 00:01

    출처 : github.com/djkeh

    Spring boot 2.7

    dependencies {
      implementation "com.querydsl:querydsl-jpa:${dependencyManagement.importedProperties['querydsl.version']}"
      
      // 아래 두 줄은 심화 기능이 필요할 경우 사용
      // implementation "com.querydsl:querydsl-core"
      // implementation "com.querydsl:querydsl-collections"
     
      annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
      annotationProcessor "jakarta.annotation:jakarta.annotation-api" 
      annotationProcessor "jakarta.persistence:jakarta.persistence-api" 
    }

    Spring boot 3.0 ~

    dependencies {
      implementation "com.querydsl:querydsl-jpa:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
      
      // 아래 두 줄은 심화 기능이 필요할 경우 사용
      // implementation "com.querydsl:querydsl-core"
      // implementation "com.querydsl:querydsl-collections"
     
      annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
      annotationProcessor "jakarta.annotation:jakarta.annotation-api" 
      annotationProcessor "jakarta.persistence:jakarta.persistence-api" 
    }

    버전 공통 설정

    build.gradle 파일의 끝부분에 추가해 주면 된다.

    동작 확인 : spring boot 2.7.14 , 3.0.7

    + qclass 위치를 build 하위로 지정하면 git에 올라가는 것을 방지하기 위해 .gitignore에 명시하는 작업을 건너뛸 수 있다는 장점이 있다.

     

    // q 클래스 위치 지정을 위한 설정
    def generated = 'build/generated/querydsl'
    // def generated = 'src/main/generated' 
     
    tasks.withType(JavaCompile).configureEach {
        options.getGeneratedSourceOutputDirectory().set(file(generated))
    }
     
    sourceSets {
        main.java.srcDirs += [ generated ]
    }
     
    clean {
        delete file(generated)
    }

     

    위에 작성된 버전 공통 설정을 작성해주지 않아도 실행이 잘 된다.

    QClass 생성 관리 위치를 지정해주는 코드이기 때문이다.

    버전 공통 설정 코드를 작성하지 않으면 Q클래스 파일은 /build/generated/sources/annotationProcessor/java/main/프로젝트.entity 위치에 생성된다.

    버전 공통 설정 코드를 작성해 준다면 /build/generated/querydsl/프로젝트.entity 위치에 생성된다.

    만약 Q클래스 위치를 src/main/generated로 한다면 .gitignore 에 해당 위치를 작성해 주어 관리해야 한다. build 폴더에 생성되게 하면 이미 .gitignore에 등록되어 있기 때문에 git에 업로드되지 않겠지만, src 폴더에 생성되게 하면 git에 업로드하려고 하기 때문이다. Q클래스는 프레임워크가 관리하는 잦은 변경이 발생하는 파일이기 때문에 git에 올리지 않는 게 좋다. 하지만 경우에 따라서 Q클래스를 Git에 업로드하여 함께 관리하는 조직도 있다고 한다!

Designed by Tistory / Custom by 얼거스