Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 将自定义方法添加到实现另一个存储库接口的抽象类中,以便在一个位置同时具有泛型方法和自定义方法_Spring Boot_Hibernate_Spring Data Jpa - Fatal编程技术网

Spring boot 将自定义方法添加到实现另一个存储库接口的抽象类中,以便在一个位置同时具有泛型方法和自定义方法

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

实体:

扩展JpaRepository的存储库:

@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();
        // ...
    } 
}