1. SpringData ์ฟผ๋ฆฌ
SpringData Common์ CrudRepository์ PagingAndSortingRepository ์ฟผ๋ฆฌ๊ธฐ๋ฅ์ ์ ๊ณต
์ฌ์ฉ ๋ฐฉ๋ฒ
- ํ๋ก๊ทธ๋๋ฐ๋์ด ์ ๊ณต๋๋ ์ฟผ๋ฆฌ๋ช
๊ท์น
- ๋ฆฌํดํ์ {์ ๋์ด}{๋์ ๋ถ}By{ํ๋กํผํฐ ํํ์}(์กฐ๊ฑด์)(And|Or){ํ๋กํผํฐ ํํ์}(์กฐ๊ฑด์) (๋งค๊ฐ๋ณ์...)
| ์ ๋์ด | Find, Get, Query, Count, ... |
| ๋์ ๋ถ | Distinct, First(N), Top(N) |
| ํ๋กํผํฐ ํํ์ | Person.Address.ZipCode => find(Person)ByAddress_ZipCode(...) |
| ์กฐ๊ฑด์ | IgnoreCase, Between, LessThan, GreaterThan, Like, Contains, ... |
| ์ ๋ ฌ ์กฐ๊ฑด | OrderBy{ํ๋กํผํฐ}Asc |
| ๋ฆฌํด ํ์ | E, Optional<E>, List<E>, Page<E>, Slice<E>, Stream<E> |
| ๋งค๊ฐ๋ณ์ | Pageable, Sort |
2. QueryDSL
QueryDSL์ Predicate ์ธํฐํ์ด์ค๋ก ์กฐ๊ฑด๋ฌธ์ ์ฌ๋ฌ ๊ฐ ๊ตฌ์ฑํ์ฌ ๋ฐ๋ก ๊ด๋ฆฌํ ์ ์๋ค
~ ์ฃผ๋ก findOne(Predicate), findAll(Predicate) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค
- findOne(Predicate) : Optional<T> ๋ฆฌํด
- findAll : List<T> | Page<T> | Iterable<T> | Slice<T> ๋ฆฌํด
Type Safe ๊ธฐ๋ฅ
์กฐ๊ฑด๋ฌธ ๊ตฌ์ฑ ์ ์ฌ์ฉํ๋ ๊ฐ์ฒด, ํ๋ ์กฐ๊ฑด์ด ์ค์ ํ์ ๊ณผ ์ผ์นํ๋ ์ง ์ฒดํฌํด์ค๋ค
์ฅ์
1. ๋ฌธ์๊ฐ ์๋ ์ฝ๋๋ก ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ฏ๋ก, ์ปดํ์ผ ์ ๋ฌธ๋ฒ ์ค๋ฅ๋ฅผ ์ฝ๊ฒ ํ์ธํ ์ ์๋ค
2. ์๋ ์์ฑ ๋ฑ IDE์ ๋์์ ๋ฐ์ ์ ์๋ค
3. ๋์ ์ธ ์ฟผ๋ฆฌ ์์ฑ์ด ํธ๋ฆฌํ๋ค
4. ์ฟผ๋ฆฌ ์์ฑ ์ ์ ์ฝ ์กฐ๊ฑด ๋ฑ์ ๋ฉ์๋ ์ถ์ถ์ ํตํด ์ฌ์ฌ์ฉํ ์ ์๋ค.
์๋ฆฌ
QueryDSL ์์กด์ฑ์ ์ถ๊ฐํ๋ฉด SpringData์ ์ํด QueryDslPreficateExecutor ์ธํฐํ์ด์ค๊ฐ ์ถ๊ฐ๋๋ค
-> QueryDslPreficateExecutor ์ธํฐํ์ด์ค : Repository๊ฐ QueryDSL์ ์คํํ ์ ์๋๋ก ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค
์ฌ์ฉ ๋ฐฉ๋ฒ
~ Spring 2.x : queryDSL ์์กด์ฑ, ์์ค ๋๋ ํ ๋ฆฌ ์ค์ , annotation ํ๋ก์ธ์ ์ค์ , Task ์ ์๋ฅผ ํด์ผ ํ๋ค
plugins {
...
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
compileQuerydsl{
options.annotationProcessorPath = configurations.querydsl
}
configurations {
...
querydsl.extendsFrom compileClasspath
}
def querydslSrcDir = 'src/querydsl/generated'
querydsl {
library = "com.querydsl:querydsl-apt"
jpa = true
querydslSourcesDir = querydslSrcDir
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', querydslSrcDir]
}
}
}
project.afterEvaluate {
project.tasks.compileQuerydsl.options.compilerArgs = [
"-proc:only",
"-processor", project.querydsl.processors() +
',lombok.launch.AnnotationProcessorHider$AnnotationProcessor'
]
}
dependencies {
implementation("com.querydsl:querydsl-jpa") // querydsl
implementation("com.querydsl:querydsl-apt") // querydsl
...
}
gradle ๋น๋ ์คํฌ๋ฆฝํธ ์๋ฆฌ
- Gradle ๋น๋์์ QueryDSL์ ํ๋ก์ ํธ ๋ด์ **@Entity** ์ด๋ ธํ ์ด์ ์ ์ ์ธํ ํด๋์ค๋ฅผ ํ์ํ๊ณ , **JPAAnnotationProcessor**๋ฅผ ์ฌ์ฉํด Qํด๋์ค๋ฅผ ์์ฑํฉ๋๋ค.
- **querydsl-apt**๊ฐ **@Entity** ๋ฐ **@Id** ๋ฑ์ ๋งคํ์ ๋ณด Annotation์ ์ ์ ์๋๋ก, **javax.persistence**๊ณผ **javax.annotation**์ annotationProcessor์ ํจ๊ป ์ถ๊ฐํฉ๋๋ค.
- **annotationProcessor**๋ Java ์ปดํ์ผ๋ฌ ํ๋ฌ๊ทธ์ธ์ผ๋ก์, ์ปดํ์ผ ๋จ๊ณ์์ ์ด๋ ธํ ์ด์ ์ ๋ถ์ ๋ฐ ์ฒ๋ฆฌํจ์ผ๋ก์จ ์ถ๊ฐ์ ์ธ ํ์ผ์ ์์ฑํฉ๋๋ค.
- ๊ฐ๋ฐ ํ๊ฒฝ์์ ์์ฑ๋ Qํด๋์ค๋ฅผ ์ธ์งํ ์ ์๋๋ก /build/generated ๋๋ ํ ๋ฆฌ๋ฅผ ํ๋ก์ ํธ์ sourceSet์ ์ถ๊ฐํฉ๋๋ค.
- ์ด๋ก์จ, IDE ์์๋ ์์ฑ๋ Qํด๋์ค ํ์ผ์ ์ธ์ํ์ฌ ๊ฐ๋ฐํ ์ ์์ต๋๋ค.
Spring 3.x ~ : ์์กด์ฑ ์ถ๊ฐ Only
// application.yml
dependencies {
....
// 9. QueryDSL ์ ์ฉ์ ์ํ ์์กด์ฑ (SpringBoot3.0 ๋ถํฐ๋ jakarta ์ฌ์ฉํด์ผํจ)
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"
}
Join์ด ํ์ํ ๊ฒฝ์ฐ์ ์ฌ์ฉํ ์ ์๋ค
- ์ ์ฒด ์ฑ๋์์ ๋ด๊ฐ ๋ฉ์
๋ ์ฐ๋ ๋ ๋ชฉ๋ก ์กฐํ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
- Mention ์ํฐํฐ ์์ฑ
- User - Mention - Thread ๋ค๋๋ค ์ฐ๊ด๊ด๊ณ ์ค์
- User, Mention ์กฐ๊ฑด์ผ๋ก Thread ๋ชฉ๋ก ์กฐํ ์ฟผ๋ฆฌ์ํ
~ QueryPredicatieExecutor๋ก๋ Join ์ฐ์ฐ์ด ๋ถ๊ฐ๋ฅํ์ฌ ๊ตฌํ ๋ถ๊ฐ๋ฅ : ๋ฉค๋ฒ ์ปฌ๋ ์ ๊น์ง๋ง ์กฐํ ๊ฐ๋ฅํ๋ค
QuerydslPredicateExecutor์ ๋จ์ : ๋ฌต์์ ์กฐ์ธ( 1 Depth ์๋ ์กฐ์ธ) ~ Channel์ Thread๊น์ง๋ง ์กฐํํ ์ ์๋ค
<-> Join ์ฐ์ฐ์ด ์ํ๋๋ ๋ช ์์ ์กฐ์ธ(2 Depth ์ด์ ์กฐ์ธ)
Join ์ด ์๋ ๋์ ์กฐ๊ฑด์ด ๋ง์ ์ฟผ๋ฆฌ
ํด๋น ์ฑ๋์์ ๋ฉ์ธ์ง๊ฐ ์๋ ์ฐ๋ ๋ ๋ชฉ๋ก ์กฐํ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ ~
Channel ์กฐ๊ฑด์ผ๋ก ๋ฉ์ธ์ง ๋ณธ๋ฌธ์ด ์๋ Thread ๋ชฉ๋ก ์กฐํ ์ฟผ๋ฆฌ์ํ
Join ์์ด ์กฐ๊ฑด์ด ๋ง์ด ์ถ๊ฐ๋ ์๋ก **QuerydslPredicateExecutor** ๋ฅผ ํ์ฉํ ์ ์๋ค.
~ ์ฌ์ค ํ์ ์์๋ ์๊ฑด ์ ์์ฐ๊ณ JPAQueryFactory ๋ฅผ ์ฃผ๋ก ์ด๋ค
'Spring๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Spring Data JPA : Auditing & HATEOAS (0) | 2023.02.13 |
|---|---|
| T ์์นด๋ฐ๋ฏธ : JPA ํ๋ก๊ทธ๋๋ฐ ๊ธฐ์ด (3) (0) | 2023.02.10 |
| SpringData JPA ํ์ด์ง๊ณผ ์ ๋ ฌ (0) | 2023.02.03 |
| SpringData ๊ตฌ์กฐ ๋ฐ JpaRepository ์๋ฆฌ (0) | 2023.02.03 |
| Raw JPA (0) | 2023.02.02 |