Backend/Spring

Pageable 객체

brian110326 2024. 4. 12. 22:49

Pageable : 페이징 처리에 필요한 메소드 제공

 

Pageable pageable = PageRequest.of(0, 10);
    Pageable pageable = PageRequest.of(0, 10, Direction.DESC);
    Pageable pageable = PageRequest.of(0, 10, Direction.DESC, "id");
    1페이지부터 10개씩, id를 기준으로 내림차순 정렬

    Pageable pageable = PageRequest.of(0, 10, Sort.by("id").descending());

 

PageRequest.of()기능

                                      PageRequest.of()                                              설명
PageRequest.of(int page, int size) 페이지 번호(0부터 시작), 페이지당 데이터

의 수
PageRequest.of(int page, int size,
Sort.Direction direction, String..props)
페이지 번호, 페이지당 데이터의 수, 정렬방
향, 속성(컬럼) 들
PageRequest.of(int page, int size, Sort
sort)
페이지 번호, 페이지당 데이터의 수, 정렬방

 

Page<Book> result = bookRepository.findAll(
      bookRepository.makePredicate(),
      pageable
    );

    System.out.println("전체 행 수 : " + result.getTotalElements());
    System.out.println("페이지 수 : " + result.getTotalPages());
    result
      .getContent()
      .forEach(book -> {
        System.out.println(book);
      });

'Backend > Spring' 카테고리의 다른 글

Security  (0) 2024.04.22
실수하는 것들, 주의할점들  (0) 2024.04.16
QueryDSL(2)  (0) 2024.04.12
QueryDSL(1)  (0) 2024.04.12
Native Sql  (0) 2024.04.12