Created
September 13, 2018 08:48
-
-
Save guozi/3a2cd12e781ffcb9e79c7cb26ce5caa2 to your computer and use it in GitHub Desktop.
Spring Boot + Spring Data Jpa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface BookQueryService { | |
Page<Book> findBookNoCriteria(Integer page,Integer size); | |
Page<Book> findBookCriteria(Integer page,Integer size,BookQuery bookQuery); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class BookQueryServiceImpl implements BookQueryService { | |
@Resource | |
BookRepository bookRepository; | |
@Override | |
public Page<Book> findBookNoCriteria(Integer page,Integer size) { | |
Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id"); | |
return bookRepository.findAll(pageable); | |
} | |
@Override | |
public Page<Book> findBookCriteria(Integer page, Integer size, final BookQuery bookQuery) { | |
Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id"); | |
Page<Book> bookPage = bookRepository.findAll(new Specification<Book>(){ | |
@Override | |
public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { | |
List<Predicate> list = new ArrayList<Predicate>(); | |
if(null!=bookQuery.getName()&&!"".equals(bookQuery.getName())){ | |
list.add(criteriaBuilder.equal(root.get("name").as(String.class), bookQuery.getName())); | |
} | |
if(null!=bookQuery.getIsbn()&&!"".equals(bookQuery.getIsbn())){ | |
list.add(criteriaBuilder.equal(root.get("isbn").as(String.class), bookQuery.getIsbn())); | |
} | |
if(null!=bookQuery.getAuthor()&&!"".equals(bookQuery.getAuthor())){ | |
list.add(criteriaBuilder.equal(root.get("author").as(String.class), bookQuery.getAuthor())); | |
} | |
Predicate[] p = new Predicate[list.size()]; | |
return criteriaBuilder.and(list.toArray(p)); | |
} | |
},pageable); | |
return bookPage; | |
} | |
/* | |
@Override | |
public Page<Book> findBookCriteria(Integer page, Integer size, final BookQuery bookQuery) { | |
Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "id"); | |
Page<Book> bookPage = bookRepository.findAll(new Specification<Book>(){ | |
@Override | |
public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { | |
Predicate p1 = criteriaBuilder.equal(root.get("name").as(String.class), bookQuery.getName()); | |
Predicate p2 = criteriaBuilder.equal(root.get("isbn").as(String.class), bookQuery.getIsbn()); | |
Predicate p3 = criteriaBuilder.equal(root.get("author").as(String.class), bookQuery.getAuthor()); | |
query.where(criteriaBuilder.and(p1,p2,p3)); | |
return query.getRestriction(); | |
} | |
},pageable); | |
return bookPage; | |
} | |
*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository("bookRepository") | |
public interface BookRepository extends JpaRepository<Book,Long> | |
,JpaSpecificationExecutor<Book> { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment