Skip to content

ashishcyn/spring-mvc-pagination

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-mvc-pagination

Step1:
Set up a spring boot, H2 project. Include lombok dependency as well

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
</dependency>

Step2:

  • Enable the H2 in the application.properties file
  • Prepare data.sql with student records for seeding the h2 DB

Step3:

  • Student Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Student {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String course;
    private int admissionYear;
}
  • Repository
public interface StudentRepository extends JpaRepository<Student, Long> {

    @Query("select s from Student s where name like %?1%")
    Page<Student> findByName(String name, Pageable pageable);

}
  • Service
@Service
@RequiredArgsConstructor
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public Page<Student> findByName(
            Optional<String> name,
            Optional<Integer> page,
            Optional<String> sortBy) {
        return studentRepository.findByName(name.orElse("_"),
                PageRequest.of(
                        page.orElse(0), 5,
                        Sort.Direction.ASC, sortBy.orElse("id")));

    }

}
  • Controller
@RestController
@RequiredArgsConstructor
public class StudentController {

    @Autowired
    private final StudentService studentService;

    @GetMapping("/students")
    public Page<Student> findByName(
            @RequestParam Optional<String> name,
            @RequestParam Optional<Integer> page,
            @RequestParam Optional<String> sortBy)
    {
        return studentService.findByName(name,page,sortBy);
    }
}

Step4: Run the spring boot application and verify the output
http://localhost:8080/students?name=john
http://localhost:8080/students?page=0
http://localhost:8080/students?page=0&sortBy=name

Happy Coding

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages