共计 2943 个字符,预计需要花费 8 分钟才能阅读完成。
此文章为 Spring Data JPA – Reference Documentation(2.1.9.RELEASE) 的备忘录。
Reference
Repository Query Keyword
Working with Spring Data Repositories
Core concepts
Repository
Combine method using find, count, get, delete, remove, read, distinct, OrderBy*Asc, and, lessThan, IgnoreCase, AllIgnoreCase.
Extended:
- CrudRepository: some simple method about CRUD, i guess.
- PagingAndSortingRepository:
Additional parameter candidates: Sort, Pageable(usage: PageRequest.of(1, 20)), Page can be returned.
Query method
package-info.java
4.3 Defining Repository Interface
Repository -> CrudRepository -> PagingAndSortingRepository -> JpaRepository(MangoDbRepository)
PageRequest.of(1, 20)
- By extending annotation before.
- By annotating with @RepositoryDefinition
Selectively expose crud method
@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.
Null handling for repository method
- Add @NonNullApi on the package-info.java to make non-null constrain work.
- Using @NonNull and @Nullable to fine-tune.
Distinguishing to Spring Data Module
- Extending @JpaRepository, using Spring Data JPA module.
- Domain annotated with @Entity, using Spring Data JPA module, MongoDb if @Document.
- Annotated with @EnableJpaRepositories and @EnableMongoRepositories with attributes basePackages to define packages to be scanned as the corresponding repository.
4.4 Defining Query Method
@EnableJpaRepositories
basePackages; queryLookupStrategy; repositoryBaseClass can be used to customize the base repository.
4.4.1 Query Lookup Strategies
4.4.2 Query Creation
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
4.5 Creating Repository Instances
4.6 Custom Implementations for Spring Data Repository
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.