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/