https://www.tianmaying.com/tutorial/spring-jpa-page-sort
Spring Data Jpa對於分頁以及排序的查詢也有着完美的支持,接下來,我們來學習如何通過Pageable
來對數據庫進行分頁查詢。
添加maven依賴
首先我們需要引入Jpa,數據庫直接使用hsqldb
內存數據庫就可以了:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<groupId>tmy</groupId>
<artifactId>demo.jpa.page</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tmy-spring-jpa-page-demo</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
繼承PagingAndSortingRepository
Jpa的基本使用方法在如何使用Jpa訪問關系型數據庫已經介紹過,我們暫且跳過,這里我們直接來看接口BlogRepository
的定義:
public interface BlogRepository extends PagingAndSortingRepository<Blog, Integer> {
Page<Blog> findByDeletedFalse(Pageable pageable);
}
我們可以看到,BlogRepository
定義了這樣一個方法:Page<Blog> findByDeletedFalse(Pageable pageable);
,我們主要關注它的參數以及返回值。
- Pageable 是Spring Data庫中定義的一個接口,該接口是所有分頁相關信息的一個抽象,通過該接口,我們可以得到和分頁相關所有信息(例如
pageNumber
、pageSize
等),這樣,Jpa就能夠通過pageable參數來得到一個帶分頁信息的Sql語句。 - Page類也是Spring Data提供的一個接口,該接口表示一部分數據的集合以及其相關的下一部分數據、數據總數等相關信息,通過該接口,我們可以得到數據的總體信息(數據總數、總頁數...)以及當前數據的信息(當前數據的集合、當前頁數等)
Spring Data Jpa除了會通過命名規范幫助我們擴展Sql語句外,還會幫助我們處理類型為Pageable
的參數,將pageable參數轉換成為sql'語句中的條件,同時,還會幫助我們處理類型為Page
的返回值,當發現返回值類型為Page
,Spring Data Jpa將會把數據的整體信息、當前數據的信息,分頁的信息都放入到返回值中。這樣,我們就能夠方便的進行個性化的分頁查詢。
Pageable
只是一個抽象的接口,那么,家下來我們學習如何獲得pageable對象。
通過參數生成Pageable對象
Pageable
定義了很多方法,但其核心的信息只有兩個:一是分頁的信息(page、size),二是排序的信息。Spring Data Jpa提供了PageRequest
的具體實現,Spring MVC提供了對Spring Data JPA非常好的支持,我們只提供分頁以及排序信息即可:
@RequestMapping(value = "/params", method=RequestMethod.GET)
public Page<Blog> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "15") Integer size) {
Sort sort = new Sort(Direction.DESC, "id");
Pageable pageable = new PageRequest(page, size, sort);
return blogRepository.findAll(pageable);
}
在這里,我們通過參數獲得分頁的信息,並通過Sort
以及Direction
告訴pageable需要通過id逆序排列。Spring MVC的介紹情移步這里:Spring MVC快速入門。
這里可以看到,通過參數來得到一個pageable對象還是比較繁瑣的,當查詢的方法比較多的時候,會產生大量的重復代碼。為了避免這種情況,Spring Data提供了直接生成pageable的方式。
直接獲取Pageable對象
@RequestMapping(value = "", method=RequestMethod.GET)
public Page<Blog> getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
return blogRepository.findAll(pageable);
}
我們可以看到,我們只需要在方法的參數中直接定義一個pageable類型的參數,當Spring發現這個參數時,Spring會自動的根據request的參數來組裝該pageable對象,Spring支持的request參數如下:
- page,第幾頁,從0開始,默認為第0頁
- size,每一頁的大小,默認為20
- sort,排序相關的信息,以
property,property(,ASC|DESC)
的方式組織,例如sort=firstname&sort=lastname,desc
表示在按firstname正序排列基礎上按lastname倒序排列
這樣,我們就可以通過url的參數來進行多樣化、個性化的查詢,而不需要為每一種情況來寫不同的方法了。
通過url來定制pageable很方便,但唯一的缺點是不太美觀,因此我們需要為pageable設置一個默認配置,這樣很多情況下我們都能夠通過一個簡潔的url來獲取信息了。
Spring提供了@PageableDefault
幫助我們個性化的設置pageable的默認配置。例如@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
表示默認情況下我們按照id倒序排列,每一頁的大小為15。
返回結果
最后,讓我們進入程序的根目錄,運行命令mvn spring-boot:run
將web應用啟動起來,啟動完成后,訪問http://localhost:8080/
頁面,我們將看到如下結果:
{
"content":[ {"id":123,"title":"blog122","content":"this is blog content"}, {"id":122,"title":"blog121","content":"this is blog content"}, {"id":121,"title":"blog120","content":"this is blog content"}, {"id":120,"title":"blog119","content":"this is blog content"}, {"id":119,"title":"blog118","content":"this is blog content"}, {"id":118,"title":"blog117","content":"this is blog content"}, {"id":117,"title":"blog116","content":"this is blog content"}, {"id":116,"title":"blog115","content":"this is blog content"}, {"id":115,"title":"blog114","content":"this is blog content"}, {"id":114,"title":"blog113","content":"this is blog content"}, {"id":113,"title":"blog112","content":"this is blog content"}, {"id":112,"title":"blog111","content":"this is blog content"}, {"id":111,"title":"blog110","content":"this is blog content"}, {"id":110,"title":"blog109","content":"this is blog content"}, {"id":109,"title":"blog108","content":"this is blog content"}],
"last":false,
"totalPages":9,
"totalElements":123,
"size":15,
"number":0,
"first":true,
"sort":[{ "direction":"DESC", "property":"id", "ignoreCase":false, "nullHandling":"NATIVE", "ascending":false }],
"numberOfElements":15 }
通過查詢結果,我們可以知道:
- 以id倒序排列的10條數據
- 當前頁不是最后一頁,后面還有數據
- 總共有9頁
- 每頁大小為15
- 當前頁為第0頁
- 當前頁是第一頁
- 當前頁是以id倒序排列的
- 當前頁一共有15條數據
怎么樣,信息是不是很豐富,代碼是不是很簡單,快點來嘗試一下Jpa的分頁查詢吧。
之前我們學習了如何使用Jpa訪問關系型數據庫。比較完整Spring MVC和JPA教程請見Spring Data JPA實戰入門,Spring MVC實戰入門。
通過Jpa大大簡化了我們對數據庫的開發工作。但是,之前的例子中我們只提到了最簡單的CRUD(增刪改查)操作。實際上,Spring Data Jpa對於分頁以及排序的查詢也有着完美的支持,接下來,我們來學習如何通過Pageable
來對數據庫進行分頁查詢。
添加maven依賴
首先我們需要引入Jpa,數據庫直接使用hsqldb
內存數據庫就可以了:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<groupId>tmy</groupId>
<artifactId>demo.jpa.page</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tmy-spring-jpa-page-demo</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
繼承PagingAndSortingRepository
Jpa的基本使用方法在如何使用Jpa訪問關系型數據庫已經介紹過,我們暫且跳過,這里我們直接來看接口BlogRepository
的定義:
public interface BlogRepository extends PagingAndSortingRepository<Blog, Integer> {
Page<Blog> findByDeletedFalse(Pageable pageable);
}
我們可以看到,BlogRepository
定義了這樣一個方法:Page<Blog> findByDeletedFalse(Pageable pageable);
,我們主要關注它的參數以及返回值。
- Pageable 是Spring Data庫中定義的一個接口,該接口是所有分頁相關信息的一個抽象,通過該接口,我們可以得到和分頁相關所有信息(例如
pageNumber
、pageSize
等),這樣,Jpa就能夠通過pageable參數來得到一個帶分頁信息的Sql語句。 - Page類也是Spring Data提供的一個接口,該接口表示一部分數據的集合以及其相關的下一部分數據、數據總數等相關信息,通過該接口,我們可以得到數據的總體信息(數據總數、總頁數...)以及當前數據的信息(當前數據的集合、當前頁數等)
Spring Data Jpa除了會通過命名規范幫助我們擴展Sql語句外,還會幫助我們處理類型為Pageable
的參數,將pageable參數轉換成為sql'語句中的條件,同時,還會幫助我們處理類型為Page
的返回值,當發現返回值類型為Page
,Spring Data Jpa將會把數據的整體信息、當前數據的信息,分頁的信息都放入到返回值中。這樣,我們就能夠方便的進行個性化的分頁查詢。
Pageable
只是一個抽象的接口,那么,家下來我們學習如何獲得pageable對象。
通過參數生成Pageable對象
Pageable
定義了很多方法,但其核心的信息只有兩個:一是分頁的信息(page、size),二是排序的信息。Spring Data Jpa提供了PageRequest
的具體實現,Spring MVC提供了對Spring Data JPA非常好的支持,我們只提供分頁以及排序信息即可:
@RequestMapping(value = "/params", method=RequestMethod.GET)
public Page<Blog> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "15") Integer size) {
Sort sort = new Sort(Direction.DESC, "id");
Pageable pageable = new PageRequest(page, size, sort);
return blogRepository.findAll(pageable);
}
在這里,我們通過參數獲得分頁的信息,並通過Sort
以及Direction
告訴pageable需要通過id逆序排列。Spring MVC的介紹情移步這里:Spring MVC快速入門。
這里可以看到,通過參數來得到一個pageable對象還是比較繁瑣的,當查詢的方法比較多的時候,會產生大量的重復代碼。為了避免這種情況,Spring Data提供了直接生成pageable的方式。
直接獲取Pageable對象
@RequestMapping(value = "", method=RequestMethod.GET)
public Page<Blog> getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
return blogRepository.findAll(pageable);
}
我們可以看到,我們只需要在方法的參數中直接定義一個pageable類型的參數,當Spring發現這個參數時,Spring會自動的根據request的參數來組裝該pageable對象,Spring支持的request參數如下:
- page,第幾頁,從0開始,默認為第0頁
- size,每一頁的大小,默認為20
- sort,排序相關的信息,以
property,property(,ASC|DESC)
的方式組織,例如sort=firstname&sort=lastname,desc
表示在按firstname正序排列基礎上按lastname倒序排列
這樣,我們就可以通過url的參數來進行多樣化、個性化的查詢,而不需要為每一種情況來寫不同的方法了。
通過url來定制pageable很方便,但唯一的缺點是不太美觀,因此我們需要為pageable設置一個默認配置,這樣很多情況下我們都能夠通過一個簡潔的url來獲取信息了。
Spring提供了@PageableDefault
幫助我們個性化的設置pageable的默認配置。例如@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
表示默認情況下我們按照id倒序排列,每一頁的大小為15。
返回結果
最后,讓我們進入程序的根目錄,運行命令mvn spring-boot:run
將web應用啟動起來,啟動完成后,訪問http://localhost:8080/
頁面,我們將看到如下結果:
{
"content":[ {"id":123,"title":"blog122","content":"this is blog content"}, {"id":122,"title":"blog121","content":"this is blog content"}, {"id":121,"title":"blog120","content":"this is blog content"}, {"id":120,"title":"blog119","content":"this is blog content"}, {"id":119,"title":"blog118","content":"this is blog content"}, {"id":118,"title":"blog117","content":"this is blog content"}, {"id":117,"title":"blog116","content":"this is blog content"}, {"id":116,"title":"blog115","content":"this is blog content"}, {"id":115,"title":"blog114","content":"this is blog content"}, {"id":114,"title":"blog113","content":"this is blog content"}, {"id":113,"title":"blog112","content":"this is blog content"}, {"id":112,"title":"blog111","content":"this is blog content"}, {"id":111,"title":"blog110","content":"this is blog content"}, {"id":110,"title":"blog109","content":"this is blog content"}, {"id":109,"title":"blog108","content":"this is blog content"}],
"last":false,
"totalPages":9,
"totalElements":123,
"size":15,
"number":0,
"first":true,
"sort":[{ "direction":"DESC", "property":"id", "ignoreCase":false, "nullHandling":"NATIVE", "ascending":false }],
"numberOfElements":15 }
通過查詢結果,我們可以知道:
- 以id倒序排列的10條數據
- 當前頁不是最后一頁,后面還有數據
- 總共有9頁
- 每頁大小為15
- 當前頁為第0頁
- 當前頁是第一頁
- 當前頁是以id倒序排列的
- 當前頁一共有15條數據
怎么樣,信息是不是很豐富,代碼是不是很簡單,快點來嘗試一下Jpa的分頁查詢吧。