π Pagination(νμ΄μ§λ€μ΄μ )μ΄λ?
νμ΄μ§λ€μ΄μ μ΄λ, ν΄λΌμ΄μΈνΈμμ νμ΄μ§ νλ λΉ νμν λ§νΌ λ°μ΄ν° μμ²μ 보λ΄κ³ μλ²μμ κ·Έλ§νΌ 보λ΄μ£Όλ νμμ λ§νλ€.
π Pagination(νμ΄μ§λ€μ΄μ )μ μ¬μ©νλ μ΄μ
μ¬μ©μκ° ν νμ΄μ§μμ μλ°±λ§κ°λ λλ λ°μ΄ν°λ€μ ν λ²μ 보λ κ²μ λΉμ°ν μ΄λ ΅κ³ λΆνΈν μΌμ΄λ€.
μ΄ λΆνΈν¨μ μ€μ΄κ³ μ, ν΄λΌμ΄μΈνΈκ° μ λΉλμ μμ²ν΄μ μλ²μμ κ·Έλ§νΌ λ°μ΄ν°λ₯Ό 보λ΄μ£Όλ κ²μ΄λ€.
λν μλ²μμλ νμ΄μ§λ³λ‘ νμν λ§νΌλ§ μ‘°ννλ©΄ μλ² λΆνλ μ€κ³ , λ°μμλλ λΉ¨λΌμ§λ€.
π μ€νλ§λΆνΈλ‘ ꡬννκΈ°
μ€νλ§λΆνΈμμ μ§μνλ Pageμ Pageableμ μ¬μ©ν κ²μ΄λ€. Pageableμ μμ²ν νμ΄μ§ μ 보λ₯Ό λ΄λ μΈν°νμ΄μ€μ΄λ€.
μ΄λ import νλ Pageλ
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
μ΄λ, ν·κ°λ¦¬μ§ λ§μκΈΈ.
BoardService.java
public Page<BoardResponse> getAllBoardResponses(SortType sortType, Pageable pageable) {
Page<Board> boards = switch (sortType) {
// μ‘°νμκ° λμ μμλλ‘
case VIEWS -> boardRepository.findAllByIsDeletedFalseOrderByViewCountDesc(pageable);
// μ’μμ μκ° λ§μ μ
case LIKES -> boardRepository.findAllByIsDeletedFalseOrderByLikeCountDesc(pageable);
// μ΅μ μ(μμ±μΌμ΄ λ¦λ μ)
case CREATES -> boardRepository.findAllByIsDeletedFalseOrderByCreatedAtDesc(pageable);
default -> boardRepository.findByIsDeletedFalse(pageable);
};
return boards.map(board -> {
long likeCount = likeRepository.countByBoard(board);
List<CommentResponse> comments = commentService.findByBoardId(board.getId());
int commentCount = comments.size();
return new BoardResponse(board, likeCount, false, commentCount);
});
}
μλΉμ€ ν΄λμ€μμ BoardResponse νμ μ Pageλ‘ λ³κ²½ν΄μ€λ€.
μ£Όμν μ μ, μλΉμ€μμ μ¬μ©λ λ©μλλ€μ λ ν¬μ§ν 리μμλ Board νμ μ Pageλ‘ ν΄μ€μΌνλ€.
BoardController.java
@GetMapping
public ResponseEntity<Page<BoardResponse>> getAllBoards(
@RequestParam(defaultValue = "CREATES") SortType sortType,
Pageable pageable
) {
Page<BoardResponse> boards = boardService.getAllBoardResponses(sortType, pageable);
return ResponseEntity.ok(boards);
}
컨νΈλ‘€λ¬μμλ λκ°μ΄ Pageλ₯Ό νμ μΌλ‘ νμ¬ μλΉμ€ λ‘μ§μ νΈμΆνλ©΄ λλ€.
π PostmanμΌλ‘ ν μ€νΈνκΈ°


μ‘°νμλ₯Ό κΈ°μ€μΌλ‘ 0λ²μ§Έ νμ΄μ§μ λ°μ΄ν°λ₯Ό 5κ° λ΄μμ 보λ΄λ¬λΌλ μμ²μ 보λλ€.
λ°μ΄ν°κ° λ κ°λ°μ μμ΄μ κ·Έλ μ§λ§,, κ°μ΄ μ μΆλ ₯λλ κ²μ λ³Ό μ μλ€.

κ°μ¬ν©λλ€ O(∩_∩)O
'Development > Back-end' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| [Spring Boot] @Scheduled μ¬μ©νκΈ° (0) | 2025.08.10 |
|---|---|
| [Spring Boot] Redisλ₯Ό μ¬μ©ν λ‘κ·Έμμ ꡬννκΈ° (0) | 2025.07.23 |
| [Socket.io] Socket.io μ λνμ¬ (0) | 2025.06.07 |
| [Express] μ’μμ κΈ°λ₯ ꡬννκΈ° (0) | 2025.06.06 |
| [Express] μλ¦¬κ³ APIλ₯Ό μ¬μ©ν λ‘κ·ΈμΈ/νμκ°μ ꡬννκΈ° (0) | 2025.06.02 |