[Spring Boot] 52. 스프링부트 블로그 v2 (JPA) (22) 페이징

김주희's avatar
Apr 15, 2025
[Spring Boot] 52. 스프링부트 블로그 v2 (JPA) (22) 페이징
prev/next 버전으로 먼저 만들기!
QueryString으로 (PathVariable 아님!! 주소 아니니까)
 
현재 페이지에 대해서 dto
다음 페이지를 받아와야함
mustache는 연산 불가라서 다음페이지 번호를 받아와야함
 
notion image
⇒ model에 있는 데이터만으로 해결이 안된다면!! → DTO를 넘겨야 (지금은 서버사이드렌더링이니까 화면에 다 뿌리고 전달하니까 LAZY고 뭐고 ㄱㅊ)
(json으로 할때는 무조건 DTO!!!!)
 
{{#model.boards}} = getter 호출하는 것
notion image
 
 
 
댓글 같은거 OneToMany는 페이징 처리가 안돼서 사용 비추~.~
 
public class BoardResponse { // 페이징 -> 현재 페이지 번호와 다음 페이지 번호를 넘겨줘야 됨 -> DTO 만듦 @Data public static class MainDTO { List<Board> boards; private Integer prev; private Integer next; private Boolean isFirst; // currentPage private Boolean isLast; public MainDTO(List<Board> boards, Integer prev, Integer next) { this.boards = boards; this.prev = prev; this.next = next; } } @Data public static class DetailDTO { private Integer id; private String title; private String content; private Boolean isPublic; private Boolean isOwner; // 값이 안 들어갈 경우: Boolean - null / boolean - 0 private Boolean isLove; private Long loveCount; private String username; // User 객체를 다 들고 갈 필요X private Timestamp createdAt; private Integer loveId; private List<ReplyDTO> replies; // DetailDTO 안에서만 쓸거니까 내부클래스로 @Data public class ReplyDTO { private Integer id; private String content; // 유저 객체가 굳이 필요 없고 username만 필요 private String username; private Boolean isOwner; // Board의 Reply를 for문 돌려서 여기에 옮기면 됨 public ReplyDTO(Reply reply, Integer sessionUserId) { this.id = reply.getId(); this.content = reply.getContent(); this.username = reply.getUser().getUsername(); // join했기 때문에 Lazy 로딩 X this.isOwner = reply.getUser().getId().equals(sessionUserId); // wrapping class는 equals로 비교 } } // model에 있는 것을 옮기는 것 // 깊은 복사 : 객체를 그대로 가져와서 getId 등으로 넣는게 낫다! public DetailDTO(Board board, Integer sessionUserId, Boolean isLove, Long loveCount, Integer loveId) { this.id = board.getId(); this.title = board.getTitle(); this.content = board.getContent(); this.isPublic = board.getIsPublic(); this.isOwner = sessionUserId == board.getUser().getId(); this.username = board.getUser().getUsername(); this.createdAt = board.getCreatedAt(); this.isLove = isLove; this.loveCount = loveCount; this.loveId = loveId; // ReplyDTO를 repliesDTO 컬렉션으로 옮기기 List<ReplyDTO> repliesDTO = new ArrayList<>(); for (Reply reply : board.getReplies()) { ReplyDTO replyDTO = new ReplyDTO(reply, sessionUserId); repliesDTO.add(replyDTO); } this.replies = repliesDTO; } } }
 
notion image
notion image
 
 

25.04.21

notion image
notion image
 
^ 이거 처리하는 방법
 
prev는 0페이지에서 뒤로 못가게만 하면 됨
 
 
 

notion image
notion image
 
board에 Reply와 User → Lazy
json으로 직렬화됨 → getter를 다 때리면서
board 크기만큼 getter를 때림
근데 getUser와 getReplies
json으로 바꾸려는 애(→ 값이 없네?)과 db에 select하려는 애
json으로 직렬화하려는 애가 getUser 때리면 id만 있고 그 안의 get을 때리는 순간 값이 없음
→ lazy 로딩하는애가 select해서 채워넣으려고 함 → 근데 json 직렬화하려는 애는 바로 값을 땡겨서 null을 가져옴
  1. board객체를 controller에 던지지X DTO를 던지기
  1. Lazy 로딩을 기다렸다가 직렬화할 수 있도록 설정
 
 
 
 
  1. 더미 20개 만들어야 됨
  1. 20개 [0, 1, 2, 3, 4, 5, 6] -> model.numbers -> 오브젝트 필드명이 없음 => {{.}} (1을 클릭하면 page=1로 queryString 만들어주면 됨)
  1. 20개 중에 5개씩 pageNumberSize = 5를 따로 들고 있어야 됨! current = 0 ~ 4 [0, 1, 2, 3, 4] current = 5 ~ 9 [5, 6, 7, 8, 9] -> [5, 6]
current % 5 = 나머지 current / 5 = 0, 1... number = [몫+나머지, 몫+나머지1,
 

25.04.22

 
Test 코드
package shop.mtcoding.blog.temp; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; public class PagingTest { @Test public void makeNumbers() { int current = 5; int totalPage = 7; List<Integer> numbers = new ArrayList<>(); int start = (current / 5) * 5; int end = Math.min(start + 5, totalPage); for (int i = start; i < end; i++) { numbers.add(i); } numbers.forEach(System.out::println); } }
Share article

jay0628