使用Spring Data JPA進行分頁和排序

2021-09-11 07:13:21 字數 3040 閱讀 3259

概觀

在處理大量資料時,延遲處理通常是必不可少的。即使服務返回大量資料,消費者也不太可能使用它。考慮乙個購物**,客戶在該**上搜尋產品,該**有數千種產品可供展示。獲取數千種產品並在網頁上顯示它們將非常耗時。在大多數情況下,客戶甚至可能不會檢視所有產品。

對於這種情況,使用稱為分頁的技術。首先只顯示一小部分產品(頁面),客戶可以要求檢視下乙個子集(頁面)等。

實體為了本教程的目的,我們將考慮employee 實體的最簡單示例 。下面是 employee 實體類。

@entity public class employee  public void setname(long name)  public string getfirstname()  public void setfirstname(string firstname)  public string getlastname()  public void setlastname(string lastname)  public date getdateofbirth()  public void setdateofbirth(date dateofbirth)  public integer getage()  public void setage(integer age)  public string getdesignation()  public void setdesignation(string designation)  public double getsalary()  public void setsalary(double salary)  public date getdateofjoining()  public void setdateofjoining(date dateofjoining)  }
員工儲存庫

在 spring data jpa查詢方法一文中,我們已經了解了spring儲存庫介面和查詢方法。在這裡,我們需要學習分頁,所以我們將使用spring pagingandsortingrepository。

@repository public inte***ce employeerepository extends pagingandsortingrepository
分頁

看看吧 employeerepository。 該方法接受 pageable 引數。 pageable 是乙個由spring定義的介面,它擁有乙個pagerequest。讓我們看看如何建立乙個 pagerequest。

pageable pageable = pagerequest.of(0, 10); pagepage = employeerepository.findall(pageable);
在第一行中,我們建立了 pagerequest10名員工,並要求提供第一頁(0)。傳遞了頁面請求 findall 以獲取employees頁面作為響應。

如果我們想要訪問下一組後續頁面,我們可以每次都增加頁碼。

pagerequest.of(1, 10); pagerequest.of(2, 10); pagerequest.of(3, 10); ...
排序

spring data jpa提供了乙個 sort 物件以提供排序機制。我們來看看排序方式。

employeerepository.findall(sort.by("fistname")); employeerepository.findall(sort.by("fistname").ascending().and(sort.by("lastname").descending());
顯然,第乙個按「firstname」排序,另乙個按「firstname」公升序和「lastname」降序排序。

分頁和排序

pageable pageable = pagerequest.of(0, 20, sort.by("firstname")); pageable pageable = pagerequest.of(0, 20, sort.by("fistname").ascending().and(sort.by("lastname").descending());
切片與 頁

在employeerepository,我們看到其中乙個方法返回 slice ,另乙個返回 page。它們都是spring data jpa,其中 page 是子介面 slice。它們都用於儲存和返回資料子集。我們乙個乙個地看看它們

切片該 slice 知道,如果它有內容,如果它是第乙個或最後乙個切片。它還能夠返回 pageable當前和先前切片中使用的。我們來看看一些重要的方法 slice。

listgetcontent(); // get content of the slice pageable getpageable(); // get current pageable boolean hascontent(); boolean isfirst(); boolean islast(); pageable nextpageable(); // pageable of the next slice pageable previouspageable(); // pageable of the previous slice

page 是乙個子介面, slice 並有幾個額外的方法。它知道表中的總頁數以及記錄總數。以下是一些重要的方法page。

static pageempty; //create an empty page long gettotalelements(); // number of total elements in the table int totalpages() // number of total pages in the table
摘要

在使用spring data jpa的分頁和排序示例中, 我們了解了為什麼需要分頁。我們還學習了如何獲取分頁和排序的資料子集。最後,我們也看到了 slice 和 page 介面以及他們之間的分歧。

Springdata Jpa使用規範

repository 1.repository 最頂層的介面,乙個空的介面,統一所有的repository型別,並且能夠讓元件掃瞄的時候能夠自動識別 2.crudrepository repository的子介面,提供crud的操作 3.pagingandsortingrepository crud...

Spring data JPA使用詳解 超詳細

目錄 jpa 元模型criteria查詢 criteriabuilder 安全查詢建立工廠 criteriaquery 安全查詢主語句 root predicate 過濾條件 predicate 多個過濾條件 spring data jpa簡介 spring data jpa是spring在orm框...

spring data jpa實體繼承

spring jpa中我們要將sql對映到物件,尤其是在spring boot這種高度自動化的環境下使用,大量的最優目錄結構與命名規則可以大大降低配置,約定大於配置貫穿其中。例如我們定義查詢dao,繼承jparepository即可。然後返回的物件,我們可以定義model entity table ...