@GetMapping("/orders")public PageResponse<OrderResponse> list( @RequestParam(defaultValue="0") int page, @RequestParam(defaultValue="20") int size) { var pg = PageRequest.of(page, size, Sort.by("placedAt").descending()); return mapper.toResponse(orderRepo.findAll(pg));}
Warning: Offset pagination becomes O(N) at the DB; avoid for large datasets — use cursor.
2. Implementing Cursor-Based Pagination
Example: Keyset cursor
-- cursor encodes (placed_at, id) of last itemSELECT * FROM ordersWHERE (placed_at, id) < (?, ?)ORDER BY placed_at DESC, id DESCLIMIT 21; -- 1 extra to detect hasNext