Pagination using Spring Boot & Spring Data JPA
Technology
Pagination allows the users to see a small portion of data at a time (a page).
Implementation:
class:
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String address;
private String school;
private String department;
Repository:
public interface StudentRepository extends JpaRepository<Student,Long> {
}
Configure:
Pageable:– pageable instance as an input parameter, as it provides the number of pages to load as well as the size of the pages.
Sort:- it also allows us to define fields to sort by and the direction in which they should be sorted in ascending or descending.
public class Pagination {
/**
* It return Pageable object to create page instance
* @param pageNo -The current page number.
* @param pageSize - Number of records on each page.
* @param sortBy - Field names if any sorting has to be applied.
* @param sortDirection - Ascending or Descending
* @return Pageable
*/
public Pageable getPageable(int pageNo, int pageSize, String sortBy, String sortDirection){
// Sort data
Sort orders = sortDirection.equalsIgnoreCase(Sort.Direction.ASC.name()) ? Sort.by(sortBy).ascending()
: Sort.by(sortBy).descending();
return PageRequest.of(pageNo,pageSize,orders);
}
Controller:
@RestController
@RequestMapping("/students")
public class StudentRestController {
private final StudentService studentService;
public StudentRestController(StudentService studentService) {
this.studentService = studentService;
}
@PostMapping
public Student addStudent(@RequestBody Student student){
return studentService.addStudent(student);
}
@GetMapping
public List<Student> getAllStudent(@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo,
@RequestParam(value = "pageSize", defaultValue = "1", required = false) int pageSize,
@RequestParam(value = "sortBy", defaultValue = "id", required = false) String sortBy,
@RequestParam(value = "sortDir", defaultValue = "asc", required = false) String sortDir){
return studentService.allStudentList(pageNo,pageSize,sortBy,sortDir);
}
Service:
Page:- The Page interfaces provide metadata for a page of items that are returned to the client .
repository.findAll(Pageable pageable) – returns a Page of entities where the paging restriction is provided in the Pageable
object.
@Service
public class StudentService {
private StudentRepository studentRepository;
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public Student addStudent(Student student){
return studentRepository.save(student);
}
/**
* It return list of Student based of page size , number
* @param pageNo -The current page number.
* @param pageSize - Number of records on each page.
* @param sortBy - Field names if any sorting has to be applied.
* @param sortDir - Ascending or Descending
* @return list of student
*/
public List<Student> allStudentList(int pageNo, int pageSize,String sortBy, String sortDir){
//create Page by using pageable information from repository
Page<Student> studentPage=studentRepository.findAll(new Pagination().getPageable(pageNo,pageSize,sortBy,sortDir));
List<Student> studentList=studentPage.getContent();
return studentList;
}
Categories
Recent Posts
- Client Device Identification in REST API using Java & Spring Boot
- Extract Meta Data and Compress RAW Images using Python
- Twilio SMS API Integration with Java & Spring Boot for Sending OTP
- Razorpay Payment Gateway Implementation for Merchants using Java & Spring Boot
- Pagination using Spring Boot & Spring Data JPA