加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 通过示例在MongoRepository查询中包含Null检查

发布时间:2020-12-15 02:17:24 所属栏目:Java 来源:网络整理
导读:我试图使用以下方法使用MongoRepository从符合特定搜索条件的集合中查找所有内容: S extends T PageS findAll(ExampleS example,Pageable pageable); 为此,我正在为搜索中包含的所有字段构建ExampleMatcher.像这样: private ExampleMatcher buildMatcher(F
我试图使用以下方法使用MongoRepository从符合特定搜索条件的集合中查找所有内容:

<S extends T> Page<S> findAll(Example<S> example,Pageable pageable);

为此,我正在为搜索中包含的所有字段构建ExampleMatcher.像这样:

private ExampleMatcher buildMatcher(FormSearchParameters formSearchParameters) {

    ExampleMatcher exampleMatcher = ExampleMatcher.matching()

            .withIgnorePaths("f1","f2","f3","f4","f5");

    if (!StringUtils.isEmpty(formSearchParameters.getName())) {
        exampleMatcher = exampleMatcher.withMatcher("name",match -> match.regex());
    }

    if (!StringUtils.isEmpty(formSearchParameters.getFoo())) {
        exampleMatcher = exampleMatcher.withMatcher("foo",matcher -> matcher.exact().ignoreCase());
    }

    if (null != formSearchParameters.getFilter()) {
        exampleMatcher = exampleMatcher.withMatcher("filter",matcher -> matcher.exact());
    }
    else {
        exampleMatcher = exampleMatcher.withIgnorePaths("filter");
    }

    return exampleMatcher.withIncludeNullValues();
}

但是我需要添加一个字段(比如nullField)为空的最后一个检查.我似乎无法在文档中找到有关如何解决此问题的任何信息.

我尝试在提供的示例模型中添加以下nullField为null,但这似乎被忽略了.

.withMatcher("nullField",matcher -> matcher.exact())

非常感谢

解决方法

根据 documentation,Null处理设置的范围是ExampleMatcher(不是属性路径).所以,长话短说,你不能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读