Spring Data Jpa實(shí)現(xiàn)分頁(yè)和排序代碼實(shí)例
之前我們學(xué)習(xí)了如何使用Jpa訪問(wèn)關(guān)系型數(shù)據(jù)庫(kù)。通過(guò)Jpa大大簡(jiǎn)化了我們對(duì)數(shù)據(jù)庫(kù)的開(kāi)發(fā)工作。但是,之前的例子中我們只提到了最簡(jiǎn)單的CRUD(增刪改查)操作。實(shí)際上,Spring Data Jpa對(duì)于分頁(yè)以及排序的查詢(xún)也有著完美的支持,接下來(lái),我們來(lái)學(xué)習(xí)如何通過(guò)Pageable來(lái)對(duì)數(shù)據(jù)庫(kù)進(jìn)行分頁(yè)查詢(xún)。
添加maven依賴(lài)
首先我們需要引入Jpa,數(shù)據(jù)庫(kù)直接使用hsqldb內(nèi)存數(shù)據(jù)庫(kù)就可以了:
<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訪問(wèn)關(guān)系型數(shù)據(jù)庫(kù)已經(jīng)介紹過(guò),我們暫且跳過(guò),這里我們直接來(lái)看接口BlogRepository的定義:
public interface BlogRepository extends PagingAndSortingRepository { Page findByDeletedFalse(Pageable pageable); }
我們可以看到,BlogRepository定義了這樣一個(gè)方法:Page findByDeletedFalse(Pageable pageable);,我們主要關(guān)注它的參數(shù)以及返回值。
Pageable 是Spring Data庫(kù)中定義的一個(gè)接口,該接口是所有分頁(yè)相關(guān)信息的一個(gè)抽象,通過(guò)該接口,我們可以得到和分頁(yè)相關(guān)所有信息(例如pageNumber、pageSize等),這樣,Jpa就能夠通過(guò)pageable參數(shù)來(lái)得到一個(gè)帶分頁(yè)信息的Sql語(yǔ)句。
Page類(lèi)也是Spring Data提供的一個(gè)接口,該接口表示一部分?jǐn)?shù)據(jù)的集合以及其相關(guān)的下一部分?jǐn)?shù)據(jù)、數(shù)據(jù)總數(shù)等相關(guān)信息,通過(guò)該接口,我們可以得到數(shù)據(jù)的總體信息(數(shù)據(jù)總數(shù)、總頁(yè)數(shù)...)以及當(dāng)前數(shù)據(jù)的信息(當(dāng)前數(shù)據(jù)的集合、當(dāng)前頁(yè)數(shù)等)
Spring Data Jpa除了會(huì)通過(guò)命名規(guī)范幫助我們擴(kuò)展Sql語(yǔ)句外,還會(huì)幫助我們處理類(lèi)型為Pageable的參數(shù),將pageable參數(shù)轉(zhuǎn)換成為sql'語(yǔ)句中的條件,同時(shí),還會(huì)幫助我們處理類(lèi)型為Page的返回值,當(dāng)發(fā)現(xiàn)返回值類(lèi)型為Page,Spring Data Jpa將會(huì)把數(shù)據(jù)的整體信息、當(dāng)前數(shù)據(jù)的信息,分頁(yè)的信息都放入到返回值中。這樣,我們就能夠方便的進(jìn)行個(gè)性化的分頁(yè)查詢(xún)。
Pageable只是一個(gè)抽象的接口,那么,家下來(lái)我們學(xué)習(xí)如何獲得pageable對(duì)象。
通過(guò)參數(shù)生成Pageable對(duì)象
Pageable定義了很多方法,但其核心的信息只有兩個(gè):一是分頁(yè)的信息(page、size),二是排序的信息。Spring Data Jpa提供了PageRequest的具體實(shí)現(xiàn),我們只提供分頁(yè)以及排序信息即可:
@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); }
在這里,我們通過(guò)參數(shù)獲得分頁(yè)的信息,并通過(guò)Sort以及Direction告訴pageable需要通過(guò)id逆序排列。
這里可以看到,通過(guò)參數(shù)來(lái)得到一個(gè)pageable對(duì)象還是比較繁瑣的,當(dāng)查詢(xún)的方法比較多的時(shí)候,會(huì)產(chǎn)生大量的重復(fù)代碼。為了避免這種情況,Spring Data提供了直接生成pageable的方式。
直接獲取Pageable對(duì)象
@RequestMapping(value = "", method=RequestMethod.GET) public Page getEntryByPageable(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) { return blogRepository.findAll(pageable); }
我們可以看到,我們只需要在方法的參數(shù)中直接定義一個(gè)pageable類(lèi)型的參數(shù),當(dāng)Spring發(fā)現(xiàn)這個(gè)參數(shù)時(shí),Spring會(huì)自動(dòng)的根據(jù)request的參數(shù)來(lái)組裝該pageable對(duì)象,Spring支持的request參數(shù)如下:
page,第幾頁(yè),從0開(kāi)始,默認(rèn)為第0頁(yè)
size,每一頁(yè)的大小,默認(rèn)為20
sort,排序相關(guān)的信息,以property,property(,ASC|DESC)的方式組織,例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基礎(chǔ)上按lastname倒序排列
這樣,我們就可以通過(guò)url的參數(shù)來(lái)進(jìn)行多樣化、個(gè)性化的查詢(xún),而不需要為每一種情況來(lái)寫(xiě)不同的方法了。
通過(guò)url來(lái)定制pageable很方便,但唯一的缺點(diǎn)是不太美觀,因此我們需要為pageable設(shè)置一個(gè)默認(rèn)配置,這樣很多情況下我們都能夠通過(guò)一個(gè)簡(jiǎn)潔的url來(lái)獲取信息了。
Spring提供了@PageableDefault幫助我們個(gè)性化的設(shè)置pageable的默認(rèn)配置。例如@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)表示默認(rèn)情況下我們按照id倒序排列,每一頁(yè)的大小為15。
返回結(jié)果
最后,讓我們進(jìn)入程序的根目錄,運(yùn)行命令mvn spring-boot:run將web應(yīng)用啟動(dòng)起來(lái),啟動(dòng)完成后,訪問(wèn)[http://localhost:8080/](http://localhost:8080/)頁(yè)面,我們將看到如下結(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 }
通過(guò)查詢(xún)結(jié)果,我們可以知道:
以id倒序排列的10條數(shù)據(jù)
當(dāng)前頁(yè)不是最后一頁(yè),后面還有數(shù)據(jù)
總共有9頁(yè)
每頁(yè)大小為15
當(dāng)前頁(yè)為第0頁(yè)
當(dāng)前頁(yè)是第一頁(yè)
當(dāng)前頁(yè)是以id倒序排列的
當(dāng)前頁(yè)一共有15條數(shù)據(jù)
怎么樣,信息是不是很豐富,代碼是不是很簡(jiǎn)單,快點(diǎn)來(lái)嘗試一下Jpa的分頁(yè)查詢(xún)吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis和orcale update語(yǔ)句中接收參數(shù)為對(duì)象的實(shí)例代碼
Mybatis的 mapper.xml 中 update 語(yǔ)句使用 if 標(biāo)簽判斷對(duì)像屬性是否為空值。本文重點(diǎn)給大家介紹Mybatis和orcale update語(yǔ)句中接收參數(shù)為對(duì)象的實(shí)例代碼,需要的朋友參考下吧2017-09-09SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解
這篇文章主要介紹了SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04SpringBoot動(dòng)態(tài)定時(shí)功能實(shí)現(xiàn)方案詳解
在SpringBoot項(xiàng)目中簡(jiǎn)單使用定時(shí)任務(wù),不過(guò)由于要借助cron表達(dá)式且都提前定義好放在配置文件里,不能在項(xiàng)目運(yùn)行中動(dòng)態(tài)修改任務(wù)執(zhí)行時(shí)間,實(shí)在不太靈活?,F(xiàn)在我們就來(lái)實(shí)現(xiàn)可以動(dòng)態(tài)修改cron表達(dá)式的定時(shí)任務(wù),感興趣的可以了解一下2022-11-11Spring中XmlWebApplicationContext的實(shí)現(xiàn)
XmlWebApplicationContext是Spring?Framework中的一個(gè)重要類(lèi),本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08