examplematcher配置,用于查找具有空值的多个字段

x8goxv8g  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(525)

尝试使用examplematcher查询查找多个字段。在下面的课程中,我们有4个领域:

public class contact {
  private String name;
  private long phoneNumber; //primary
  private String email;
  private String organization;

}
现在举例来说,我想搜索与姓名和电子邮件领域和其他领域的要求是空的。结果应该是包含姓名和电子邮件请求的联系人列表。我的搜索请求获取未知数量的字段。

ExampleMatcher userExampleMatcher = ExampleMatcher.matchingAll()
            .withNullHandler(ExampleMatcher.NullHandler.IGNORE);
    Iterable<Contact> contacts = dao.findAll(Example.of(contactObject, userExampleMatcher));

此配置仅用于电话号码返回true结果,其他字段返回null。

c7rzv4ha

c7rzv4ha1#

如果 Contact 实体具有基本字段,那么它们的默认值将实际用于创建查询。
如果不设置phonenumber,它将始终查找phonenumber=0
你可以替换 Primitive 字段 Wrapper 领域:

public class Contact {
    private String name;
    private Long phoneNumber;
    private String email;
    private String organization;
}
``` `OR` 你可以 `ignore` 通过手动指定这些字段的名称:

ExampleMatcher userExampleMatcher = ExampleMatcher.matchingAll()
.withIgnorePaths("phoneNumber").withIgnoreNullValues();

这个 `.withIgnoreNullValues()` 是 `.withNullHandler(ExampleMatcher.NullHandler.IGNORE)` 在你的代码里!

相关问题