JPA
[Querydsl] 스프링 데이터 페이징
주씨.
2024. 4. 20. 21:13
728x90
# 스프링 데이터의 Page, Pageable 활용
@Override
public Page<MemberTeamDto> searchPage(MemberSearchCondition condition, Pageable pageable) {
List<MemberTeamDto> content = queryFactory
.select(new QMemberTeamDto(
member.id,
member.username,
member.age,
team.id,
team.name))
.from(member)
.leftJoin(member.team, team)
.where(usernameEq(condition.getUsername()),
teamNameEq(condition.getTeamName()),
ageGoe(condition.getAgeGoe()),
ageLoe(condition.getAgeLoe()))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
JPAQuery<Long> countQuery = queryFactory
.select(member.count())
.from(member)
.leftJoin(member.team, team)
.where(
usernameEq(condition.getUsername()),
teamNameEq(condition.getTeamName()),
ageGoe(condition.getAgeGoe()),
ageLoe(condition.getAgeLoe())
);
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}
- 인터넷에 돌아다니는 레퍼런스 중, fetchResults() 와 fetchCount() 를 사용하는 경우가 있는데, 이 두 메서드는 Querydsl 5.0.0 버전에서부터 deprecated 되었다.
http://localhost:8080/v3/members?size=5&page=2