简化普通模糊查询SQL

       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;
            }
        });

欢迎加群技术交流234731686

推荐阅读更多精彩内容