Java SpringDataJPA:使用规范实现自定义存储库行为
我想创建一个具有自定义行为的SpringDataJPA存储库,并使用s。我已经介绍了如何设置它,只是没有在自定义存储库中使用Spring数据的示例。如果可能的话,人们将如何做到这一点 我看不到一种方法可以将某种东西注入到采用规范的定制实现中。我认为将存储库的CRUD存储库部分注入自定义部分会很棘手,但这会导致循环实例化依赖关系Java SpringDataJPA:使用规范实现自定义存储库行为,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,我想创建一个具有自定义行为的SpringDataJPA存储库,并使用s。我已经介绍了如何设置它,只是没有在自定义存储库中使用Spring数据的示例。如果可能的话,人们将如何做到这一点 我看不到一种方法可以将某种东西注入到采用规范的定制实现中。我认为将存储库的CRUD存储库部分注入自定义部分会很棘手,但这会导致循环实例化依赖关系 我没有使用QueryDSL。谢谢 我想灵感的主要来源可能是SimpleParepository如何处理规范。要查看的关键点有: -它基本上是创建一个标准查询,并使用JP
我没有使用QueryDSL。谢谢 我想灵感的主要来源可能是
SimpleParepository
如何处理规范。要查看的关键点有:
- -它基本上是创建一个
,并使用JPA标准查询
根目录引导一个select。后者是否适用于您的用例已经取决于您了。我认为前者肯定适用
- -它基本上使用在
中生成的工件(即getQuery(…)
根
和
),并将给定的CriteriaQuery
规范应用于这些工件
public abstract class GenericServiceImpl<T extends GenericEntity, J extends JpaRepository<T, Long> & GenericRepository<T>> implements GenericService<T, Long> {
// constructor takes in specific repository
public GenericServiceImpl(J genericRepository) {
// save this to local var
}
// using the above repository, specific methods are programmed
}
public interface GenericRepository<T> {
// add any common methods to your entity hierarchy objects,
// so that you don't have to repeat them in each of the children entities
// since you will be extending from this interface
}
公共接口通用存储库{
//向实体层次结构对象添加任何常用方法,
//这样就不必在每个子实体中重复它们
//因为您将从此接口进行扩展
}
二,。将特定存储库从generic(步骤1)和JPARepository扩展为
public interface MySpecificEntityRepository extends GenericRepository<MySpecificEntity>, JpaRepository<MySpecificEntity, Long> {
// add all methods based on column names, entity graphs or JPQL that you would like to
// have here in addition to what's offered by JpaRepository
}
公共接口MySpecificEntityRepository扩展了GenericRepository,JpaRepository{
//添加所有基于列名、实体图或JPQL的方法
//除了JpaRepository提供的服务之外,还有
}
iii.在服务实现类中使用上述存储库
public interface GenericService<T extends GenericEntity, ID extends Serializable> {
// add specific methods you want to extend to user
}
公共接口通用服务{
//添加要扩展到用户的特定方法
}
public abstract class GenericServiceImpl<T extends GenericEntity, J extends JpaRepository<T, Long> & GenericRepository<T>> implements GenericService<T, Long> {
// constructor takes in specific repository
public GenericServiceImpl(J genericRepository) {
// save this to local var
}
// using the above repository, specific methods are programmed
}
公共抽象类GenericServiceImpl实现GenericService{
//构造函数接受特定的存储库
公共GenericServiceImpl(J genericRepository){
//将此保存到本地变量
}
//使用上述存储库,编写了具体的方法
}
public class MySpecificEntityServiceImpl extends GenericServiceImpl<MySpecificEntity, MySpecificEntityRepository> implements MySpecificEntityService {
// the specific repository is autowired
@Autowired
public MySpecificEntityServiceImpl(MySpecificEntityRepository genericRepository) {
super(genericRepository);
this.genericRepository = (MySpecificEntityRepository) genericRepository;
}
}
公共类MySpecificEntityServiceImpl扩展了GenericServiceImpl实现了MySpecificEntityService{
//特定的存储库是自动连接的
@自动连线
公共MySpecificationServiceImpl(MySpecificationEntityRepository genericRepository){
超级(一般保存);
this.genericRepository=(MySpecificEntityRepository)genericRepository;
}
}