按示例查询 Spring 数据

Query by Example Spring Data

我有带日期字段的域对象 Person:

public class Person {
@Id
private Long id;
private Date date

像这样构建示例:

Person person = new Person();
person.setSomeOtherFields("some fields");
Example<Person> example = Example.of(person);

我如何创建具有日期范围的示例查询(搜索实体包含大于或等于某个日期且小于或等于另一个日期的日期)?

Spring Data JPA 按示例查询技术使用 Examples 和 ExampleMatchers 将实体实例转换为基础查询。 current official documentation 阐明只有精确匹配可用于非字符串属性。由于你的需求涉及到java.util.Date字段,只能通过实例查询的方式进行精确匹配

您可以根据需要编写自己的 ExampleMatcher that returns 查询子句。