Spring Data Jpa實現(xiàn)分頁和排序代碼實例
之前我們學習了如何使用Jpa訪問關(guān)系型數(shù)據(jù)庫。通過Jpa大大簡化了我們對數(shù)據(jù)庫的開發(fā)工作。但是,之前的例子中我們只提到了最簡單的CRUD(增刪改查)操作。實際上,Spring Data Jpa對于分頁以及排序的查詢也有著完美的支持,接下來,我們來學習如何通過Pageable來對數(shù)據(jù)庫進行分頁查詢。
添加maven依賴
首先我們需要引入Jpa,數(shù)據(jù)庫直接使用hsqldb內(nèi)存數(shù)據(jù)庫就可以了:
<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> spring-boot-starter-parent</artifactid> <version>1.2.5.RELEASE</version> </parent> <groupid>tmy</groupid> demo.jpa.page</artifactid> <version>0.0.1-SNAPSHOT</version> <name>tmy-spring-jpa-page-demo</name> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>org.hsqldb</groupid> hsqldb</artifactid> <scope>runtime</scope> </dependency> </dependencies> </project>
繼承PagingAndSortingRepository
Jpa的基本使用方法在如何使用Jpa訪問關(guān)系型數(shù)據(jù)庫已經(jīng)介紹過,我們暫且跳過,這里我們直接來看接口BlogRepository的定義:
public interface BlogRepository extends PagingAndSortingRepository {
Page findByDeletedFalse(Pageable pageable);
}
我們可以看到,BlogRepository定義了這樣一個方法:Page findByDeletedFalse(Pageable pageable);,我們主要關(guān)注它的參數(shù)以及返回值。
Pageable 是Spring Data庫中定義的一個接口,該接口是所有分頁相關(guān)信息的一個抽象,通過該接口,我們可以得到和分頁相關(guān)所有信息(例如pageNumber、pageSize等),這樣,Jpa就能夠通過pageable參數(shù)來得到一個帶分頁信息的Sql語句。
Page類也是Spring Data提供的一個接口,該接口表示一部分數(shù)據(jù)的集合以及其相關(guān)的下一部分數(shù)據(jù)、數(shù)據(jù)總數(shù)等相關(guān)信息,通過該接口,我們可以得到數(shù)據(jù)的總體信息(數(shù)據(jù)總數(shù)、總頁數(shù)...)以及當前數(shù)據(jù)的信息(當前數(shù)據(jù)的集合、當前頁數(shù)等)
Spring Data Jpa除了會通過命名規(guī)范幫助我們擴展Sql語句外,還會幫助我們處理類型為Pageable的參數(shù),將pageable參數(shù)轉(zhuǎn)換成為sql'語句中的條件,同時,還會幫助我們處理類型為Page的返回值,當發(fā)現(xiàn)返回值類型為Page,Spring Data Jpa將會把數(shù)據(jù)的整體信息、當前數(shù)據(jù)的信息,分頁的信息都放入到返回值中。這樣,我們就能夠方便的進行個性化的分頁查詢。
Pageable只是一個抽象的接口,那么,家下來我們學習如何獲得pageable對象。
通過參數(shù)生成Pageable對象
Pageable定義了很多方法,但其核心的信息只有兩個:一是分頁的信息(page、size),二是排序的信息。Spring Data Jpa提供了PageRequest的具體實現(xiàn),我們只提供分頁以及排序信息即可:
@RequestMapping(value = "/params", method=RequestMethod.GET)
public Page 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);
}
在這里,我們通過參數(shù)獲得分頁的信息,并通過Sort以及Direction告訴pageable需要通過id逆序排列。
這里可以看到,通過參數(shù)來得到一個pageable對象還是比較繁瑣的,當查詢的方法比較多的時候,會產(chǎn)生大量的重復代碼。為了避免這種情況,Spring Data提供了直接生成pageable的方式。
直接獲取Pageable對象
@RequestMapping(value = "", method=RequestMethod.GET)
public Page getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
Pageable pageable) {
return blogRepository.findAll(pageable);
}
我們可以看到,我們只需要在方法的參數(shù)中直接定義一個pageable類型的參數(shù),當Spring發(fā)現(xiàn)這個參數(shù)時,Spring會自動的根據(jù)request的參數(shù)來組裝該pageable對象,Spring支持的request參數(shù)如下:
page,第幾頁,從0開始,默認為第0頁
size,每一頁的大小,默認為20
sort,排序相關(guān)的信息,以property,property(,ASC|DESC)的方式組織,例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基礎(chǔ)上按lastname倒序排列
這樣,我們就可以通過url的參數(shù)來進行多樣化、個性化的查詢,而不需要為每一種情況來寫不同的方法了。
通過url來定制pageable很方便,但唯一的缺點是不太美觀,因此我們需要為pageable設(shè)置一個默認配置,這樣很多情況下我們都能夠通過一個簡潔的url來獲取信息了。
Spring提供了@PageableDefault幫助我們個性化的設(shè)置pageable的默認配置。例如@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)表示默認情況下我們按照id倒序排列,每一頁的大小為15。
返回結(jié)果
最后,讓我們進入程序的根目錄,運行命令mvn spring-boot:run將web應用啟動起來,啟動完成后,訪問[http://localhost:8080/](http://localhost:8080/)頁面,我們將看到如下結(jié)果:
{
"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
}
通過查詢結(jié)果,我們可以知道:
以id倒序排列的10條數(shù)據(jù)
當前頁不是最后一頁,后面還有數(shù)據(jù)
總共有9頁
每頁大小為15
當前頁為第0頁
當前頁是第一頁
當前頁是以id倒序排列的
當前頁一共有15條數(shù)據(jù)
怎么樣,信息是不是很豐富,代碼是不是很簡單,快點來嘗試一下Jpa的分頁查詢吧。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java多線程場景解析volatile和AtomicLong區(qū)別原理
這篇文章主要為大家介紹了Java中volatile和AtomicLong的區(qū)別原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Jackson中json格式的字符串與對象的互相轉(zhuǎn)換方式
這篇文章主要介紹了Jackson中json格式的字符串與對象的互相轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例
本篇文章主要介紹了基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Spring事務(wù)中的事務(wù)傳播行為使用方式詳解
Spring框架作為一個輕量級的開源框架,在企業(yè)應用開發(fā)中被廣泛使用,在Spring事務(wù)管理中,事務(wù)傳播行為是非常重要的一部分,它定義了方法如何參與到已經(jīng)存在的事務(wù)中或者如何開啟新的事務(wù),本文將詳細介紹Spring事務(wù)中的幾種事務(wù)傳播行為,詳細講解具體使用方法2023-06-06
Springboot輕量級的監(jiān)控組件SpringbootAdmin
這篇文章主要為大家介紹了Springboot輕量級的監(jiān)控組件SpringbootAdmin使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

