好,現(xiàn)在我要搭一個(gè)項(xiàng)目,我想定義一個(gè)BaseDao,然后直接讓其他模塊的dao直接繼承我這個(gè)BaseDao,BaseDao有通用的方法也有spring data jpa提供的便捷方法
那就新建一個(gè)BaseDao唄,這個(gè)BaseDao應(yīng)該是繼承JpaRepository,JpaSpecificationExecutor因?yàn)槲覀兿胗胹pring data jpa,然后你再定義自己一些常用方法,例如myMethod
@NoRepositoryBean
public interface BaseDao<T,ID extends Serializable> extends JpaRepository<T, ID>,JpaSpecificationExecutor<T> {
void myMethod();
}
接下來應(yīng)該就是實(shí)現(xiàn)了
public class BaseDaoImpl<T,ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BaseDao<T,ID>{
private final EntityManager em;
public BaseDaoImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
this.em = em;
}
@Override
public void myMethod() {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
繼承于SimpleJpaRepository不單可以讓我們獲取操作數(shù)據(jù)庫(kù)的EntityManager ,還可以實(shí)現(xiàn)JpaRepository,JpaSpecificationExecutor的方法,這樣我們就可以只寫我們自己方法的實(shí)現(xiàn)
接下來再定義下factory,他的作用是將我們的basedao的實(shí)現(xiàn)類扔給factorybean
public class BaseDaoFactory<S, ID extends Serializable> extends JpaRepositoryFactory {
public BaseDaoFactory(EntityManager entityManager) {
super(entityManager);
}
@SuppressWarnings({ "rawtypes", "unchecked", "hiding" })
@Override
protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata,
EntityManager entityManager) {
// TODO Auto-generated method stub
return new BaseDaoImpl(metadata.getDomainType(), entityManager);
}
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
// TODO Auto-generated method stub
return BaseDao.class;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
然后再定義一個(gè)factorybean,接到factory之后,把factory扔了spring data jpa
public class BaseDaoFactoryBean<R extends JpaRepository<S, ID>, S, ID extends Serializable>
extends JpaRepositoryFactoryBean<R, S, ID> {
@SuppressWarnings("rawtypes")
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
// TODO Auto-generated method stub
return new BaseDaoFactory(entityManager);
}
}
spring data jpa來接了,在配置文件配置自己定義的FactoryBean,這樣就可以直接使用你的baseDao了
<jpa:repositories base-package="com.liuxg.**.dao"
repository-impl-postfix="Impl"
query-lookup-strategy = "create-if-not-found"
factory-class="com.liuxg.base.dao.BaseDaoFactoryBean"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" >
</jpa:repositories>
然后下次我們要用,就這樣子用
public interface TaskDao extends BaseDao<Task,Long>{
}
在service層,你就可以用spring data jpa的方法和自己定義的一些通用方法了,如果你還想在自己模塊再定義一些方法,那參考
http://blog.csdn.net/yingxiake/article/details/51017699
然后我們就這么寫
public interface TaskDao extends BaseDao<Task,Long>,TaskDaoCustom<Task, Long>{
}
然后,畢竟有一些東西,框架還是幫我們不了啊,太復(fù)雜的業(yè)務(wù),想自己寫操作數(shù)據(jù)庫(kù),想自己寫sql原生語句,那咋辦啊,親,那就只能利用EntityManager了,利用jpa api直接自己干了,下次看看怎么用EntityManager自己干活了
|