通过示例查询Spring数据 [英] Query by Example Spring Data
问题描述
我有域对象"Person"和日期字段:
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
和ExampleMatcher
将实体实例转换为基础查询. 当前的官方文档阐明仅精确匹配可用于非字符串属性.由于您的要求涉及一个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屋!