首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否存在不等于条件的ExampleMatcher

是否存在不等于条件的ExampleMatcher
EN

Stack Overflow用户
提问于 2019-05-25 04:10:26
回答 2查看 1.7K关注 0票数 2

我正在使用spring data jpa,我想知道在QueryByExample(QBE)中我可以得到所有的记录(其中列值不等于'XXX')

我看过ExampleMatcher,但找不到任何像not equals这样的东西

代码语言:javascript
运行
复制
        Employee filterBy = new Employee();
        filterBy.setLastName("ar"); 

        //Filter - ignore case search and contains 
        ExampleMatcher matcher = ExampleMatcher.matching()     
                  .withStringMatcher(StringMatcher.CONTAINING)   // Match string containing pattern   
                  .withIgnoreCase();                 // ignore case sensitivity 

        example = Example.of(filterBy, matcher);

上面的代码得到了所有的记录,其中的姓氏是ar,但我正在寻找的姓氏不应该是"ar“。有没有其他的ExampleMatcher?

EN

回答 2

Stack Overflow用户

发布于 2020-07-22 20:41:44

代码语言:javascript
运行
复制
BizExceptionConfig condition = configRequestPair.getCondition();
ExampleMatcher exampleMatcher = ExampleMatcher.matching()
        .withMatcher("appCode", startsWith())
        .withMatcher("name", startsWith())
        .withMatcher("code", startsWith());

if(Objects.isNull(condition.getLifecycle())){
    condition.setLifecycle(LifeCycle.DELETE.getCode());
    HashMap<String, Integer> params = new HashMap<>();
    params.put("$ne", LifeCycle.DELETE.getCode());
    exampleMatcher = exampleMatcher.withTransformer("lifecycle", (obj) -> Optional.of(params));
}
Example<BizExceptionConfig> example = Example.of(condition, exampleMatcher);
Page<BizExceptionConfig> pageRecord = bizExcConfigRepository.findAll(example, PageUtil.toPageRequest(configRequestPair.getPage()));`enter code here`

这个问题可以通过"withTransformer“来解决。JPA相当有限,所以我建议使用Mongotmpl。我希望它能帮助你

票数 2
EN

Stack Overflow用户

发布于 2019-08-22 22:53:22

您的问题可以通过使用REGEX作为StringMatcher使用QBE来解决,解决方案如下所示:

代码语言:javascript
运行
复制
Employee filterBy = new Employee();
filterBy.setLastName("ar");
filterBy.setLastName(String.format("^(?!.*$1$).*$", Pattern.quote(filterBy.getLastName())));
//filterBy.setLastName(String.format(".*(?<!$1)$", Pattern.quote(filterBy.getLastName())));  // this would be another alternative
ExampleMatcher matcher = ExampleMatcher.matching()
    .withStringMatcher(StringMatcher.REGEX)   // Match string containing pattern   
                  .withIgnoreCase();                 // ignore case sensitivity
Example example = Example.of(filterBy, matcher);

不幸的是,尽管开发人员最初会认为支持正则表达式(因为存在前面提到的枚举常量),但Spring目前实际上不支持它们-根据对相关Jira问题的讨论,它永远不会不支持:https://jira.spring.io/browse/DATAJPA-944

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56298978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档