慕田峪9158850
2022-07-14 09:47:54
如何實現 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(..)如何避免這種情況?
1 回答

夢里花落0921
TA貢獻1772條經驗 獲得超6個贊
嘗試這個:
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,默認匹配從示例派生的所有謂詞。
添加回答
舉報
0/150
提交
取消