DEV Community

Cover image for Spring Boot - Pagination and Sorting Project
Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on

Spring Boot - Pagination and Sorting Project

Pagination is often helpful when we have a large dataset and we want to present it to the user in smaller chunks.
Also, we often need to sort that data by some criteria while paging.

Installation

1) Clone the Repo from here CLICK HERE

2) Import Maven based project in any of your Favourite IDE.

./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Output

Open in Browser

Access H2 Database.

http://localhost:8080/h2-console
Enter fullscreen mode Exit fullscreen mode

Output

Endpoint Method Body Description
http://localhost:8080/student/search GET - To get all students in paginated format
http://localhost:8080/student POST { "id":214214,"name":"Atharva","rollno":32 } To Add Students to the DB
http://localhost:8080/pagination?pageSize=5 GET - to get first 5 student details
http://localhost:8080/pagination?pageSize=5&pageNo=1 GET - To get student details by pagesize 5 and pageno 2
http://localhost:8080/pagination?pageSize=5&pageNo=2 GET - To get student details by pagesize 5 and pageno 1
http://localhost:8080/pagination?pageSize=5&pageNo=1&sortBy=id GET - To sort student details by id
http://localhost:8080/pagination?pageSize=5&pageNo=1&sortBy=name GET - To sort student by name

Usage

SortingRepository.java

@Repository
public interface SortingRepository extends PagingAndSortingRepository<Student, Integer>  {

}
Enter fullscreen mode Exit fullscreen mode

StudentService.java

public PaginatedStudentResponse readStudent(Pageable pageable) {
        Page<Student> students = studentRepository.findAll(pageable);
        return PaginatedStudentResponse.builder()
                .numberOfItems(students.getTotalElements()).numberOfPages(students.getTotalPages())
                .studentList(students.getContent())
                .build();

    }

    public List<Student> getAllStudents(Integer pageNo, Integer pageSize, String sortBy)
        {
            Pageable paging = PageRequest.of(pageNo, pageSize, Sort.by(sortBy));
            Page<Student> pageResult = studentRepository.findAll(paging);

            if(pageResult.hasContent()) {
                return pageResult.getContent();
            }
            else
            {
                return new ArrayList<Student>();
            }
        }

    public Student createStudent(Student student) {
        return studentRepository.save(student);
    }
Enter fullscreen mode Exit fullscreen mode

PaginationController.java


    @GetMapping
    public ResponseEntity<List<Student>> getAllStudents(
            @RequestParam(defaultValue = "0") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "id") String sortBy)
    {
        List<Student> list = service.getAllStudents(pageNo, pageSize, sortBy);

        return new ResponseEntity<List<Student>>(list, new HttpHeaders(),HttpStatus.OK);
    }
Enter fullscreen mode Exit fullscreen mode

StudentController.java

@Autowired
    private StudentService studentService;

    @GetMapping("/student/search")
    public ResponseEntity<PaginatedStudentResponse> readStudent(Pageable pageable) {
        return ResponseEntity.ok(studentService.readStudent(pageable));
    }

    @PostMapping("/student")
    public ResponseEntity<Student> createProduct(@RequestBody Student student){
        return ResponseEntity.ok().body(this.studentService.createStudent(student));
    }
Enter fullscreen mode Exit fullscreen mode

Configuration

application.properties

spring.jpa.show-sql = true

# Enabling H2 Console
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true

# Enable Web Access
spring.h2.console.settings.web-allow-others=true

Enter fullscreen mode Exit fullscreen mode

Credits

https://howtodoinjava.com

Top comments (0)