Spring Data JPA @EnableJpaRepositories配置详解

时间:2024-04-11 15:07:47

@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注解

Spring Data JPA @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 )
Spring Data JPA @EnableJpaRepositories配置详解

下面分别解释各个配置项的作用

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

  • CREATE:按照接口名称自动构建查询
  • USE_DECLARED_QUERY:用户声明查询
  • CREATE_IF_NOT_FOUND:先搜索用户声明的,不存在则自动构建

该策略针对如下通过接口名称自动生成查询的场景

Spring Data JPA @EnableJpaRepositories配置详解
  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)
Spring Data JPA @EnableJpaRepositories配置详解

8)repositoryFactoryBeanClass

指定Repository的工厂类

9)entityManagerFactoryRef

实体管理工厂引用名称,对应到@Bean注解对应的方法

Spring Data JPA @EnableJpaRepositories配置详解
 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 }
Spring Data JPA @EnableJpaRepositories配置详解

10)transactionManagerRef

事务管理工厂引用名称,对应到@Bean注解对应的方法

1
2
3
4
5
6
@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}