Use Spring Data to generate database query methods by method names.
Spring Data repositories implement a strategy for generating database query methods from the method's names themselves.
Spring Data JpaRepository:
interface JpaRepository<T, ID extends Serializable>
Create an interface repository for your model by extending JpaRepository like this:
interface AddressRepository extends JpaRepository<Address, Long>
In your interface you can add method signatures following Spring Data Query Methods' strategy:
Set<Customer> findByBirthdayBetween(LocalDate startDate, LocalDate endDate);
Set<Customer> findByAddress_ZipCode(String zipCode);
By following this strategy, Spring Data will generate implementation for these methods and you will be able to use them without having to write a single JPQL (Java Persistence Query Language) line nor the worst case, native SQL.
Check tests in this project for a better comprehension.
See this blog post for a complete guidance on implementing Spring Data Query Methods.
See Spring Data documentation for further details.
- Clone
mvn test