前几天看了springside4的mini-web代码发现确实有不少新的东东,咱这次单说说Spring Data JPA吧。
引用springside4的 wiki关于对Spring Data JPA的简介
Spring Data JPA在JPA上又做了一层封装,只要编写接口就够了,不用写一行实现代码,CRUD方法啦,分页啦,自动将findByLoginName()的方法定义翻译成适当的QL啦都由它包了:
public interface UserDao extends PagingAndSortingRepository<User, Long> {
User findByLoginName(String loginName);
}
使用上很简单,快速浏览一下下面的资料就够了。
只有一个坑爹的地方,如果要为UserDao扩展方法(而不是接口),要新增一个UserDaoCustom接口,这时候,实现类的名字必须是UserDaoImpl,而不是UserDaoCustomImpl。
另外,除了智能地翻译连Less,Not,And,Or都支持的方法名,它当然也可以直接用@Query在方法上标注复杂的查询语句。
资料
•官方文档
•使用 Spring Data JPA 简化 JPA 开发 IBM DW上的中文版教程.
如果看完上面的资料 也许你对Spring Data JPA有了初步的认识,动动手你就知道他的强大,但是对DAO的扩展上有点麻烦,上面红字部分是对单个dao进行扩展的方法。下面我们来说一下对全局DAO的扩展,创建你自己的CustomRepository。
首先咱们要建一个自己的扩展接口类MyRepository
- importjava.io.Serializable;
-
- importorg.springframework.data.jpa.repository.JpaRepository;
-
- publicinterfaceMyRepository<T,IDextendsSerializable>
- extendsJpaRepository<T,ID>{
-
- StringsharedCustomMethod();
- }
- importjava.io.Serializable;
-
- importorg.springframework.data.jpa.repository.JpaRepository;
-
- publicinterfaceMyRepository<T,IDextendsSerializable>
- extendsJpaRepository<T,ID>{
-
- StringsharedCustomMethod();
- }
其中sharedCustomMethod是全局的共享自定义方法。
然后在建一个实现类MyCustomRepository
- importjava.io.Serializable;
-
- importjavax.persistence.EntityManager;
-
- importorg.springframework.data.jpa.repository.support.JpaEntityInformation;
- importorg.springframework.data.jpa.repository.support.SimpleJpaRepository;
- importorg.springframework.data.repository.NoRepositoryBean;
-
- @NoRepositoryBean
- publicclassMyCustomRepository<T,IDextendsSerializable>
- extendsSimpleJpaRepository<T,ID>implementsMyRepository<T,ID>{
-
-
- privatefinalEntityManagerentityManager;
-
- publicMyCustomRepository(Class<T>domainClass,EntityManagerem){
- super(domainClass,em);
-
-
- entityManager=em;
- }
-
- publicMyCustomRepository(finalJpaEntityInformation<T,?>entityInformation,finalEntityManagerentityManager){
- super(entityInformation,entityManager);
- this.entityManager=entityManager;
- }
-
-
- publicStringsharedCustomMethod(){
- return"hellosharedCustomMethod";
-
- }
- }
- importjava.io.Serializable;
-
- importjavax.persistence.EntityManager;
-
- importorg.springframework.data.jpa.repository.support.JpaEntityInformation;
- importorg.springframework.data.jpa.repository.support.SimpleJpaRepository;
- importorg.springframework.data.repository.NoRepositoryBean;
-
- @NoRepositoryBean
- publicclassMyCustomRepository<T,IDextendsSerializable>
- extendsSimpleJpaRepository<T,ID>implementsMyRepository<T,ID>{
-
-
- privatefinalEntityManagerentityManager;
-
- publicMyCustomRepository(Class<T>domainClass,EntityManagerem){
- super(domainClass,em);
-
-
- entityManager=em;
- }
-
- publicMyCustomRepository(finalJpaEntityInformation<T,?>entityInformation,finalEntityManagerentityManager){
- super(entityInformation,entityManager);
- this.entityManager=entityManager;
- }
-
-
- publicStringsharedCustomMethod(){
- return"hellosharedCustomMethod";
-
- }
- }
注意 @NoRepositoryBean一定要有的,还有全局的扩展实现类不要用Imp作为后缀名,不然会报异常的(目前还没搞清楚报异常的具体原因,个人猜测可能是和局部的扩展有冲突吧)。
然后在定义MyRepositoryFactory
- importjava.io.Serializable;
-
- importstaticorg.mockito.Mockito.*;
-
- importjavax.persistence.EntityManager;
-
- importorg.springframework.data.jpa.repository.JpaRepository;
- importorg.springframework.data.jpa.repository.support.JpaEntityInformation;
- importorg.springframework.data.jpa.repository.support.JpaRepositoryFactory;
- importorg.springframework.data.repository.core.RepositoryMetadata;
-
-
- publicclassMyRepositoryFactoryextendsJpaRepositoryFactory{
-
- publicMyRepositoryFactory(EntityManagerentityManager){
- super(entityManager);
-
- }
- @Override
- @SuppressWarnings("unchecked")
- protectedJpaRepository<?,?>getTargetRepository(RepositoryMetadatametadata,EntityManagerem){
-
- JpaEntityInformation<Object,Serializable>entityMetadata=mock(JpaEntityInformation.class);
- when(entityMetadata.getJavaType()).thenReturn((Class<Object>)metadata.getDomainType());
- returnnewMyCustomRepository<Object,Serializable>(entityMetadata,em);
- }
-
-
-
-
-
-
-
-
- @Override
- protectedClass<?>getRepositoryBaseClass(RepositoryMetadatametadata){
-
- returnMyCustomRepository.class;
- }
- }
- importjava.io.Serializable;
-
- importstaticorg.mockito.Mockito.*;
-
- importjavax.persistence.EntityManager;
-
- importorg.springframework.data.jpa.repository.JpaRepository;
- importorg.springframework.data.jpa.repository.support.JpaEntityInformation;
- importorg.springframework.data.jpa.repository.support.JpaRepositoryFactory;
- importorg.springframework.data.repository.core.RepositoryMetadata;
-
-
- publicclassMyRepositoryFactoryextendsJpaRepositoryFactory{
-
- publicMyRepositoryFactory(EntityManagerentityManager){
- super(entityManager);
-
- }
- @Override
- @SuppressWarnings("unchecked")
- protectedJpaRepository<?,?>getTargetRepository(RepositoryMetadatametadata,EntityManagerem){
-
- JpaEntityInformation<Object,Serializable>entityMetadata=mock(JpaEntityInformation.class);
- when(entityMetadata.getJavaType()).thenReturn((Class<Object>)metadata.getDomainType());
- returnnewMyCustomRepository<Object,Serializable>(entityMetadata,em);
- }
-
-
-
-
-
-
-
-
- @Override
- protectedClass<?>getRepositoryBaseClass(RepositoryMetadatametadata){
-
- returnMyCustomRepository.class;
- }
- }
接着在建一个MyRepositoryFactoryBean
- importjava.io.Serializable;
-
- importjavax.persistence.EntityManager;
-
- importorg.springframework.data.jpa.repository.JpaRepository;
- importorg.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
- importorg.springframework.data.repository.core.support.RepositoryFactorySupport;
-
- publicclassMyRepositoryFactoryBean<TextendsJpaRepository<Object,Serializable>>extendsJpaRepositoryFactoryBean<T,Object,Serializable>{
-
-
- @Override
- protectedRepositoryFactorySupportcreateRepositoryFactory(EntityManagerem){
-
- returnnewMyRepositoryFactory(em);
- }
- }
- importjava.io.Serializable;
-
- importjavax.persistence.EntityManager;
-
- importorg.springframework.data.jpa.repository.JpaRepository;
- importorg.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
- importorg.springframework.data.repository.core.support.RepositoryFactorySupport;
-
- publicclassMyRepositoryFactoryBean<TextendsJpaRepository<Object,Serializable>>extendsJpaRepositoryFactoryBean<T,Object,Serializable>{
-
-
- @Override
- protectedRepositoryFactorySupportcreateRepositoryFactory(EntityManagerem){
-
- returnnewMyRepositoryFactory(em);
- }
- }
最后在配置文件里需要定义
<jpa:repositories base-package="org.springside.examples.miniweb" transaction-manager-ref="transactionManager"
factory-class="org.springside.examples.miniweb.dao.account.MyRepositoryFactoryBean"
entity-manager-factory-ref="entityManagerFactory"/>
其实这里还有个repository-impl-postfix=" "个人理解他是来定义局部扩展库的实现类的后缀的默认是Imp。
呵呵 这样你就可以使用全局的自定义扩展库了。