Session 10 - Spring Boot Config, Pagi, Sorting
Session 10 - Spring Boot Config, Pagi, Sorting
Spring Boot
SPRING BOOT :
• 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
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
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
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
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
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
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
public class YourEntityService {
private final YourEntityRepository entityRepository;
SPRING BOOT :
CONFIGURING, PAGINATION AND SORTING
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
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
2. Create Sorting in a Spring Boot application using Spring Data JPA for a