我正在使用spring数据jpa,我想在QueryByExample(QBE)中获取所有记录(列值不等于“ XXX”)
我看过ExampleMatcher,但是找不到不等于
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吗?
答案 0 :(得分:1)
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。希望它能对您有所帮助
答案 1 :(得分:0)
使用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问题的讨论,它从未成功't:https://jira.spring.io/browse/DATAJPA-944