我正在尝试构建动态查询(具有类似条件)。当我用原始的sql尝试它进入mysql时,它运行得很好。这是一个例子:
select * from dbiwant.want_activity m where m.want like "%bike%"
然后,我尝试使用CriteriaBuilder,但是我无法在Java中获得相同的结果。只是空的结果。这是我的代码:
Specifications specifications = Specifications.where(specificationService.isLike("want", search));
public Specification<WantActivity> isLike(String param, String search) {
return new Specification<WantActivity>() {
public javax.persistence.criteria.Predicate toPredicate(Root<WantActivity> root, CriteriaQuery<?> query,
CriteriaBuilder builder) {
return builder.like(root.get(param) ,"%" + search + "%");
}
};
}
有什么问题?
答案 0 :(得分:0)
试试这个:
public Specification<WantActivity> isLike(String param, String search) {
return new Specification<WantActivity>() {
public javax.persistence.criteria.Predicate toPredicate(Root<WantActivity> root, CriteriaQuery<?> query,
CriteriaBuilder builder) {
final Predicate like = query.where(builder.like(root.<String> get(param), search)).getRestriction();
return like;
}
};