Java 规范 CriteriaBuilder 复杂查询
Java Specification CriteriaBuilder complex query
我对规范、构建器、查询不是很有经验,我必须像这样进行相当复杂的查询:
select * from table where
code in ('code1', 'code2' //codes) and
(
(
date between "2020-03-23 //from" and "2020-03-30 //to"
and
status in ('Status1' , 'Status2' //status)
)
or
(
date between "2021-03-23" and "2021-03-30"
and
status in ('Status3' , 'Status4')
)
)
我有一个这样的 DTO:
public class SearchCriteria {
@Embedded
private Filters filters;
@Embeddable
@Getter
@Setter
public static class Filters {
private List<String> codes;
private List<TimePeriod> timePeriods;
}
@Embeddable
@Getter
@Setter
public static class TimePeriod {
private List<String> status;
private StartDate startDate;
}
@Embeddable
@Getter
@Setter
public static class StartDate {
private LocalDate from;
private LocalDate to;
}
这对我来说很难。我正在尝试一切。我更愿意向您展示一个具体案例,以免 运行 引起误解。有人可以帮我吗?非常感谢!
我不需要使用规范,我只需要能够重现该查询示例,规范似乎是最佳选择。
谢谢大家
我认为您的方向是正确的,条件 class 看起来不错。
以下是如何在方法中使用它来构建 JPA 条件并使用与您的实体对应的存储库执行查询:
public void query(List<String> codes, List<TimePeriod> timePeriods) {
// build the code filter
Specification<Table> codeSpec = (root, query, criteriaBuilder) -> {
Path<String> codeField = root.get("code");
var codePredicate = criteriaBuilder.in(codeField);
codes.forEach(code -> codePredicate.value(code));
return codePredicate;
};
// iterate over the time periods
var timePeriodSpec = timePeriods.stream().map(timePeriod -> {
Specification<Table> dateSpec = (root, query, criteriaBuilder) -> {
Path<LocalDate> dateField = root.get("date");
return criteriaBuilder.between(dateField, timePeriod.startDate.from, timePeriod.startDate.to);
};
Specification<Table> statusSpec = (root, query, criteriaBuilder) -> {
Path<String> statusField = root.get("status");
var statusPredicate = criteriaBuilder.in(statusField);
timePeriod.status.forEach(status -> statusPredicate.value(status));
return statusPredicate;
};
// combine the date and status filter
return dateSpec.and(statusSpec);
})
.reduce(Specification::or).get(); // chain the time period filters together
var fullSpec = codeSpec.and(timePeriodSpec);
var result = tableRepository.findAll(fullSpec, Pageable.unpaged());
}
您还需要确保您的存储库实现了 JpaSpecificationExecutor 接口,但您可能已经想到了这一点。
我对规范、构建器、查询不是很有经验,我必须像这样进行相当复杂的查询:
select * from table where
code in ('code1', 'code2' //codes) and
(
(
date between "2020-03-23 //from" and "2020-03-30 //to"
and
status in ('Status1' , 'Status2' //status)
)
or
(
date between "2021-03-23" and "2021-03-30"
and
status in ('Status3' , 'Status4')
)
)
我有一个这样的 DTO:
public class SearchCriteria {
@Embedded
private Filters filters;
@Embeddable
@Getter
@Setter
public static class Filters {
private List<String> codes;
private List<TimePeriod> timePeriods;
}
@Embeddable
@Getter
@Setter
public static class TimePeriod {
private List<String> status;
private StartDate startDate;
}
@Embeddable
@Getter
@Setter
public static class StartDate {
private LocalDate from;
private LocalDate to;
}
这对我来说很难。我正在尝试一切。我更愿意向您展示一个具体案例,以免 运行 引起误解。有人可以帮我吗?非常感谢!
我不需要使用规范,我只需要能够重现该查询示例,规范似乎是最佳选择。
谢谢大家
我认为您的方向是正确的,条件 class 看起来不错。 以下是如何在方法中使用它来构建 JPA 条件并使用与您的实体对应的存储库执行查询:
public void query(List<String> codes, List<TimePeriod> timePeriods) {
// build the code filter
Specification<Table> codeSpec = (root, query, criteriaBuilder) -> {
Path<String> codeField = root.get("code");
var codePredicate = criteriaBuilder.in(codeField);
codes.forEach(code -> codePredicate.value(code));
return codePredicate;
};
// iterate over the time periods
var timePeriodSpec = timePeriods.stream().map(timePeriod -> {
Specification<Table> dateSpec = (root, query, criteriaBuilder) -> {
Path<LocalDate> dateField = root.get("date");
return criteriaBuilder.between(dateField, timePeriod.startDate.from, timePeriod.startDate.to);
};
Specification<Table> statusSpec = (root, query, criteriaBuilder) -> {
Path<String> statusField = root.get("status");
var statusPredicate = criteriaBuilder.in(statusField);
timePeriod.status.forEach(status -> statusPredicate.value(status));
return statusPredicate;
};
// combine the date and status filter
return dateSpec.and(statusSpec);
})
.reduce(Specification::or).get(); // chain the time period filters together
var fullSpec = codeSpec.and(timePeriodSpec);
var result = tableRepository.findAll(fullSpec, Pageable.unpaged());
}
您还需要确保您的存储库实现了 JpaSpecificationExecutor 接口,但您可能已经想到了这一点。