Spring boot : query dsl ์„ค์ •

๊น€์•„๋ฌด๊ฐœยท2023๋…„ 7์›” 9์ผ
0

Spring Boot ๐Ÿƒ

๋ชฉ๋ก ๋ณด๊ธฐ
47/95
post-custom-banner

์ถœ์ฒ˜ : 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/generated/querydsl'
+ h2 ์ž„๋ฒ ๋“œ ์œ„์น˜ 'build/generated/h2'

// 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
์œ„์น˜์— ์ƒ์„ฑ๋œ๋‹ค.


๋ฒ„์ „ ๊ณตํ†ต ์„ค์ • ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค๋ฉด
src - main - generated - ํ”„๋กœ์ ํŠธ.entity
์œ„์น˜์— ์ƒ์„ฑ๋œ๋‹ค.


๋‹ค๋งŒ,

Qํด๋ž˜์Šค ์œ„์น˜๋ฅผ src/main/generated๋กœ ํ•œ๋‹ค๋ฉด,

.gitignore ์— ํ•ด๋‹น ์œ„์น˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์–ด ๊ด€๋ฆฌํ•ด์•ผํ•œ๋‹ค.


build ํด๋”์— ์ƒ์„ฑ๋˜๊ฒŒ ํ•˜๋ฉด

์ด๋ฏธ .gitignore์— ๋“ฑ๋ก๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— git์— ์—…๋กœ๋“œ ๋˜์ง€ ์•Š๊ฒ ์ง€๋งŒ


src ํด๋”์— ์ƒ์„ฑ๋˜๊ฒŒ ํ•˜๋ฉด git์— ์—…๋กœ๋“œ ํ•˜๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


Qํด๋ž˜์Šค๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ,
์žฆ์€ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜๋Š” ํŒŒ์ผ์ด๊ธฐ ๋•Œ๋ฌธ์— git์— ์˜ฌ๋ฆฌ์ง€ ์•Š๋Š”๊ฒŒ ์ข‹๋‹ค.


ํ•˜์ง€๋งŒ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ์„œ Qํด๋ž˜์Šค๋ฅผ Git์— ์—…๋กœ๋“œํ•˜์—ฌ ํ•จ๊ป˜ ๊ด€๋ฆฌํ•˜๋Š” ์กฐ์ง๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค!


์—ญ์‹œ ๋ชจ๋“  ๊ฒƒ์€ ์ผ€๋ฐ”์ผ€ ๐Ÿคจ








๋‚˜๋ฅผ ์œ„ํ•œ ๊ฒ€์ƒ‰์–ด
์ฟผ๋ฆฌ , ๋””์—์Šค์—˜ , ๋””์—์…€ , query , qclass , dsl , querydsl , query dsl ,

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€