Ash*_*rma 2 criteria-api spring-data-jpa spring-boot spring-rest
我需要使用Spring Boot REST和Spring Data JPA使用 Criteria API 来实现复杂的搜索功能。我需要提供如下所示的 RPI
,/college?select=*&where=name:DemoCollege并输入[1,2,3]location:LAstaff{firstName:foo, lastName:boo, workExp>10}
Collage 对象有name、location、type字段和staff列表。它有onetomany关系,Staff所以College可以有一名或多名工作人员。
基于 uri,我需要使用 criteria api 构建查询。
我发现实现该toPredicate()方法非常复杂org.springframework.data.jpa.domain.Specification。有没有这样的例子来处理如此复杂的搜索过滤器?
提前致谢。
我认为,就您而言,最好的选择是规范arg-resolver库,它提供了以声明方式构建规范的便捷方法。例如,这段代码:
@RequestMapping("/customers")
public Object findByName(
@And({
@Spec(path="registrationDate", params="registeredBefore", spec=DateBefore.class),
@Spec(path="lastName", spec=Like.class)}) Specification<Customer> customerSpec) {
return customerRepo.findAll(customerSpec);
}
Run Code Online (Sandbox Code Playgroud)
对应这个请求:
GET http://myhost/customers?registeredBefore=2015-01-18&lastName=Simpson
Run Code Online (Sandbox Code Playgroud)
它支持以下规范:Like、LikeIgnoreCase、Equal、EqualIgnoreCase、In、Null、NotNull、GreaterThan、GreaterThanOrEqual、LessThan、LessThanOrEqual、DateBetween、Join、Join fetch,并且它允许您组合它们,组成复杂的表达式。
另一个选择是Querydsl和Web 支持Spring Data 扩展。它还允许您构建“REST 查询语言”,但可能性较小。您可以在我的回答中阅读如何使用它:https ://stackoverflow.com/a/48596145
| 归档时间: |
|
| 查看次数: |
2223 次 |
| 最近记录: |