springboot2.1 之 JPA自定义BaseRepository

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/

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值