Java Spring数据合并
我使用Spring Boot编写了一个web应用程序,现在我面临以下问题: 我有以下服务类别:Java Spring数据合并,java,spring,spring-data-jpa,Java,Spring,Spring Data Jpa,我使用Spring Boot编写了一个web应用程序,现在我面临以下问题: 我有以下服务类别: @Service class ExampleService { @Autowired ARepository aRepository; @Autowired BRepository bRepository; @Autowired CRepository cRepository; } 所有存储库接口都将扩展 JpaRepository<MatchingClass, Integer>
@Service
class ExampleService {
@Autowired
ARepository aRepository;
@Autowired
BRepository bRepository;
@Autowired
CRepository cRepository;
}
所有存储库接口都将扩展
JpaRepository<MatchingClass, Integer>
JpaRepository
现在,我想对每个存储库执行以下crud操作:
public List<AClass> getAll() {
List<AClass> aElements = new List<>();
aRepository.findAll().forEach(x->aElements.add(x));
return aElements;
}
public AClass getOne(Integer id) { return aRepository.getOne(id);}
public void addOne(AClass aClass) { aRepository.save(aClass);}
public void deleteOne(Integer id) {aRepository.delete(id);}
}
public List getAll(){
列表元素=新列表();
aRepository.findAll().forEach(x->aeelements.add(x));
返回元素;
}
public AClass getOne(整数id){return aRepository.getOne(id);}
public void addOne(AClass AClass){aRepository.save(AClass);}
public void deleteOne(整数id){aRepository.delete(id);}
}
如何在不重复使用不同参数类型的方法的情况下实现它?我对java中的泛型有基本的了解,但我不确定spring数据中是否允许使用泛型,以及如何正确地完成泛型 如果您的存储库接口已经在扩展
JpaRepository
,那么您不需要方法deleteOne
,addOne
,getOne
,您可以直接使用JpaRepository
中的方法
例如,只需从您的服务中调用方法delete
、save
、findOne
,等等:
aRepository.save(aClassEntity);
检查
org.springframework.data.repository.crudepository
是否正在使用Java8?一个扩展JPARepository的接口,其中所有方法都作为默认接口方法实现,应该可以做到这一点。