java - 如何使 ExampleMatcher 只包含一个属性?
问题描述
如何实现 ExampleMatcher,从我的类中随机只包含一个属性而忽略其他属性?
假设我的班级是这样的:
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
所以我需要重复withIgnorePaths(..)
如何避免这种情况?
解决方案
试试这个:
Teacher t = new Teacher("Peter");
Example<Teacher> te = Example.of(t,
ExampleMatcher.matching()
.withStringMatcher(StringMatcher.CONTAINING)
.withIgnoreCase());
与示例教师中的所有非空字段进行比较ExampleMatcher.matching()
或比较,因此只需命名(假设来自“Peter”)。ExampleMatcher.matchingAll()
t
注意:对于原始值,您只需将它们添加到withIgnorePaths(..)
或更改为盒装类型,例如int -> Integer
,没有其他简单的解决方法。
如果您只需要通过int area
不设置名称进行搜索,但在您的示例中t
t.setArea(55);
或者如果你有Date created
,搜索创建:
t.setCreated(someDate);
您甚至可以将它们全部设置为通过应用它们来缩小搜索范围。
从文档
静态 ExampleMatcher 匹配()
( & 静态 ExampleMatcher matchingAll() )创建一个包含所有非空属性的新 ExampleMatcher,默认匹配从示例派生的所有谓词。
推荐阅读
- angular - 如何在角度 7 的 concatMap/switchMap 中使用检查条件
- c# - 在 Visual Studio Code 中调试时对象的条件表达式
- python - 如何从python中的行元素中获取最大值?
- r - 如何在 R 项目中使用 fviz_cluster 更改符号和颜色
- angular - 我们如何使用角垫表按姓氏排序或按字母顺序排列?
- c++ - 在迷宫中的 2 点之间找到一条最小转弯的路径
- nginx - Nginx 保留代理导航回主机
- c# - EF-Core:如何将连接表达式传递给动态 lambda 表达式?
- python - 无法从 keras.optimizer 安装 Adam
- python - 根据用户在计算器中给出的数据类型输出结果[Python]