Loslegen
Javaneer
Zurück zur Stufe
Modul 3·Daten & Persistenz

Paging, Sortierung & Projektionen

Mit Pageable seitenweise zurückgeben, Ergebnisse sortieren und mit Projektionen nur die benötigten Spalten laden.

14 Min. LesezeitFortgeschritten

Deutsche Übersetzung in Arbeit

Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.

Auf dieser Seite

A library might hold a hundred thousand books. findAll() returning every one in a single response is slow, memory-hungry, and useless to a client that shows twenty at a time. Spring Data has first-class support for returning one page at a time, sorted, fetching only the fields you need.

Paging with Pageable

Add a Pageable parameter and return a Page:

interface BookRepository extends JpaRepository<Book, Long> {
    Page<Book> findByAuthor(String author, Pageable pageable);
}
Page<Book> page = repository.findByAuthor(
    "Joshua Bloch",
    PageRequest.of(0, 20, Sort.by("title"))   // page 0, 20 per page, sorted by title
);

page.getContent();        // the 20 books on this page
page.getTotalElements();  // total matching rows
page.getTotalPages();     // total pages

A Page carries the slice and the metadata a UI needs to render "Page 1 of 47." Spring Data runs an efficient LIMIT/OFFSET query plus a count query for you.

Let Spring bind the page from the request

In a controller, accept Pageable directly and Spring binds it from query parameters: GET /api/books?page=2&size=20&sort=title,asc. No manual parsing - the client controls paging and sorting through the URL.

Paging is a search-results page

Nobody wants a web search that returns ten million links on one page. It gives you page one - ten results, sorted by relevance - plus "Next" and a total count. A JPA Page is exactly that: a manageable slice of the data with the navigation metadata attached, so your API and UI stay fast no matter how big the table grows.

Projections: fetch only what you need

Sometimes you don't need the whole entity - a dropdown just needs each book's title and ISBN. A projection interface tells Spring Data to select only those columns:

interface BookSummary {           // a projection
    String getIsbn();
    String getTitle();
}

interface BookRepository extends JpaRepository<Book, Long> {
    List<BookSummary> findByAuthor(String author);   // SELECT isbn, title …
}

Less data leaves the database, less memory is used, and the response is leaner - a straightforward performance win when you don't need every field.

Serve a big catalog

BookVault's catalog grows to 100,000 books and a client wants to display them 25 at a time, sorted by title, showing only each book's title and ISBN in the list view. Which two Spring Data features do you combine, and what would the request URL look like?

What does returning a Page<Book> (with a Pageable) give you over returning List<Book>?

Key takeaways

  • Return Page<T> with a Pageable parameter to fetch one page at a time, with an efficient LIMIT/OFFSET query plus a count.
  • A Page carries both the slice and metadata (total elements/pages) a UI needs.
  • Controllers can accept Pageable directly and Spring binds it from ?page=&size=&sort= query params.
  • Projections (interfaces exposing a subset of getters) fetch only the columns you need, reducing data transfer and memory.
  • Combine paging + projections to serve large catalogs quickly.
War diese Lektion hilfreich?
Diese Seite auf GitHub bearbeiten