QueryDsl 적용 방법 3가지
QueryDsl 메서드 분리(BooleanExpression, OrderSpecifier)
•
BooleanExpression을 where절에 넣기
@Override
public List<Member> findByMonthPointRanking(int pageSize, Long lastId, Integer lastPoint) {
return queryFactory
.select(member)
.from(member)
.where(whereClauseMonth(lastId, lastPoint))
.orderBy(member.monthPoint.desc(), member.id.asc())
.limit(pageSize + 1)
.fetch();
}
private BooleanExpression whereClauseTotal(Long lastId, Integer lastPoint) {
if(lastId != null && lastPoint != null) {
return (member.totalPoint.lt(lastPoint))
.or(member.totalPoint.eq(lastPoint).and(member.id.gt(lastId)));
}
return null;
}
JavaScript
복사
•
OrderSpecifier로 order by 절에 넣기
protected OrderSpecifier<?> createOrderSpecifier(String sort, String order) {
// TODO: 전략 패턴 사용하여 리팩토링
if (sort == null || sort.isEmpty()) {
return center.id.asc();
}
if (sort.equals("participation")) {
if (order.equals("asc")) {
return center.participation.asc();
} else {
return center.participation.desc();
}
}
if (sort.equals("count")) {
if (order.equals("asc")) {
return center.c_people.castToNum(Integer.class).asc();
} else {
return center.c_people.castToNum(Integer.class).desc();
}
}
return center.id.asc();
}
JavaScript
복사
@Override
public List<CenterSearchResponseDTO> findBySearchCenterDTO(String c_name, String c_address, String c_ph, Participation participation, String sort, String order) {
List<CenterSearchResponseDTO> centerList;
OrderSpecifier<?> orderSpecifier = createOrderSpecifier(sort, order);
if(c_name == null && c_address == null && c_ph == null && participation == null) {
centerList =
jpaQueryFactory
.select(Projections.constructor(CenterSearchResponseDTO.class,
qCenter.id, qCenter.c_name, qCenter.c_address, qCenter.c_ph, qCenter.participation, qCenter.c_people))
.from(qCenter)
.orderBy(orderSpecifier)
.limit(1000)
.fetch();
}
else {
centerList =
jpaQueryFactory
.select(Projections.constructor(CenterSearchResponseDTO.class,
qCenter.id, qCenter.c_name ,qCenter.c_address, qCenter.c_ph, qCenter.participation, qCenter.c_people))
.from(qCenter)
.where(cNameLike(c_name),
cAddressLike(c_address),
cPhLike(c_ph),
cParticipationEq(participation))
.orderBy(orderSpecifier)
.limit(1000)
.fetch();
}
return centerList;
}
JavaScript
복사
QueryDsl Sort, Pageable 적용
•
Sort 사용
Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");
List<Application> applicationList = applicationRepository.findAllByCenterId(centerId, sort);
JavaScript
복사
•
Pageable 사용