The goal of the Spring Data Repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores

Using Spring Data Repository

Define a domain class-specific repository interface and its query methods. The interface must extend Repository and be typed to the domain class and an ID type.

import org.springframework.data.repository.Repository;

public interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByLastname(String lastname);
  // more query methods
}
  • If you want to expose CRUD methods for that domain type, extend CrudRepository instead of Repository
  • If you want to expose paging & sorting features methods for that domain type, extend PagingAndSortingRepository instead of Repository

Set up Spring to create proxy instances for those interfaces

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
public class Config {}

To customize the package to scan, use one of the basePackage… attributes of the data-store-specific repository’s @Enable${store}Repositories-annotation

Subpages