(QueryDSL) 결과조회, 페이징과 정렬


  • uniqueResult() : 조회결과가 한 건일때 사용한다. 조회 결과가 없으면 null을 반환하고 결과가 하나이상이면 com.mysema.query.NonUniqueResultException예외가 발생
  • singleResult() : uniqueResult() 와 같지만 결과가 하나 이상이면 처음 데이터를 반환한다.
  • list() : 결과가 하나 이상일 때 사용한다. 결과가 없으면 빈 컬렉션을 반환한다.



<페이징과 정렬>


QItem qItem = QItem.item;

query.from(item)
.where(item.price.gt(20000))
.orderBy(item.price.desc(), item.stockQuantity.asc())
.offset(10).limit(20)
.list(item);



또는
QueryModifiers를 사용할 수 있다.

QueryModifiers queryModifiers = new QueryModifiers(20L, 10L);

List<Item> list =
query.from(item)
.restrict(queryModifiers)
.list(item);


실제 페이징 처리를 하려면 검색된 전체 데이터 수를 알아야한다.
이떄는 List() 대신에 listResults()를 사용한다.


SearchResults<Item> result =
query.from(item)
.where(item.price.gt(10000))
.offset(10).limit(20)
.listResults(item);


long total = result.getTotal();
long limit = result.getLimit();
long offset = result.getOffset();
List<Item> results = result.getResults();


<그룹>

groupBy를 사용하고 결과를 제한하려면 having을 사용하면된다.

query.from(item)
.groupBy(item.price)
.having(item.price.gt(1000))
.list(item);



댓글

이 블로그의 인기 게시물

(네트워크)폴링방식 vs 롱 폴링방식

(ElasticSearch) 결과에서 순서 정렬

(18장) WebSocekt과 STOMP를 사용하여 메시징하기