简化普通模糊查询SQL
2017-12-03 本文已影响0人
扯嘴
List<Book> bookList=bookDao.findAll(new Specification<Book>() {
@Override
public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate predicate= criteriaBuilder.conjunction();
if (book!=null){
if (book.getName()!=null && !"".equals(book.getName())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("name"),"%"+book.getName()+"%"));
}
if (book.getAuthor()!=null && !"".equals(book.getAuthor())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("author"),"%"+book.getAuthor()+"%"));
}
}
return predicate;
}
});