0% found this document useful (0 votes)
7 views15 pages

Session 10 - Spring Boot Config, Pagi, Sorting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Session 10 - Spring Boot Config, Pagi, Sorting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SPRING BOOT

Spring Boot

Configuring, Pagination and Sorting


3

SPRING BOOT :

CONFIGURING, PAGINATION AND SORTING

• Spring Boot provides convenient features for configuring pagination & sorting in applications.
• These features are often used in web applications to manage large sets of data and present it to users in
smaller, more manageable portions.
• To demonstrate pagination and sorting in Spring Boot users should have basic understanding of Spring
Boot and have already set up a project.
• Pagination is combined with Sorting provides a user-friendly experience.
4

SPRING BOOT :
CONFIGURING, PAGINATION AND SORTING

STEP BY STEP PROCEDURE

Pagination:
Pagination is the process of dividing data into smaller chunks known as pages
It is used to avoid loading and displaying a large amount of data at once.
1. Add Dependencies
2. Configure Pagination in Repository
3. Use Pagination in Service/Controller
SPRING BOOT :
5

CONFIGURING, PAGINATION AND SORTING


Example: Add Dependencies

Ensure that the necessary dependencies are available in


1. pom.xml (if Maven is used)
or
2. build.gradle (if Gradle is used) file:
For Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
For Gradle:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
SPRING BOOT : 6

CONFIGURING, PAGINATION AND SORTING


Example: Configure Pagination in Repository

In repository interface, Spring Data JPA's Pageable object can be use to enable pagination.
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface YourEntityRepository extends JpaRepository<YourEntity, Long>
{
Page<YourEntity> findAll(Pageable pageable);
}
SPRING BOOT : 7

CONFIGURING, PAGINATION AND SORTING


Example: Use Pagination in Service or Controller
In service or controller, Pageable object can be used to retrieve paginated data.

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class YourEntityService {
private final YourEntityRepository entityRepository;
public YourEntityService(YourEntityRepository entityRepository) {
this.entityRepository = entityRepository;
}
public Page<YourEntity> getAllEntities(Pageable pageable) {
return entityRepository.findAll(pageable);
}
}
8

SPRING BOOT :
CONFIGURING, PAGINATION AND SORTING

STEP BY STEP PROCEDURE

Sorting:
Sorting allows you to order the data based on specific criteria
Ascending or Descending order
1. Add Sorting in Repository
2. Use Sorting in Service/Controller
SPRING BOOT : 9

CONFIGURING, PAGINATION AND SORTING


Example: Add Sorting in Repository

To add sorting to the repository, the signature method can be modified to accept a Sort object.
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
public interface YourEntityRepository extends JpaRepository<YourEntity, Long>
{
List<YourEntity> findAll(Sort sort);
}
SPRING BOOT : 10

CONFIGURING, PAGINATION AND SORTING


Example: Use Sorting in Service or Controller
In the service or controller, the Sort object can be used to retrieve sorted data.

import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class YourEntityService {
private final YourEntityRepository entityRepository;

public YourEntityService(YourEntityRepository entityRepository) {


this.entityRepository = entityRepository;
}
public List<YourEntity> getAllEntitiesSorted(Sort sort) {
return entityRepository.findAll(sort);
}}
11

SPRING BOOT :
CONFIGURING, PAGINATION AND SORTING

STEP BY STEP PROCEDURE

Combined Pagination & Sorting:


You can combine pagination and sorting to get paginated and sorted results.

1. Add combined method in Repository


2. Use combined method in Service/Controller
SPRING BOOT : 12

CONFIGURING, PAGINATION AND SORTING


Example: Add Combined method in Repository

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {
Page<YourEntity> findAll(Pageable pageable);
List<YourEntity> findAll(Sort sort);
}
SPRING BOOT : 13

CONFIGURING, PAGINATION AND SORTING


Example: Use Combined Method in Service or Controller
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
public class YourEntityService {
private final YourEntityRepository entityRepository;
public YourEntityService(YourEntityRepository entityRepository) {
this.entityRepository = entityRepository; }
public Page<YourEntity> getAllEntities(Pageable pageable) {
return entityRepository.findAll(pageable); }
public List<YourEntity> getAllEntitiesSorted(Sort sort) {
return entityRepository.findAll(sort); }
}
14

SPRING BOOT :
CONFIGURING, PAGINATION AND SORTING
MCQs :
1. ____________ is the process of dividing data into smaller chunks known as pages.
a. Pagination b. Segmentation c. Tasking d. Chunking
2. Spring Data JPA’s __________ object can be use to enable pagination.
a. viewable b. pageable c. extendable d. scalable
3. Sorting allows you to order the data based on specific criteria in ____________ order.
a. Ascending b. Descending c. Both d. None
4. To add sorting to the repository _______ method can be modified to accept a Sort object.
a. watermark b. signature c. signed d. sort
5. In the service or controller _______ object can be used to retrieve sorted data.
a. watermark b. signature c. signed d. sort
15

SPRING BOOT :
CONFIGURING, PAGINATION AND SORTING

Exercise :

1. Create pagination in a Spring Boot application using Spring Data JPA for a

Book Repository – Login Id, Book Title , Book Author

2. Create Sorting in a Spring Boot application using Spring Data JPA for a

Book Repository – Login Id, Book Title , Book Author

You might also like