[따라하기] 3-6. 페이지네이션

허성재's avatar
Sep 20, 2024
[따라하기] 3-6. 페이지네이션
 
import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.PageRequest;
중복된 라이브러리의 이름들이 많아서 당황했다..
쟤네들을 써야한다.
 
테스트를 먼저 해보자
notion image
notion image
 
그리고 위처럼 테스트코드 쓰면 bytebuddy.ByteBuddyInterceptor 에러가 나서
notion image
 
notion image
 
요롷게 하면
이쁘게 나온다.
notion image
 
테스트는 끝났으니 JsonIgnore을 지우자
 
notion image
List가 아닌 Page로
 
notion image
 
Page 컬렉션이아닌 객체로 넘어간다 그렇기 때문에 model~
 
notion image
 
notion image
page 객체안엔 이런 데이터가 있다. 만약 앞 페이지나 뒷페이지로 가고싶을 때, 링크를 만들어야 하는데 머스태치는 연산이 되지 않는다.
page 객체안에도 앞페이지, 뒷페이지에 대한 정보가 없으니, DTO로 만들어주겠다.
 
@Data public static class PageDTO { private Integer number; // 현재페이지 private Integer totalPage; // 전체페이지 개수 private Integer size; // 한페이지에 아이템 개수 private Boolean first; private Boolean last; private Integer prev; // 현재페이지 -1 private Integer next; // 현재페이지 +1 private List<Integer> numbers = new ArrayList<>(); private List<Content> contents = new ArrayList<>(); private String keyword; public PageDTO(Page<Board> boardPG, String title) { this.keyword = title; this.number = boardPG.getNumber(); this.totalPage = boardPG.getTotalPages(); this.size = boardPG.getSize(); this.first = boardPG.isFirst(); this.last = boardPG.isLast(); this.prev = boardPG.getNumber()-1; this.next = boardPG.getNumber()+1; int temp = (number / 3)*3; // 0 -> 0, 3 -> 3, 6 -> 6 for(int i=temp; i<temp+3; i++){ // 0 this.numbers.add(i); } for (Board board : boardPG.getContent()){ contents.add(new Content(board)); } } @Data class Content { private Integer id; private String title; public Content(Board board) { this.id = board.getId(); this.title = board.getTitle(); } } }
길어서 코드를 넣었다. Boolean으로 페이지의 앞과 뒤가 있는지를 알려주고, 뭐로 찾았는지를 이쁘게 넣어 쏴주자.
notion image
인제 페이지를 넘길 수 있는 버튼도 생성할수 있다.
Share article

heo-gom