Spring Data Jpa - 类型规范已弃用

2023-12-02

我正在实现链接中的逻辑:Spring Data - 多列搜索我想要搜索的地方FirstName.

根据链接:https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/Specifications.html

EmployeeSpecification.java

public class EmployeeSpecification {
    public static Specification<Employee> textInAllColumns(String text) {
        if (!text.contains("%")) {
            text = "%" + text + "%";
        }
        final String finalText = text;

        return new Specification<Employee>() {
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<Employee> cq, CriteriaBuilder builder) {
                return builder.or(root.getModel().getDeclaredSingularAttributes().stream().filter(a -> {
                    if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
                        return true;
                    } else {
                        return false;
                    }
                }).map(a -> builder.like(root.get(a.getName()), finalText)).toArray(Predicate[]::new));
            }
        };
    }
}

EmployeeRepository.java

public interface EmployeeRepository extends JpaRepository<Employee, Long>{
    List<Employee> findAll(Specification<Employee> spec);
}

EmployeeServiceImpl.java

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;

    @Override
    public void findAllCustomersByFirstName(String firstName) {
        employeeRepository.findAll(Specifications.where(EmployeeSpecification.textInAllColumns(firstName)));
    }
}

Error:

该行有多个标记 - 类型规范中的其中(规范)方法不适用于参数 (规格) - 类型规格已弃用

enter image description here


您的回购代码需要扩展JpaSpecificationExecutor像那样:

public interface EmployeeRepository extends JpaRepository<Employee, Long>, 
    JpaSpecificationExecutor<Employee> {
}

JpaSpecificationExecutor 有那些可以调用的方法:

public interface JpaSpecificationExecutor<T> {
    Optional<T> findOne(@Nullable Specification<T> var1);

    List<T> findAll(@Nullable Specification<T> var1);

    Page<T> findAll(@Nullable Specification<T> var1, Pageable var2);

    List<T> findAll(@Nullable Specification<T> var1, Sort var2);

    long count(@Nullable Specification<T> var1);
}

然后你可以这样做:

public void findAllCustomersByFirstName(String firstName) {
    employeeRepository.findAll(
            EmployeeSpecification.textInAllColumns(firstName)
    );
}

我更改了您的规范以使用 lambda:

public class EmployeeSpecification {
    public static Specification<Employee> textInAllColumns(String text) {
        if (!text.contains("%")) {
            text = "%" + text + "%";
        }
        final String finalText = text;

        return  (Specification<Employee>) (root, query, builder) -> 
                builder.or(root.getModel().getDeclaredSingularAttributes().stream().filter(a -> {
                if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
                    return true;
                } else {
                    return false;
                }
            }).map(a -> builder.like(root.get(a.getName()), finalText)).toArray(Predicate[]::new));
    }
}

您可以在此处查看答案中代码的更新版本:https://github.com/zifnab87/spring-boot-rest-api-helpers/blob/26501c1d6afcd6afa8ea43c121898db85b4e5dbe/src/main/java/springboot/rest/specifications/CustomSpecifications.java#L172

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Data Jpa - 类型规范已弃用 的相关文章

随机推荐