2020-05-18

Spring PagingAndSortingRepository example

Spring PagingAndSortingRepository example

interface PersonRepository extends PagingAndSortingRepository<Person, String> {

  List<Person> findByFirstname(String firstname);                                 

  List<Person> findByFirstnameOrderByLastname(String firstname, Pageable pageable);

  Person findByFirstnameAndLastname(String firstname, String lastname);           

  Person findFirstByLastname(String lastname);                                     

  @Query("SELECT * FROM person WHERE lastname = :lastname")
  List<Person> findByLastname(String lastname);                                   
}
The method shows a query for all people with the given lastname.
The query is derived by parsing the method name for constraints that can be concatenated with And and Or.
Thus, the method name results in a query expression of SELECT … FROM person WHERE firstname = :firstname.
Use Pageable to pass offset and sorting parameters to the database.
Find a single entity for the given criteria.
It completes with IncorrectResultSizeDataAccessException on non-unique results.
In contrast to <3>, the first entity is always emitted even if the query yields more result documents.
The findByLastname method shows a query for all people with the given last name.

No comments:

Post a Comment