상황
queryDsl에서 지원했는 fetchResults와 fetchCount가 deprecate된다고 한다.
그렇기에 이전에 지원했는 fetchResults에서 제공하는 count 쿼리를 직접 작성해서 크기를 구해야 한다.
deprecate 되기 이전 버전 코드
public class MemberRepositoryImpl implements MemberRepositoryCustom{
private final JPAQueryFactory queryFactory;
public MemberRepositoryImpl(JPAQueryFactory queryFactory) {
this.queryFactory = queryFactory;
}
@Override
public Page<MemberTeamDto> searchPageSimple(MemberSearchCondition condition, Pageable pageable) {
QueryResults<MemberTeamDto> result = 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()),
ageGoe(condition.getAgeGoe()),
ageLoe(condition.getAgeLoe())
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetchResults();
List<MemberTeamDto> content = result.getResults();
long total = result.getTotal();
return new PageImpl<>(content, pageable, total);
}
private BooleanExpression usernameEq(String username) {
return hasText(username) ? member.username.eq(username) : null;
}
private BooleanExpression teamNameEq(String teamName) {
return hasText(teamName) ? team.name.eq(teamName) : null;
}
private BooleanExpression ageGoe(Integer ageGoe) {
return ageGoe != null ? member.age.goe(ageGoe) : null;
}
private BooleanExpression ageLoe(Integer ageLoe) {
return ageLoe != null ? member.age.loe(ageLoe) : null;
}
}
deprecate된 이후 버전
@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()),
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 new PageImpl<>(content, pageable, total);
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}
참고 자료
https://jjunn93.com/entry/Querydsl-페이징-연동-Querydsl-fetchResults-fetchCount-Deprecated향후-미지원
'Spring' 카테고리의 다른 글
@RequiredArgsConstructor 와 @Autowired의 차이 (0) | 2023.09.26 |
---|---|
스프링 시큐리티 주요 아키텍처 이해 - 위임 필터 및 필터 빈 초기화 - DelegatingProxyChain, FilterChainProxy (0) | 2023.07.11 |
로그인 처리1 - 쿠키, 세션 : 로그인 처리하기 -세션 정보와 타임아웃 설정 (0) | 2022.09.12 |
로그인 처리1 - 쿠키, 세션 : 로그인 처리하기 - 서블릿 HTTP 세션2 (0) | 2022.09.12 |
로그인 처리1 - 쿠키, 세션 : 로그인 처리하기 - 서블릿 HTTP 세션1 (0) | 2022.09.12 |