javascript
使用Spring Data JPA进行分页和排序
通過代碼示例學習使用Spring Data JPA進行分頁和排序 。 了解如何使用Springs PagingAndSortingRepository接口獲取分頁和排序結果。
1概述
在處理大量數據時,惰性處理通常是必不可少的。 即使服務返回了大量數據,消費者也不太可能使用它。 考慮一個購物網站,客戶在其中搜索產品,并且該網站有數千種產品要顯示。 獲取數千種產品并將其顯示在網頁上將非常耗時。 在大多數情況下,客戶甚至可能不會查看所有產品。
對于這種情況,使用了稱為分頁的技術。 最初只顯示一小部分產品(頁面),客戶可以要求查看下一個子集(頁面),依此類推。 這稱為分頁。
是否想通過Spring和Spring Boot學習使用Java Persistence API(JPA)?
讀這個:
- 使用Spring Data JPA的Spring Boot
- 具有@EmbeddedId的Spring Data JPA復合密鑰
- 通過@EmbeddedId部分找到Spring Data JPA
- Java Persistence API指南
- Spring Data JPA查詢方法
2實體
在本教程中,我們將考慮“ Employee ”實體的最簡單示例。 下面是Employee實體類。
@Entity public class Employee {@Id private Long name;private String firstName;private String lastName;private Date dateOfBirth;private Integer age;private String designation;private double salary;private Date dateOfJoining;public Long getName() {return name;}public void setName(Long name) {this.name = name;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Date getDateOfBirth() {return dateOfBirth;}public void setDateOfBirth(Date dateOfBirth) {this.dateOfBirth = dateOfBirth;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getDesignation() {return designation;}public void setDesignation(String designation) {this.designation = designation;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Date getDateOfJoining() {return dateOfJoining;}public void setDateOfJoining(Date dateOfJoining) {this.dateOfJoining = dateOfJoining;} }3員工資料庫
在Spring Data JPA查詢方法一文中 ,我們已經了解了Spring存儲庫接口和查詢方法。 在這里,我們必須學習Pagination ,因此我們將使用Spring的PagingAndSortingRepository 。
@Repository public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {Page<Employee> findAll(Pageable pageable);Page<Employee> findByFirstName(String firstName, Pageable pageable);Slice<Employee> findByFirstNameAndLastName(String firstName, String lastName, Pageable pageable); }4分頁
看一下EmployeeRepository ,該方法接受Pageable參數。 Pageable是Spring定義的接口,其中包含Page請求。 讓我們看看如何創建頁面請求。
Pageable pageable = PageRequest.of(0, 10); Page<Employee> page = employeeRepository.findAll(pageable);在第一行中,我們創建了一個10名員工的Page請求,并請求了第一(0)頁。 該頁面請求傳遞給findAll的頁面以獲取雇員頁面作為響應。
如果要訪問下一組后續頁面,則可以每次增加頁碼。
PageRequest.of(1, 10); PageRequest.of(2, 10); PageRequest.of(3, 10); ...5排序
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());6 Slice vs Page
在EmployeeRepository中,我們看到其中一個方法返回Slice ,另一個方法返回Page 。 它們都是Spring Data JPA ,其中Page是Slice的子接口。 它們都用于保存和返回數據的子集。 讓我們一一看一下
切片
切片知道它是否有內容,無論它是第一個還是最后一個切片。 它還能夠返回當前和先前切片中使用的Pageable 。 讓我們看一下Slice的一些重要方法。
List<T> getContent(); // get content of the slicePageable getPageable(); // get current pageableboolean hasContent(); boolean isFirst();boolean isLast();Pageable nextPageable(); // pageable of the next slicePageable previousPageable(); // pageable of the previous slice頁
該頁面是Slice的子接口,并具有幾個其他方法。 它知道表中的總頁數以及記錄的總數。 以下是Page中的一些重要方法。
static <T> Page<T> empty; //create an empty pagelong getTotalElements(); // number of total elements in the tableint totalPages() // number of total pages in the table7小結
在這篇使用Spring Data JPA進行分頁和排序的文章中,我們了解了為什么需要分頁。 我們學習了如何對數據進行分頁和排序。 我們還看到了Slice和Page界面及其差異。
翻譯自: https://www.javacodegeeks.com/2019/02/pagination-sorting-spring-data-jpa.html
總結
以上是生活随笔為你收集整理的使用Spring Data JPA进行分页和排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么防止别人攻击服务器(服务器如何防止别
- 下一篇: spring 事务持久性_项目学生:Sp