Spring boot 将自定义方法添加到实现另一个存储库接口的抽象类中,以便在一个位置同时具有泛型方法和自定义方法
实体: 扩展JpaRepository的存储库:Spring boot 将自定义方法添加到实现另一个存储库接口的抽象类中,以便在一个位置同时具有泛型方法和自定义方法,spring-boot,hibernate,spring-data-jpa,Spring Boot,Hibernate,Spring Data Jpa,实体: 扩展JpaRepository的存储库: @Entity public class Person { @Id private String id; private String name; private int age; } 在服务中,我只想使用PersonRepositoryCustom,这样我就可以从PersonRepository以及PersonRepositoryCustom获得所有方法 像这样的 @Repository public abst
@Entity
public class Person {
@Id
private String id;
private String name;
private int age;
}
在服务中,我只想使用PersonRepositoryCustom,这样我就可以从PersonRepository以及PersonRepositoryCustom获得所有方法
像这样的
@Repository
public abstract class PersonRepositoryCustom implements PersonRepository {
@Autowired
EntityManager entityManager;
public void customMethod() {
// uses criteria builder, criteria query to create custom query using
}
}
我可以这样做吗?或者我如何才能做到这一点。您可以通过以下方式实现:
接口CustomPersonRepository{
void customMethod();
}
@存储库
类CustomPersonRepositoryImpl实现CustomPersonRepository{/*实现*/}
公共接口PersonRepository
扩展JpaRepository、CustomPersonRepository{
...
}
请注意,PersonRepository
扩展了CustomPersonRepository
现在您可以注入PersonRepository
,它将具有来自CustomPersonRepositoryImpl
@Repository
public abstract class PersonRepositoryCustom implements PersonRepository {
@Autowired
EntityManager entityManager;
public void customMethod() {
// uses criteria builder, criteria query to create custom query using
}
}
@Service
public class Service {
@Autowired
PersonRepositoryCustom personRepository;
// I want to access all methods like
public void method() {
personRepository.save();
personRepository.findPersonByNameAndAge("name", 20);
personRepository.findSomethingBySomething();
personRepository.customMethod();
personRepository.findById();
// ...
}
}