meleto 2019-07-25
此文章为Spring Data JPA - Reference Documentation(2.1.9.RELEASE)的备忘录。
Repository Query Keyword
Combine method using find, count, get, delete, remove, read, distinct, OrderBy*Asc, and, lessThan, IgnoreCase, AllIgnoreCase.
Extended:
Additional parameter candidates: Sort, Pageable(usage: PageRequest.of(1, 20)), Page can be returned.
package-info.java
Repository -> CrudRepository -> PagingAndSortingRepository -> JpaRepository(MangoDbRepository)
PageRequest.of(1, 20)
@NoRepositoryBean interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> { Optional<T> findById(ID id); <S extends T> S save(S entity); } interface UserRepository extends MyBaseRepository<User, Long> { User findByEmailAddress(EmailAddress emailAddress); }
Make sure that you add @NoRepositoryBean to all repositories for which should not create an instance at runtime.
@EnableJpaRepositories
basePackages; queryLookupStrategy; repositoryBaseClass can be used to customize the base repository.
findTop10ByLastnameAndFirstnameAllIgnoreCaseOrderByGenderAcs
findByAddressZipCode: AddressZip.Code -> Address.ZipCode
findByAddress_ZipCode: Address.ZipCodeSlice<User> findByLastname(String lastname, Pageable pageable, Sort sort);
Stream<User>(Jave8 String<T>) can be returned.Future<User>, CompletableFuture<User>, ListenableFuture<User> can be used with annotaion @Async
interface HumanRepository { void someHumanMethod(User user); } // Impl postfix. In namespace configuration, postfix can be configured with attributes **repository-impl-postfix** in **repositories** tag // If this class is annotated with **@Camponent("beanNameImpl")**, thing changed. class HumanRepositoryImpl implements HumanRepository { public void someHumanMethod(User user) { // Your custom implementation } } interface ContactRepository { void someContactMethod(User user); User anotherContactMethod(User user); } class ContactRepositoryImpl implements ContactRepository { public void someContactMethod(User user) { // Your custom implementation } public User anotherContactMethod(User user) { // Your custom implementation } } // usage interface UserRepository extends CrudRepository<User, Long>, HumanRepository, ContactRepository { // Declare query methods here }
Custom implementations have a higher priority than base implementation. this feature let you can override base repository.