[스프링] Page 사용법

[스프링] Page 사용법

페이지 리스폰스용 오브젝트로 스프링의 Page 사용법

org.springframework.data.domain.Page

도큐먼트 보기

Page는 Pageable로 리퀘스트가 들어와 리스폰스로 결과값을 전달할 때 사용할 수 있는 오브젝트다.

@Query annotation을 사용하는 것이 이 오브젝트의 의도인 것 같다.

쿼리문만 넣어주면 다음과 같이 손쉽게 페이징 처리를 할 수 있는 장점이 있는 것 같다.

//컨트롤러 public ResponseEntity> findUserByName(String name, Pageable pageable) { Page result = memberRepository.findByUsername( name, pageable); //... }

//생성할 JPA repository public interface MemberRepository extends Repository { @Query("select * from Users where name like :name ") Page findByUsername(String name, Pageable pageable); }

그러나..

실제 서비스를 구현할 때는 오히려 위와 같은 방식은 불편할 수 있다..

여러 쿼리문을 돌리고 합친다던가.. 임의로 값을 변경해야하는 상황이 있다던가..

그래서 다음과 같이 아예 new로 생성해 쓰는 것이 편하다.

Page result = new PageImpl( '실제 반환할 리스트', '인풋으로 받은 pageable', '총 아이템 개수' );

from http://kye-develop.tistory.com/32 by ccl(A) rewrite - 2021-03-24 18:26:42