[英]Howto make ExampleMatcher with containing just one property?
Howto implement ExampleMatcher, with containinig just one property at randomly from my class and ignore the other properties? 如何实现ExampleMatcher,使用containinig从类中随机选择一个属性,而忽略其他属性?
Assume my class like this : 假设我的班级是这样的:
Public Class Teacher() {
String id;
String name;
String address;
String phone;
int area;
..other properties is here...
}
If I want to match by the name: 如果要按名称进行匹配:
Teacher TeacherExample = new Teacher("Peter");
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase()
.withIgnorePaths("id", "address", "phone","area",...); //no name
and if I want to match by the address: 如果我想按地址进行匹配:
ExampleMatcher matcher = ExampleMatcher.matchingAny()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase()
.withIgnorePaths("id", "name", "phone","area",...); //no address
so I need to repeat the withIgnorePaths(..)
How to avoid that? 所以我需要重复
withIgnorePaths(..)
如何避免这种情况?
Try this: 尝试这个:
Teacher t = new Teacher("Peter");
Example<Teacher> te = Example.of(t,
ExampleMatcher.matching()
.withStringMatcher(StringMatcher.CONTAINING)
.withIgnoreCase());
With ExampleMatcher.matching()
or ExampleMatcher.matchingAll()
comparison is done against all non-null fields in your example teacher t
so just name (assumed from "Peter"). 随着
ExampleMatcher.matching()
或ExampleMatcher.matchingAll()
比较是针对所有非空字段在你的榜样老师做t
所以只是名称(从“彼得”假设)。
NOTE : with primitive values you just need to add them to withIgnorePaths(..)
or change them to boxed types like int -> Integer
, there are no other easy workarounds. 注意 :使用原始值,您只需要将它们添加到
withIgnorePaths(..)
或将它们更改为类似withIgnorePaths(..)
int -> Integer
框式类型,就没有其他简单的解决方法。
If you need to search only by int area
set no name but are in your example t
如果你只需要通过搜索
int area
设置没有名字,但在你的例子t
t.setArea(55);
or if you had Date created
, searching by created: 或者,如果您
Date created
了Date created
,则按创建者进行搜索:
t.setCreated(someDate);
you can even set them all to narrow the search by applying them all. 您甚至可以将它们全部设置为通过全部应用来缩小搜索范围。
static ExampleMatcher matching()
静态ExampleMatcher matching()
( & static ExampleMatcher matchingAll() )(&静态ExampleMatcher matchingAll())
Create a new ExampleMatcher including all non-null properties by default matching all predicates derived from the example.
默认情况下,创建一个包含所有非空属性的新ExampleMatcher,以匹配从该示例派生的所有谓词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.