java - Spring Data Jpa - 类型规范已弃用
问题描述
我正在实现链接中的逻辑:Spring Data - Multi-column search where I want to search by FirstName
。
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)));
}
}
错误:
此行有多个标记 - 类型规范中的方法 where(规范) 不适用于参数 (规范) - 不推荐使用类型规范
解决方案
您的回购代码需要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
推荐阅读
- string - Powershell - 从字符串中提取句子的最佳方法是什么
- python - 如何同时运行 python flask 应用程序和 webview
- etl - 如何使用 Talend 从 CSV 中提取键值对
- algorithm - 在扑克中分配边底池
- optimization - Julia:NLsolve 函数内部函数
- vue.js - 安装异步 axios 时未更新存储副本
- python - 从数据框中创建逗号分隔列表以传递到 SQL 查询时出错
- docker - Dockerfile 环境变量
- mysql - 从远程机器通过 SSH 的 Docker 命令
- mysql - 如何从 MySQL 中的比较和空检查中指定 ORDER BY?