我正在尝试理解如何使用Spring Data的Query by Example功能,但我很难理解如何使用ExampleMatcher
及其各种with*
方法。匹配器的经典使用示例包括以下代码片段:
Person person = new Person();
person.setFirstname("Dave");
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnorePaths("lastname")
.withIncludeNullValues()
.withStringMatcherEnding();
Example<Person> example = Example.of(person, matcher);
出于某种原因,我就是理解不了这个DSL。我们来看看文档中的Person
示例。假设一个Person
实体长成这样:
// Pseudo code; omitting JPA annotations for this example
public class Person {
private String firstName;
private String lastName;
private Date dob;
private Long score;
// Getters, setters & constructor omitted
}
请问有人能给我展示如何构建一个ExampleMatcher
的例子,以便我可以找到符合以下标准的Person
记录:
- 名字以"Sme"开头;并且
- 姓氏长度不超过10个字符;并且
- 出生日期早于1970年01月01日;并且
- 分数在10到20之间(包括边界值)。
如果任何这些标准都无法使用ExampleMatcher
实现,那没关系,但能否有人告诉我哪些标准可以或者解释一下哪些方法可以让我实现相似的功能?