Spring Data JPA - Reference Documentation Memorandum
此文章为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.
相关推荐
yupi0 2020-10-10
spring 2020-08-18
编程点滴 2020-07-29
幸运小侯子 2020-07-05
itjavashuai 2020-07-04
qingjiuquan 2020-06-29
shushan 2020-06-25
小鱿鱼 2020-06-22
咻pur慢 2020-06-18
melonjj 2020-06-17
qingjiuquan 2020-06-13
neweastsun 2020-06-05
小鱿鱼 2020-06-05
mxcsdn 2020-05-31
吾日五省我身 2020-05-27
牧场SZShepherd 2020-05-27
sweetgirl0 2020-05-14