๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

STUDY/Spring

Spring Boot | Querydsl ์ถ”๊ฐ€ (Gradle 7.x)

๐Ÿ“Œ์ฐธ๊ณ !!!! [gradle] ๊ทธ๋ ˆ์ด๋“ค Annotation processor ์™€ Querydsl



Querydsl์€ JPQL ๋นŒ๋” ์˜คํ”ˆ์†Œ์Šค ํ”„๋ ˆ์ž„์›Œํฌ๋‹ค.


์ผ๋‹จ Querydsl์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.
SpringDataJPA๋„ ์ถ”๊ฐ€๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

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

    // Querydsl ์ถ”๊ฐ€ ์‹œ์ž‘
    implementation 'com.querydsl:querydsl-jpa'
}

์ด๋ ‡๊ฒŒ๋งŒ ํ•˜๋ฉด QuerydslRepositorySupport๋‚˜ QueryDslPredicateExecutor๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, Qclass๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค.
querydsl-apt๊ฐ€ ์ฟผ๋ฆฌ ํƒ€์ž…(Q)๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ๋ฐ, ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” annotationProcessor๋ฅผ ์ด์šฉํ•ด ์ถ”๊ฐ€ํ•œ๋‹ค.

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

    // Querydsl ์ถ”๊ฐ€ ์‹œ์ž‘
    implementation 'com.querydsl:querydsl-jpa'
    annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
}

๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ java.lang.NoClassDefFoundError๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด annotationProcessor๋ฅผ ๋” ์ถ”๊ฐ€ํ•œ๋‹ค.

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

    // Querydsl ์ถ”๊ฐ€ ์‹œ์ž‘
    implementation 'com.querydsl:querydsl-jpa'

    annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
    annotationProcessor "jakarta.persistence:jakarta.persistence-api"
    annotationProcessor "jakarta.annotation:jakarta.annotation-api"
    // Querydsl ์ถ”๊ฐ€ ๋
}

์ด์ œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค ์•ž์— Q๊ฐ€ ๋ถ™์€ ์ด๋ฆ„์˜ ์ฟผ๋ฆฌ ํƒ€์ž…๋“ค์ด ์ƒ์„ฑ๋œ๋‹ค.
์ด ์ฟผ๋ฆฌ ํƒ€์ž…๋“ค์ด ์ƒ์„ฑ๋˜๋Š” ์œ„์น˜๋Š” ์ธํ…”๋ฆฌ์ œ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
Preferences > Build, Execution, Deployment > Build Tools > Gradle์˜ ๊ฒฝ๋กœ๋กœ ๊ฐ”์„ ๋•Œ, Build and run using์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค.

IntelliJ IDEA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

src/main/generated๊ฒฝ๋กœ๋กœ ์ƒ์„ฑ๋œ๋‹ค.


์—”ํ‹ฐํ‹ฐ ์œ„์น˜๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋  ๊ฒฝ์šฐ ๊ธฐ์กด ์ฟผ๋ฆฌ ํƒ€์ž…(Qํด๋ž˜์Šค)๋ฅผ ์‚ญ์ œํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค!
์ด ๊ฒฝ์šฐ, ๋ณดํ†ต build.gradle์— task๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํ•ด๋‹น ํด๋ž˜์Šค๋“ค์„ ์‚ญ์ œํ•œ๋‹ค.
task๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ด ๊ธ€ ์ƒ๋‹จ์˜ ์ฐธ๊ณ  ๋งํฌ๋กœ...

Gradle์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

build/generated/sources/annotationProcessor/java/main๊ฒฝ๋กœ์— ์ƒ์„ฑ๋œ๋‹ค.


gradle clean์œผ๋กœ ์ฟผ๋ฆฌ ํƒ€์ž…์„ ์‚ญ์ œํ•œ๋‹ค!


๋‘˜ ์ค‘ ์–ด๋Š ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€ ์—†์ง€๋งŒ, ์„ค์ •์— ๋”ฐ๋ผ ์ฟผ๋ฆฌ ํƒ€์ž… ์ƒ์„ฑ ๊ฒฝ๋กœ๊ฐ€ ๋‹ฌ๋ผ์ง์— ์ฃผ์˜ํ•˜๋ฉด ๋œ๋‹ค.

IntelliJ IDEA๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข€ ๋” ๋น ๋ฅด๋‹ค๊ณ  ํ•จ..