SpringbootJPA分頁 PageRequest過時(shí)的替代方法
1. 原因
最近學(xué)習(xí)spring data JPA 時(shí)候要用到分頁功能,但是發(fā)現(xiàn)網(wǎng)上所有教程都是通過new PageRequest()方法解決分頁,實(shí)際使用中發(fā)現(xiàn)已經(jīng)過時(shí)
2. 解決方案
替代的方法是不要new PageRequest,而是直接用 PageRequest.of這個(gè)方法 根據(jù)你的需求選擇入?yún)?/p>
3. 對(duì)比
原來:
@Override @Transactional(readOnly = true) // 只讀事務(wù) public Page<People> getPage(Integer pageNum, Integer pageLimit) { Pageable pageable =new PageRequest(pageNum - 1,pageLimit); return emr.findAll(pageable); }
現(xiàn)在:
@Override @Transactional(readOnly = true) // 只讀事務(wù) public Page<People> getPage(Integer pageNum, Integer pageLimit) { Pageable pageable =PageRequest.of(pageNum - 1,pageLimit); return emr.findAll(pageable); }
pageRequest隨著spring版本的更新變動(dòng)
2x版本:
/* * Copyright 2008-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.domain; import org.springframework.data.domain.Sort.Direction; import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** * Basic Java Bean implementation of {@code Pageable}. * * @author Oliver Gierke * @author Thomas Darimont */ public class PageRequest extends AbstractPageRequest { private static final long serialVersionUID = -4541509938956089562L; private final Sort sort; /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index, must not be negative. * @param size the size of the page to be returned, must be greater than 0. * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead. */ protected PageRequest(int page, int size, Sort sort) { super(page, size); Assert.notNull(sort, "Sort must not be null!"); this.sort = sort; } /** * Creates a new unsorted {@link PageRequest}. * * @param page zero-based page index, must not be negative. * @param size the size of the page to be returned, must be greater than 0. * @since 2.0 */ public static PageRequest of(int page, int size) { return of(page, size, Sort.unsorted()); } /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead. * @since 2.0 */ public static PageRequest of(int page, int size, Sort sort) { return new PageRequest(page, size, sort); } /** * Creates a new {@link PageRequest} with sort direction and properties applied. * * @param page zero-based page index, must not be negative. * @param size the size of the page to be returned, must be greater than 0. * @param direction must not be {@literal null}. * @param properties must not be {@literal null}. * @since 2.0 */ public static PageRequest of(int page, int size, Direction direction, String... properties) { return of(page, size, Sort.by(direction, properties)); } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#getSort() */ public Sort getSort() { return sort; } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#next() */ @Override public Pageable next() { return new PageRequest(getPageNumber() + 1, getPageSize(), getSort()); } /* * (non-Javadoc) * @see org.springframework.data.domain.AbstractPageRequest#previous() */ @Override public PageRequest previous() { return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort()); } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#first() */ @Override public Pageable first() { return new PageRequest(0, getPageSize(), getSort()); } /* * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(@Nullable Object obj) { if (this == obj) { return true; } if (!(obj instanceof PageRequest)) { return false; } PageRequest that = (PageRequest) obj; return super.equals(that) && this.sort.equals(that.sort); } /* * (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return 31 * super.hashCode() + sort.hashCode(); } /* * (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort); } }
1x版本:
/* * Copyright 2008-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.data.domain; import org.springframework.data.domain.Sort.Direction; /** * Basic Java Bean implementation of {@code Pageable}. * * @author Oliver Gierke * @author Thomas Darimont */ public class PageRequest extends AbstractPageRequest { private static final long serialVersionUID = -4541509938956089562L; private final Sort sort; /** * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return the first * page. * * @param page zero-based page index. * @param size the size of the page to be returned. */ public PageRequest(int page, int size) { this(page, size, null); } /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param direction the direction of the {@link Sort} to be specified, can be {@literal null}. * @param properties the properties to sort by, must not be {@literal null} or empty. */ public PageRequest(int page, int size, Direction direction, String... properties) { this(page, size, new Sort(direction, properties)); } /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param sort can be {@literal null}. */ public PageRequest(int page, int size, Sort sort) { super(page, size); this.sort = sort; } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#getSort() */ public Sort getSort() { return sort; } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#next() */ public Pageable next() { return new PageRequest(getPageNumber() + 1, getPageSize(), getSort()); } /* * (non-Javadoc) * @see org.springframework.data.domain.AbstractPageRequest#previous() */ public PageRequest previous() { return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort()); } /* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#first() */ public Pageable first() { return new PageRequest(0, getPageSize(), getSort()); } /* * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof PageRequest)) { return false; } PageRequest that = (PageRequest) obj; boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort); return super.equals(that) && sortEqual; } /* * (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode()); } /* * (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort == null ? null : sort.toString()); } }
2x版本常用創(chuàng)建實(shí)例方式:
調(diào)用靜態(tài)方法
從源碼中看到2x版本的構(gòu)造器是使用protected修飾的,所有無法通過new的方式去創(chuàng)建實(shí)例,只能通過調(diào)用static修飾的方法進(jìn)行創(chuàng)建。
1x版本常用創(chuàng)建實(shí)例方式:
直接調(diào)用構(gòu)造器即可
因?yàn)?x版本使用的是public修飾的構(gòu)造器,所以可以直接使用構(gòu)造器創(chuàng)建實(shí)例。
剛使用spring自帶的分頁工具Pageable入的坑,自己記錄一下,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析
這篇文章主要介紹了BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Linux+Docker+SpringBoot+IDEA一鍵自動(dòng)化部署的詳細(xì)步驟
這篇文章主要介紹了Linux+Docker+SpringBoot+IDEA一鍵自動(dòng)化部署的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java中的==和equals()區(qū)別小結(jié)
在Java編程中,理解==操作符和equals()方法的區(qū)別是至關(guān)重要的,本文主要介紹了Java中的==和equals()區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08利用Sharding-Jdbc組件實(shí)現(xiàn)分表
這篇文章主要為大家詳細(xì)介紹了利用Sharding-Jdbc組件實(shí)現(xiàn)分表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07java獲取網(wǎng)絡(luò)圖片上傳到OSS的方法
這篇文章主要為大家詳細(xì)介紹了java獲取網(wǎng)絡(luò)圖片上傳到OSS,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10jvm細(xì)節(jié)探索之synchronized及實(shí)現(xiàn)問題分析
這篇文章主要介紹了jvm細(xì)節(jié)探索之synchronized及實(shí)現(xiàn)問題分析,涉及synchronized的字節(jié)碼表示,JVM中鎖的優(yōu)化,對(duì)象頭的介紹等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11java虛擬機(jī)學(xué)習(xí)筆記基礎(chǔ)篇
在本篇文章里小編給大家整理的是關(guān)于java虛擬機(jī)學(xué)習(xí)筆記的相關(guān)內(nèi)容,分享了一些基礎(chǔ)知識(shí)點(diǎn),需要的朋友們參考下。2019-06-06使用SpringBoot進(jìn)行身份驗(yàn)證和授權(quán)的示例詳解
在廣闊的 Web 開發(fā)世界中,身份驗(yàn)證是每個(gè)數(shù)字領(lǐng)域的守護(hù)者,在本教程中,我們將了解如何以本機(jī)方式保護(hù)、驗(yàn)證和授權(quán) Spring-Boot 應(yīng)用程序的用戶,并遵循框架的良好實(shí)踐,希望對(duì)大家有所幫助2023-11-11