0Pricing
Spring Boot 4 Microservices & REST APIs · Lekcja

Stronicowanie i sortowanie

Wydajnie zwracaj duże zbiory danych

Stronicowanie i sortowanie to bezpłatna lekcja Spring Boot 4 Microservices & REST APIs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Microservices & REST APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Why Paginate

Returning thousands of rows at once is slow and memory-heavy. Pagination fetches one page at a time.

Spring Data provides Pageable, Page, and Sort for this.

PagingAndSortingRepository

JpaRepository already includes paging and sorting support, so you get these methods for free.

public interface UserRepository
    extends JpaRepository<User, Long> {
}

Creating a Pageable

PageRequest.of(page, size) builds a request. Page numbers are zero-based.

Pageable pageable = PageRequest.of(0, 20);
Page<User> page = userRepository.findAll(pageable);

The Page Object

A Page carries the content plus metadata.

List<User> content = page.getContent();
long total = page.getTotalElements();
int pages = page.getTotalPages();
boolean hasNext = page.hasNext();

Sorting

Build a Sort and pass it alone or inside a PageRequest.

Sort sort = Sort.by("name").ascending();
List<User> sorted = userRepository.findAll(sort);

Combining Sort and Paging

Pass a Sort into PageRequest.of.

Pageable pageable = PageRequest.of(
    0, 20, Sort.by("name").descending());
Page<User> page = userRepository.findAll(pageable);

Multiple Sort Orders

Chain sort orders for tie-breaking.

Sort sort = Sort.by(
    Sort.Order.asc("lastName"),
    Sort.Order.desc("createdAt"));

Paging Derived Queries

Add a Pageable parameter to any derived query method.

Page<User> findByActiveTrue(Pageable pageable);

Slice vs Page

A Slice knows only whether a next page exists, skipping the expensive COUNT query that Page runs. Use Slice for infinite scroll.

Slice<User> findByActiveTrue(Pageable pageable);

Pageable from the Web Layer

Spring MVC can bind page, size, and sort request params into a Pageable automatically.

@GetMapping("/users")
public Page<User> list(Pageable pageable) {
    return userRepository.findAll(pageable);
}
// GET /users?page=0&size=20&sort=name,desc

Mapping Page Content to DTOs

Transform a Page's entities to DTOs while preserving pagination metadata with map.

Page<UserDto> dtos = page.map(
    user -> new UserDto(user.getName(), user.getEmail()));

Quick Check

Test your understanding of pagination.

Recap

You learned pagination and sorting:

  • PageRequest.of(page, size) — page is zero-based
  • Page carries content plus totals; Slice skips the count
  • Sort.by(...) for ordering
  • Web layer binds page/size/sort into a Pageable

Często zadawane pytania

Czy lekcja „Stronicowanie i sortowanie” jest bezpłatna?

Tak — pełny tekst „Stronicowanie i sortowanie” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Microservices & REST APIs, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Microservices & REST APIs zawiera 4 lekcji w sumie.

Co nauczysz się w „Stronicowanie i sortowanie”?

Wydajnie zwracaj duże zbiory danych Ćwiczysz Spring Boot 4 Microservices & REST APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Microservices & REST APIs?

Nie wymagamy żadnego doświadczenia. Spring Boot 4 Microservices & REST APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Stronicowanie i sortowanie”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Microservices & REST APIs?

Tak. Każda lekcja Spring Boot 4 Microservices & REST APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dynamiczne zapytania ze Specifications
  2. Projekcje i DTO
  3. Audytowanie za pomocą @CreatedDate
  4. Stronicowanie i sortowanie
← Powrót do Spring Boot 4 Microservices & REST APIs