Spring
java
jpa
persistence
在實際應用中,資料查詢的條件常常是多變性的,也就是無法100%完全預測使用者所下的限制,所以得使用 Criteria API 以滿足動態搜尋條件。首先 repository 得繼承 JpaSpecificationExecutor<T>
JavaDoc
使其支援
public long count(Specification<T> spec)
Specification
回傳符合的資料集數量。public List<T> findAll(Specification<T> spec)
Specification
回傳符合的資料集。public Page<T> findAll(Specification<T> spec, Pageable pageable)
Specification
及 Pageable
回傳符合的資料 Page
。public List<T> findAll(Specification<T> spec, Sort sort)
Specification
及Sort
回傳符合的資料集。public Optional<T> findOne(Specification<T> spec)
Specification
回傳符合的資料,若沒有符合的資料則回傳 Optional.empty()
。等 methods。Page
、Pageable
請參考此筆記,那什麼是Specification
呢?
https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-four-jpa-criteria-queries/
https://developer.ibm.com/articles/j-typesafejpa/
https://javaee.github.io/tutorial/toc.html