0
0
Spring Bootframework~10 mins

Pagination and sorting with Pageable in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pagination and sorting with Pageable
Client sends request with page, size, sort
Controller receives Pageable object
Repository queries DB with Pageable
DB returns page of sorted data
Controller returns Page<T> response
Client receives paged, sorted data
The client sends paging and sorting info, the controller uses Pageable to query the database, and returns a page of sorted data.
Execution Sample
Spring Boot
Pageable pageable = PageRequest.of(0, 5, Sort.by("name"));
Page<User> page = userRepository.findAll(pageable);
List<User> users = page.getContent();
This code requests the first page with 5 users sorted by name, then gets the list of users from the page.
Execution Table
StepActionPage NumberPage SizeSort FieldSort DirectionResult
1Create Pageable with page=0, size=5, sort by name ascending05nameASCPageable object ready
2Call repository findAll with Pageable05nameASCQuery DB for first 5 users sorted by name
3DB returns users sorted by name, limited to 505nameASCPage<User> with 5 users
4Extract content list from Page<User>05nameASCList<User> with 5 users
5Client receives paged, sorted user list05nameASCUsers displayed in sorted order
💡 Completed fetching first page of 5 users sorted by name ascending
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
pageablenullPageRequest(page=0, size=5, sort=name ASC)SameSameSameSame
pagenullnullPage<User> with 5 sorted usersSameSameSame
usersnullnullnullnullList<User> with 5 usersSame
Key Moments - 3 Insights
Why does the page number start at 0 instead of 1?
Page numbers in Pageable are zero-based, so page=0 means the first page. See execution_table step 1 where page=0 is set.
How does sorting work with Pageable?
Sorting is part of the Pageable object. In step 1, Sort.by("name") sets sorting by the 'name' field ascending, which the repository uses in the DB query (step 2).
What if the requested page has fewer items than the page size?
The Page object will contain only the available items. For example, if only 3 users remain, the page content list will have 3 users instead of 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the page size after step 2?
A5
B0
C10
DUndefined
💡 Hint
Check the 'Page Size' column in execution_table row for step 2
At which step does the database return the sorted user data?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step describing DB returning data in execution_table
If we change Sort.by("name") to Sort.by("age").descending(), what changes in the execution_table?
ASort Field changes to 'age' and Sort Direction to 'DESC'
BPage Number changes to 1
CPage Size changes to 10
DNo changes
💡 Hint
Look at the 'Sort Field' and 'Sort Direction' columns in execution_table step 1
Concept Snapshot
Pagination and sorting with Pageable in Spring Boot:
- Use PageRequest.of(page, size, Sort) to create Pageable
- Page numbers start at 0 (first page)
- Sort can be ascending or descending by any field
- Repository returns Page<T> with content and metadata
- Extract list with page.getContent() for display
Full Transcript
In Spring Boot, pagination and sorting are handled by the Pageable interface. The client sends parameters for page number, page size, and sorting. The controller creates a Pageable object using PageRequest.of, specifying the page index (starting at 0), the number of items per page, and the sorting field and direction. This Pageable is passed to the repository's findAll method, which queries the database accordingly. The database returns a Page object containing the requested slice of data sorted as specified. The controller extracts the list of items from the Page and returns it to the client. This process allows efficient loading of large datasets in manageable chunks with sorting applied.