0
0
Spring Bootframework~5 mins

Pagination and sorting with Pageable in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the Pageable interface in Spring Boot?
The Pageable interface helps to request a specific page of data with a defined size and sorting order. It makes it easy to handle pagination and sorting in database queries.
Click to reveal answer
beginner
How do you specify sorting when using Pageable?
You can specify sorting by using PageRequest.of(page, size, Sort.by("fieldName").ascending()) or PageRequest.of(page, size, Sort.by("fieldName").descending()) to sort the results by a field in ascending or descending order.
Click to reveal answer
intermediate
What does the Page object returned by a repository method contain?
The Page object contains the list of items for the requested page, total number of pages, total number of elements, current page number, and page size. It helps to navigate through paged data easily.
Click to reveal answer
beginner
How can you enable pagination and sorting in a Spring Data JPA repository method?
Add a Pageable parameter to the repository method signature, for example: Page<Entity> findAll(Pageable pageable);. Spring Data will automatically apply pagination and sorting based on the Pageable argument.
Click to reveal answer
advanced
What is the difference between Pageable and Slice in Spring Data?
Pageable returns a Page which contains total count information, while Slice only knows if there is a next slice available and does not fetch total count, which can improve performance when total count is not needed.
Click to reveal answer
Which method creates a Pageable object for page 2 with 10 items per page sorted by 'name' ascending?
APageRequest.of(0, 10, Sort.by("name").ascending())
BPageRequest.of(2, 10, Sort.by("name").ascending())
CPageRequest.of(1, 10, Sort.by("name").ascending())
DPageRequest.of(1, 10, Sort.by("name").descending())
What does the Page interface provide besides the list of items?
AOnly the list of items
BSorting configuration only
CDatabase connection info
DTotal pages, total elements, current page number, page size
How do you add pagination support to a Spring Data JPA repository method?
AAdd a <code>Pageable</code> parameter to the method
BAdd a <code>Sort</code> parameter only
CUse <code>List</code> return type
DNo changes needed
Which of the following is true about Slice compared to Page?
A<code>Slice</code> does not contain total count information
B<code>Slice</code> contains total pages info
C<code>Slice</code> fetches all data at once
D<code>Slice</code> is deprecated
What is the default sort direction if not specified in Pageable?
AAscending
BNo sorting applied
CRandom
DDescending
Explain how to implement pagination and sorting in a Spring Boot REST API using Pageable.
Think about how to request a specific page and sort order, and how to return the data with helpful info.
You got /4 concepts.
    Describe the difference between Page and Slice in Spring Data pagination.
    Focus on what metadata each provides and when you might prefer one over the other.
    You got /4 concepts.