简体   繁体   English

ExampleMatcher 配置用于查找具有 null 值的多个字段

[英]ExampleMatcher config for find multiple fields with null value

Trying to find multiple fields with ExampleMatcher query.尝试使用 ExampleMatcher 查询查找多个字段。 in below class we have 4 field:在下面的 class 我们有 4 个字段:

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

} }

Now for example I want to search with name and email fields and other fields request are null.现在例如我想用名称和 email 字段搜索,其他字段请求是 null。 result should be list of contacts that contain name and email request.结果应该是包含姓名和 email 请求的联系人列表。 My search request get unknown number of fields.我的搜索请求获得未知数量的字段。

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

This config just for phone number return true result and for other fields return null.此配置仅用于电话号码返回真实结果,其他字段返回 null。

If the Contact entity has primitive fields, then their default value will actually be used for creating the query.如果Contact实体有原始字段,那么它们的默认值实际上将用于创建查询。

If not setting the phoneNumber, it will always look for phoneNumber = 0如果不设置phoneNumber,它会一直寻找phoneNumber = 0

You can replace Primitive fields with Wrapper fields:您可以用Wrapper字段替换Primitive字段:

public class Contact {
    private String name;
    private Long phoneNumber;
    private String email;
    private String organization;
}

OR

You can ignore primitive fields by manually specifying names of these fields:您可以通过手动指定这些字段的名称来ignore原始字段:

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

The .withIgnoreNullValues() is the shorthand for the .withNullHandler(ExampleMatcher.NullHandler.IGNORE) handler in your code! .withIgnoreNullValues()是代码中.withNullHandler(ExampleMatcher.NullHandler.IGNORE)处理程序的简写!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM