通过示例 Spring 数据查询 [英] Query by Example Spring Data
问题描述
我有带日期字段的域对象人:
I have domain object Person with date fields:
public class Person {
@Id
private Long id;
private Date date
像这样构建示例:
Person person = new Person();
person.setSomeOtherFields("some fields");
Example<Person> example = Example.of(person);
如何创建具有日期范围的示例查询(搜索实体包含大于或等于某个日期且小于或等于另一个日期的日期)?
How i can create example query with date range (search entity contains date greater or equal from some date and less or equal some another date)?
推荐答案
Spring Data JPA 实例查询技术使用 Example
s 和 ExampleMatcher
s 转换实体实例到基础查询中.当前官方文档 阐明只有完全匹配可用于非字符串属性.由于您的要求涉及 java.util.Date
字段,因此您只能使用示例查询技术进行精确匹配.
The Spring Data JPA query-by-example technique uses Example
s and ExampleMatcher
s to convert entity instances into the underlying query. The current official documentation clarifies that only exact matching is available for non-string attributes. Since your requirement involves a java.util.Date
field, you can only have exact matching with the query-by-example technique.
您可以编写自己的 ExampleMatcher
,根据您的需要返回查询子句.
You could write your own ExampleMatcher
that returns query clauses according to your needs.
这篇关于通过示例 Spring 数据查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!