νμ΄μ§ μΉ΄μ΄νΈ
, λ°μ΄ν° νμΉ
쿼리 λΆλ¦¬ μμ .
λΉλ ν, νλ‘μ νΈ μ€ν ν©λλ€. μ ν리μΌμ΄μ μ€ν ν, λ°μ΄ν° μ΄κΈ°ν μμ μ΄ μκΈ° λλ¬Έμ μ κΉ λκΈ°ν©λλ€.
./gradlew build
java -jar build/libs/paging-async-0.0.1.jar
νμ΄μ§μμ μΉ΄μ΄νΈ
μ λ°μ΄ν° νμΉ
λ μΏΌλ¦¬κ° μμ°¨μ μΌλ‘ μ€νλ©λλ€. μ΄λ₯Ό λΆλ¦¬ν ν, λΉλκΈ°λ‘ μ‘°ννλ©΄ μ‘°ν μ±λ₯μ μ‘°κΈ λ ν₯μμν¬ μ μμ΅λλ€.
@Repository
class UserEntityReadRepository(
private val queryFactory: JPAQueryFactory,
) : UserReadRepository {
companion object {
private val totalCountExpression = numberTemplate(Long::class.java, "count(1)")
}
override suspend fun findUsers(
page: Int,
size: Int,
): Pair<Long, List<User>> = coroutineScope {
val totalCount = async {
queryFactory.select(totalCountExpression)
.from(user)
.fetchOne() ?: 0L
}
val findUsers = async {
queryFactory.selectFrom(user)
.offset((page) * size.toLong())
.limit(10)
.fetch()
}
Pair(totalCount.await(), findUsers.await())
}
}
λ¨, μ΄λ offset λ°©μμ νκ³λ λ²μ΄λμ§ λͺ»νλ―λ‘, λ°μ΄ν°κ° λ§μ μλ‘ μλκ° λλ €μ§λλ€.