Java 如何使ExampleMatcher只包含一个属性?
如何实现ExampleMatcher,从我的类中随机包含一个属性,而忽略其他属性 假设我的班级是这样的:Java 如何使ExampleMatcher只包含一个属性?,java,spring-data,matcher,query-by-example,Java,Spring Data,Matcher,Query By Example,如何实现ExampleMatcher,从我的类中随机包含一个属性,而忽略其他属性 假设我的班级是这样的: Public Class Teacher() { String id; String name; String address; String phone; int area; ..other properties is here... } 如果我想通过名称匹配: Teacher TeacherExample = new Teacher("P
Public Class Teacher() {
String id;
String name;
String address;
String phone;
int area;
..other properties is here...
}
如果我想通过名称匹配:
Teacher TeacherExample = new Teacher("Peter");
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase()
.withIgnorePaths("id", "address", "phone","area",...); //no name
如果我想通过地址匹配:
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase()
.withIgnorePaths("id", "name", "phone","area",...); //no address
因此,我需要用ignorepath(..)重复,
如何避免这种情况?尝试以下方法:
Teacher t = new Teacher("Peter");
Example<Teacher> te = Example.of(t,
ExampleMatcher.matching()
.withStringMatcher(StringMatcher.CONTAINING)
.withIgnoreCase());
或者,如果您创建了日期
,则按创建进行搜索:
t.setCreated(someDate);
您甚至可以将它们全部设置为通过应用它们来缩小搜索范围
从
静态示例匹配器匹配()(&static ExampleMatcher matchingAll()) 创建一个新的ExampleMatcher,默认情况下包含所有非空属性,以匹配从该示例派生的所有谓词
我认为这对我不起作用,因为我还有其他数字类型的属性。(内部区域)。当我创建新教师时,java默认情况下,该区域将被0填充。(不能为空),因为它是整数。而且,如果我还有datetime类型的属性,该怎么办?@JiguJigu更新了答案。同时用新的细节更新您的问题,而不是将其作为注释,这有助于其他人回答。我将属性类型更改为INTEGER。非常感谢。
t.setCreated(someDate);