BaseRepository 定義接口
@NoRepositoryBean
public interface BaseRepository<T,ID extends Serializable> extends JpaRepository<T,ID> {
/**
* 傳入SQL查詢語句 返回結果集
* @param sql語句
* @return List 的 數據集 Object[] 結果根據sql 映射的順序
*/
List<Object[]> listBySQL(String sql);
}
BaseRepositoryImpl 定義實現
public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T,ID> implements BaseRepository<T,ID> {
private final EntityManager entityManager;
BaseRepositoryImpl(JpaEntityInformation<T, ID> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
@SuppressWarnings("unchecked")
@Override
public List<Object[]> listBySQL(String sql) {
return entityManager.createNativeQuery(sql).getResultList();
}
}
BaseRepositoryFactoryBean :repository裝載工廠重寫
public class BaseRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID>{
public BaseRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
}
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
return new BaseRepositoryFactory(em);
}
// 用內部類完成工廠
private static class BaseRepositoryFactory<T, I extends Serializable>
extends JpaRepositoryFactory {
private final EntityManager em;
public BaseRepositoryFactory(EntityManager em) {
super(em);
this.em = em;
}
//設置=實現類是BaseRepositoryImpl
@Override
protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
JpaEntityInformation<?, Serializable> entityInformation = this.getEntityInformation(information.getDomainType());
Object repository = this.getTargetRepositoryViaReflection(information, new Object[]{entityInformation, entityManager});
Assert.isInstanceOf(BaseRepositoryImpl.class, repository);
return (JpaRepositoryImplementation)repository;
}
//設置自定義實現類class
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseRepositoryImpl.class;
}
}
}
最後一步是使Spring Data Infrastructure瞭解自定義存儲庫基類。在Java配置中,您可以使用註釋的repositoryBaseClass屬性來執行此操作@Enable${store}Repositories,如以下示例所示:
@ComponentScan
@Configuration
@EnableJpaRepositories(basePackages = {"com.example.demo.repository"},repositoryFactoryBeanClass = BaseRepositoryFactoryBean.class)//指定自己的工廠類
public class ApplicationConfig {}
(2)或者使用XML配置自定義存儲庫基類
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<repositories base-package=" 路徑" factory-class="類名 "></repositories>
</beans:beans>
官方文檔:https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/