简化普通模糊查询SQL

       List  bookList=bookDao.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root 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

你可能感兴趣的:(简化普通模糊查询SQL)