Spring 数据示例匹配器示例
Spring Data ExampleMatchers by Example
我正在尝试了解如何使用 Spring 数据的 Query by Example capabilities, and am struggling to understand how to use 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
无法满足这些条件中的任何一个,那没关系,但有人可以告诉我哪些是或解释哪些方法可以让我 关闭 我在找什么?
您可以获得 firstName 以 "Sme" 开头且 score=50
的记录
Person person = new Person();
person.setFirstName("Sme");
person.setScore(50L);
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstName", startsWith())
.withMatcher("score", exact());
Example<History> example = Example.of(person, matcher);
personRepository.findAll(example)
我正在尝试了解如何使用 Spring 数据的 Query by Example capabilities, and am struggling to understand how to use 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
无法满足这些条件中的任何一个,那没关系,但有人可以告诉我哪些是或解释哪些方法可以让我 关闭 我在找什么?
您可以获得 firstName 以 "Sme" 开头且 score=50
的记录Person person = new Person();
person.setFirstName("Sme");
person.setScore(50L);
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstName", startsWith())
.withMatcher("score", exact());
Example<History> example = Example.of(person, matcher);
personRepository.findAll(example)