Spring Data JPA-规格

问题描述

将规范传递给repository.findAll()

时遇到问题
Version:spring-boot-starter-data-jpa-2.2.4.RELEASE

存储库

@Repository
public interface ServerAttributesRepository extends JpaRepository<ServerAttributes,Integer>{
    public ServerAttributes findById(int Id);
}

当我通过规范调用repository.findAll时,我会出错

ServerAttributeSpecification spec1 = new ServerAttributeSpecification(
            new SearchCriteria("server",SearchOperation.EQUALITY,"1"));
    List<ServerAttributes> serverAttributesList = serverAttributesRepository.findAll(spec1);

错误

Cannot resolve method 'findAll(com.cloud.compareService.specification.ServerAttributeSpecification)'

我在我的JpaRepository中找不到findAll(Specification<T> spec)

enter image description here

参考:https://www.baeldung.com/rest-api-query-search-language-more-operations

解决方法

您需要扩展JpaSpecificationExecutor接口以获取对规范的支持,因此请尝试:

@Repository
public interface ServerAttributesRepository extends JpaRepository<ServerAttributes,Integer>,JpaSpecificationExecutor<ServerAttributes> {
    public ServerAttributes findById(int Id);
}