@EnableJpaRepositories annotations are used for srping JPA code configuration, to replace configuration files in XML form, @EnableJpaRepositories supported configuration forms are rich and versatile, this article explains in detail.
1. Simple Configuration
@EnableJpaRepositories ("Com.spr.repository")
The simple configuration supports multiple package formats as follows:
@EnableJpaRepositories ({"Com.cshtong.sample.repository", "Com.cshtong.tower.repository"})
2, single-value and multi-group value configuration method
Most annotations can support a single annotation method and multiple annotations, and multiple annotations typically take a set of data that is contained in the "{}" symbol.
For example: "x.y.z" in the form of a string = {"X.y.z", "A.B.C"}
Category: A.class = {a.class, b.class}
3, the complete @enablejparepositories annotation
@EnableJpaRepositories ( basePackages = {}, basePackageClasses = {}, includeFilters = {}, excludeFilters = {}, repositoryimplementationpostfix = "Impl", namedquerieslocation = "", //meta-inf/jpa-named-queries.properties querylookupstrategy= querylookupstrategy.key.create_if_not_found, //querylookupstrategy.key.x repositoryfactorybeanclass=jparepositoryfactorybean.class, //class Entitymanagerfactoryref= "Entitymanagerfactory", transactionmanagerref= " TransactionManager ", considernestedrepositories=false, enableDefaultTransactions=true )
The functions of each configuration item are explained separately below
1) basepackage
Used to configure the package and sub-package where the scan repositories is located. The configuration in a simple configuration is equivalent to this configuration value, and Basepackages can be configured as a single string or as a string array.
@EnableJpaRepositories (basepackages = "Com.cshtong")
Multiple package paths
@EnableJpaRepositories (basepackages = {"Com.cshtong.sample.repository", "Com.cshtong.tower.repository"}
2) basepackageclasses
Specifying the Repository class
@EnableJpaRepositories (basepackageclasses = bookrepository.class)
@EnableJpaRepositories (basepackageclasses = {shoprepository.class, organizationrepository.class})
Note: When testing, it is found that a repositories class of the configuration package class, the other repositores in the package will also be loaded
3) Includefilters
Filter with Componentscan Filter class
@EnableJpaRepositories (includefilters={@ComponentScan. Filter (Type=filtertype.annotation, Value=repository. Class)}
4) excludefilters
does not contain filters
@EnableJpaRepositories (excludefilters={@ComponentScan. Filter (Type=filtertype.annotation, VALUE=SERVICE.CLA SS), @ComponentScan. Filter (Type=filtertype.annotation, Value=controller.class)})
5) Repositoryimplementationpostfix
Implements the end of the class append, such as shoprepository, corresponding to the Shoprepositoryimpl
6) Namedquerieslocation
Named the location of SQL storage, default to Meta-inf/jpa-named-queries.properties
7) Querylookupstrategy
Build a policy for conditional queries, including three ways create,use_declared_query,create_if_not_found
Create: Automatically build queries by interface name
Use_declared_query: User claims query
Create_if_not_found: Search for user-declared, non-existent, build automatically
This policy is for scenarios where queries are automatically generated by interface name as follows
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
Specify the factory class for repository
9) Entitymanagerfactoryref
Entity Management Factory reference name, corresponding to the method that corresponds to the @bean annotation
@Beanpublic localcontainerentitymanagerfactorybean entitymanagerfactory () { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new localcontainerentitymanagerfactorybean (); Entitymanagerfactorybean.setdatasource (DataSource ()); Entitymanagerfactorybean.setpersistenceproviderclass (Hibernatepersistenceprovider.class); entitymanagerfactorybean.setpackagestoscan ( env.getrequiredproperty (Property_name_entitymanager_packages_to_scan )); entitymanagerfactorybean.setjpaproperties (Hibproperties ()); Return entitymanagerfactorybean;}
) Transactionmanagerref
Transaction Management factory reference name, corresponding to the method that corresponds to the @bean annotation
@Beanpublic Jpatransactionmanager TransactionManager () {Jpatransactionmanager TransactionManager = new JpaTransaction Manager (); Transactionmanager.setentitymanagerfactory (Entitymanagerfactory (). GetObject ()); return TransactionManager;}
Original address: http://www.linuxidc.com/Linux/2015-08/121198.htm
Spring Data JPA @EnableJpaRepositories Configuration