我正在使用spring data jpa,我想知道在QueryByExample(QBE)中我可以得到所有的记录(其中列值不等于'XXX')
我看过ExampleMatcher,但找不到任何像not equals这样的东西
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?
发布于 2020-07-22 20:41:44
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。我希望它能帮助你
发布于 2019-08-22 22:53:22
您的问题可以通过使用REGEX作为StringMatcher使用QBE来解决,解决方案如下所示:
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
https://stackoverflow.com/questions/56298978
复制相似问题