我有一个类似的搜索控制器方法
@RequestMapping(method = RequestMethod.GET)
public Page<MyEntity> find(@QuerydslPredicate(root = MyEntity.class)
Predicate predicate,
Pageable pageable) {
return this.myEntityRepository.findAll(predicate, pageable);
}
它非常有效。我可以发出带有各种查询字符串参数的GET
请求,它会相应地进行筛选,但现在我想通过null进行搜索。我尝试过做类似/myentity?myParam1=&
的操作,但predicate
的参数始终是null
。
如何搜索特定字段为空的位置?
没有办法通过像myParam1=null
这样的null参数来搜索实体,因为它会抛出NullPointerException。我也遇到了同样的问题,这是我唯一能让它发挥作用的方法。
@RequestMapping(method = RequestMethod.GET)
public Page find(@QuerydslPredicate(root = MyEntity.class) Predicate predicate, Pageable pageable, @RequestParam MultiValueMap parameters) {
Page entities = myEntityRepository.findAll(predicate, pageable);
if(parameters.get("myParam1") != null &&
parameters.get("myParam1").get(0) != null &&
parameters.get("myParam1").get(0).isEmpty()) {
QEntity entity = QEntity.entity;
BooleanExpression myParam1IsNull = entity.in(entities.getContent()).and(entities.myParam1.isNull());
entities = myEntityRepository.findAll(myParam1IsNull, pageable);
}
return entities;
}
它对数据库执行两次查询,但它解决了问题。我希望这对你有帮助。