Spring JPA 分頁
JPA本身提供分頁功能,前端傳過來pageable物件,也就是在路徑上新增?page=1&size=5這樣的引數(代表第一頁,每頁五個)。從controller層裡獲得Pageable pageable物件,然後一直傳到service層,dao層最後到repository中去。JPA會自動為我們分頁。如下例子一樣
@Query(value = "select b from Banner b where (b.enabled = ?1 or b.enabled = ?2) and b.operationType = 2 order by b.sortOrder asc")
Page<Banner> findAllOpenClassBannerByEnabled(boolean enabled1,boolean enabled2,Pageable pageable);
@Query(value = "select b from Banner b where (b.enabled = ?1 or b.enabled = ?2) and b.operationType = 3 order by b.sortOrder asc")
Page<Banner> findAllSpecialSubjectBannerByEnabled(boolean enabled1,boolean enabled2,Pageable pageable);
@Query(value = "select b from Banner b where (b.enabled = ?1 or b.enabled = ?2) and b.operationType = 1 order by b.sortOrder asc ")
Page<Banner> findAllBannersByEnabled(boolean enabled1,boolean enabled2,Pageable pageable);
還有一種使用原生SQL進行分頁,這種就比較麻煩一點。需要countQuery這個值來記錄value中的條數。簡單來說就是把value中的SQL語句中的數量統計出來。
@Query(value = "SELECT * FROM (SELECT * FROM banner WHERE (enabled = ?2 OR enabled = ?3)) AS a WHERE\n" +
"operation_type = 1\n" +
"AND FIND_IN_SET(?1, grade) order by id asc /* #pageable */",
countQuery = "SELECT\n" +
"\tcount(*)\n" +
"FROM\n" +
"\t(\n" +
"\t\tSELECT\n" +
"\t\t\t*\n" +
"\t\tFROM\n" +
"\t\t\tbanner\n" +
"\t\tWHERE\n" +
"\t\t\t(enabled = ?2 OR enabled = ?3)\n" +
"\t) AS a\n" +
"WHERE\n" +
"\toperation_type = 1\n" +
"AND FIND_IN_SET(?1, grade)",
nativeQuery = true)
Page<Banner> findAllBannersByGrade(int grade,boolean enabled1,boolean enabled2,Pageable pageable);
相關文章
- SpringMVC+Spring Data JPA +Bootstrap 分頁實現和模糊查詢分頁SpringMVCboot
- Spring Data Jpa 複雜查詢總結 (多表關聯 以及 自定義分頁 )Spring
- Spring Data JpaSpring
- Spring Data JPA之Spring Data JPA快速入門(三)Spring
- Spring Boot:整合Spring Data JPASpring Boot
- Spring中的REST分頁SpringREST
- Spring JPA 擴充Spring
- spring boot 配置 JPASpring Boot
- Spring Data Jpa APISpringAPI
- Spring專案處理分頁(邏輯和物理分頁)Spring
- Spring Data JPA中TransactionInterceptorSpring
- Spring Data JPA中ConfigurableTransactionManagerSpring
- spring data jpa查詢Spring
- SpringBoot整合Spring Data JPASpring Boot
- Spring Data JPA:解析CriteriaQuerySpring
- Spring Data JPA:解析SimpleJpaRepositorySpring
- Spring Data JPA:解析CriteriaBuilderSpringUI
- Spring Data JPA 的使用Spring
- Testing JPA Queries with Spring Boot and @DataJpaTestSpring Boot
- Spring Boot + JPA DataTable原始碼Spring Boot原始碼
- Spring Data JPA原始碼案例Spring原始碼
- Spring JPA 聯表查詢Spring
- Spring Data JPA REST Query CriteriaSpringREST
- Spring Data JPA簡單使用Spring
- Spring Boot (五)Spring Data JPA 操作 MySQL 8Spring BootMySql
- 【Spring技術棧】初識Spring Data JPASpring
- Spring Data JPA中事務ReactiveTransactionManagerSpringReact
- Spring Data JPA 參考文件三Spring
- Spring JPA 定義查詢方法Spring
- Spring同時整合JPA與MybatisSpringMyBatis
- 一文搞定 Spring Data JPASpring
- Spring Boot 2和JPA入門Spring Boot
- Spring Boot + JPA學習總結Spring Boot
- Spring boot方式使用MyBatis-Plus分頁操作Spring BootMyBatis
- Spring Data JPA系列2:SpringBoot整合JPA詳細教程,快速在專案中熟練使用JPASpring Boot
- Spring Data JPA的簡單入門Spring
- Spring Data JPA專案實戰(下)Spring
- @Query註解的用法(Spring Data JPA)Spring