zoukankan      html  css  js  c++  java
  • Spring Data JPA @EnableJpaRepositories配置详解

    @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

    • 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注解对应的方法

    	@Bean
    	public JpaTransactionManager transactionManager() {
    		JpaTransactionManager transactionManager = new JpaTransactionManager();
    		transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    		return transactionManager;
    	}
    

      

  • 相关阅读:
    修正MYSQL错误数据的一个存储过程
    解决教学平台上文件中存在无扩展名BUG的办法
    使用C#解析XMIND文件格式
    ASPOSE的示例下载地址
    Https所涉及名词及相关后缀名解释
    Https单向认证和双向认证介绍
    转Postman请求Https接口
    About the Apple Captive Network Assistant
    python之numpy的基本使用
    Python Numpy 数组的初始化和基本操作
  • 原文地址:https://www.cnblogs.com/kaihe/p/4707995.html
Copyright © 2011-2022 走看看