content, count 쿼리를 가져오는데 때에 따라서 count 쿼리를 생략할 수 있다.
스프링 데이터에서 기가 막힌 기능을 제공해 준다.
@Override
public Page<MemberTeamDto> searchPageComplex(MemberSearchCondition condition, Pageable pageable) {
List<MemberTeamDto> content = queryFactory
.select(new QMemberTeamDto(
member.id.as("memberId"),
member.username,
member.age,
team.id.as("teamId"),
team.name.as("teamName")
))
.from(member)
.leftJoin(member.team, team)
.where(
usernameEq(condition.getUsername()),
teamNameEq(condition.getTeamName()),
ageBetween(condition.getAgeLoe(), condition.getAgeGoe())
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();
JPAQuery<Member> countQuery = queryFactory
.select(member)
.from(member)
.where(
usernameEq(condition.getUsername()),
teamNameEq(condition.getTeamName()),
ageBetween(condition.getAgeLoe(), condition.getAgeGoe())
);
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchCount);
}
PageableExecutionUtil.getPage()
를 이용하면 된다.