728x90
게시글 목록을 조회할 때 페이지 번호로 목록이 구분되는 걸 많이 본적이 있을 거다.
한 화면에 모든 글을 보여줄 수 없기 때문에 페이지로 나눠 게시글 목록을 보여주는 것인데 오늘은 이 페이징 처리에 관해 글을 써보려고 한다.
이 기능을 실제로 하나하나 구현할 시 상당히 번거로운데 Spring Data JPA에서는 Pageable을 이용해 손쉽게 페이징 처리를 할 수 있도록 도와준다. 이떄 단순하게 pagination만 해주는 게 아니라 보여지는 목록에 정렬이 필요한 경우 sorting도 같이 할 수 있다.
1. 페이징 처리할 레포지토리에 JPARepositoy를 상속한다.
@Repository
public interface postRepository extends JpaRepository<Post, Long> {
List<Post> findAll(Pageable pageable);
}
2. 페이징 처리
pageable 객체를 만들어 repository의 메소드에 인자로 전달하면 된다.
Pageable pageable = PageRequest.of(현재 페이지 넘버, 조회할 페이지의 수, 정렬관련정보들);
2-1. 페이징 처리
Pageable pageable = PageRequest.of(page - 1, 10);
repo.메소드명(pageable);
페이지 번호는 0번 부터 시작해 1을 빼줘야한다.
2-2. 페이징 처리+정렬
Pageable pageable = PageRequest.of(0, 3, Sort.by("postId").descending());
repo.메소드명(pageable);
참고자료
https://www.baeldung.com/spring-data-jpa-pagination-sorting
https://devlog-wjdrbs96.tistory.com/414
http://devstory.ibksplatform.com/2020/03/spring-boot-jpa-pageable.html
728x90
'프로그램 개발(분석, 설계, 코딩, 배포) > 100. 기타' 카테고리의 다른 글
| null pointer 역참조(null pointer dereference) (0) | 2025.07.15 |
|---|---|
| 무료 소프트웨어 FREE SW (0) | 2025.07.11 |
| SKT 해킹에 사용된 BPF도어 악성코드, 리눅스 서버 점검 방법 (9) | 2025.07.09 |
| 장애 대응 You configured HTTP(80) on the standard HTTPS(443) port (0) | 2025.07.05 |
| IT, 의미, 2>/dev/null (1) | 2025.07.02 |