@EnableJpaRepositories注解用於Srping JPA的代碼配置,用於取代xml形式的配置文件,@EnableJpaRepositories支持的配置形式豐富多用,本篇文章詳細講解。
1、簡單配置
1 @EnableJpaRepositories("com.spr.repository")
簡單配置支持多個package,格式如下:
1 @EnableJpaRepositories({"com.cshtong.sample.repository", "com.cshtong.tower.repository"})
2、單值和多組值配置方式
大部分注解可以都支持單個注解方式和多個注解,多個注解通常采用"{}"符號包含的一組數據。
比如:字符串形式的 "x.y.z" => {"x.y.z","a.b.c"}
類別: A.class => {A.class, B.class}
3、完整的@EnableJpaRepositories注解
1 @EnableJpaRepositories( 2 basePackages = {}, 3 basePackageClasses = {}, 4 includeFilters = {}, 5 excludeFilters = {}, 6 repositoryImplementationPostfix = "Impl", 7 namedQueriesLocation = "",//META-INF/jpa-named-queries.properties 8 queryLookupStrategy=QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND, //QueryLookupStrategy.Key.x 9 repositoryFactoryBeanClass=JpaRepositoryFactoryBean.class, //class 10 entityManagerFactoryRef="entityManagerFactory", 11 transactionManagerRef="transactionManager", 12 considerNestedRepositories=false, 13 enableDefaultTransactions=true 14 )
下面分別解釋各個配置項的作用
1)basePackage
用於配置掃描Repositories所在的package及子package。簡單配置中的配置則等同於此項配置值,basePackages可以配置為單個字符串,也可以配置為字符串數組形式。
1 @EnableJpaRepositories( 2 basePackages = "com.cshtong")
多個包路徑
1 @EnableJpaRepositories( 2 basePackages = {"com.cshtong.sample.repository", "com.cshtong.tower.repository"})
2)basePackageClasses
指定 Repository 類
1 @EnableJpaRepositories(basePackageClasses = BookRepository.class)
1 @EnableJpaRepositories( 2 basePackageClasses = {ShopRepository.class, OrganizationRepository.class})
備注:測試的時候發現,配置包類的一個Repositories類,該包內其他Repositores也會被加載
3)includeFilters
過濾器,該過濾區采用ComponentScan的過濾器類
1 @EnableJpaRepositories( 2 includeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION, value=Repository.class)})
4)excludeFilters
不包含過濾器
1 @EnableJpaRepositories( 2 excludeFilters={ 3 @ComponentScan.Filter(type=FilterType.ANNOTATION, value=Service.class), 4 @ComponentScan.Filter(type=FilterType.ANNOTATION, value=Controller.class)})
5)repositoryImplementationPostfix
實現類追加的尾部,比如ShopRepository,對應的為ShopRepositoryImpl
6)namedQueriesLocation
named SQL存放的位置,默認為META-INF/jpa-named-queries.properties
7)queryLookupStrategy
構建條件查詢的策略,包含三種方式CREATE,USE_DECLARED_QUERY,CREATE_IF_NOT_FOUND
該策略針對如下通過接口名稱自動生成查詢的場景
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname); // Enables the distinct flag for the query List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname); List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname); // Enabling ignoring case for an individual property List<Person> findByLastnameIgnoreCase(String lastname); // Enabling ignoring case for all suitable properties List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname); // Enabling static ORDER BY for a query List<Person> findByLastnameOrderByFirstnameAsc(String lastname); List<Person> findByLastnameOrderByFirstnameDesc(String lastname)
8)repositoryFactoryBeanClass
指定Repository的工廠類
9)entityManagerFactoryRef
實體管理工廠引用名稱,對應到@Bean注解對應的方法
1 @Bean 2 public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 3 LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 4 entityManagerFactoryBean.setDataSource(dataSource()); 5 entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class); 6 entityManagerFactoryBean 7 .setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN)); 8 entityManagerFactoryBean.setJpaProperties(hibProperties()); 9 return entityManagerFactoryBean; 10 }
10)transactionManagerRef
事務管理工廠引用名稱,對應到@Bean注解對應的方法
1 2 3 4 5 6@Bean
public
JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager =
new
JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return
transactionManager;
}
Spring Data 的詳細介紹:請點這裡
Spring Data 的下載地址:請點這裡