亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringbootJPA分頁 PageRequest過時的替代方法

瀏覽:12日期:2023-03-04 17:57:22
1. 原因

最近學習spring data JPA 時候要用到分頁功能,但是發現網上所有教程都是通過new PageRequest()方法解決分頁,實際使用中發現已經過時

2. 解決方案

替代的方法是不要new PageRequest,而是直接用 PageRequest.of這個方法 根據你的需求選擇入參

3. 對比

原來:

@Override@Transactional(readOnly = true) // 只讀事務public Page<People> getPage(Integer pageNum, Integer pageLimit) {Pageable pageable =new PageRequest(pageNum - 1,pageLimit);return emr.findAll(pageable);}

現在:

@Override@Transactional(readOnly = true) // 只讀事務public Page<People> getPage(Integer pageNum, Integer pageLimit) {Pageable pageable =PageRequest.of(pageNum - 1,pageLimit);return emr.findAll(pageable);}pageRequest隨著spring版本的更新變動

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() */@Overridepublic Pageable next() {return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());}/* * (non-Javadoc) * @see org.springframework.data.domain.AbstractPageRequest#previous() */@Overridepublic PageRequest previous() {return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());}/* * (non-Javadoc) * @see org.springframework.data.domain.Pageable#first() */@Overridepublic Pageable first() {return new PageRequest(0, getPageSize(), getSort());}/* * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */@Overridepublic 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() */@Overridepublic int hashCode() {return 31 * super.hashCode() + sort.hashCode();}/* * (non-Javadoc) * @see java.lang.Object#toString() */@Overridepublic 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) */@Overridepublic 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() */@Overridepublic int hashCode() {return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode());}/* * (non-Javadoc) * @see java.lang.Object#toString() */@Overridepublic String toString() {return String.format('Page request [number: %d, size %d, sort: %s]', getPageNumber(), getPageSize(),sort == null ? null : sort.toString());}}

2x版本常用創建實例方式:

調用靜態方法

SpringbootJPA分頁 PageRequest過時的替代方法

從源碼中看到2x版本的構造器是使用protected修飾的,所有無法通過new的方式去創建實例,只能通過調用static修飾的方法進行創建。

1x版本常用創建實例方式:

直接調用構造器即可

SpringbootJPA分頁 PageRequest過時的替代方法

因為1x版本使用的是public修飾的構造器,所以可以直接使用構造器創建實例。

剛使用spring自帶的分頁工具Pageable入的坑,自己記錄一下,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 涩涩99| 久青草视频在线播放 | 亚洲深夜 | 国产剧情一区二区三区 | 精品免费久久久久久成人影院 | 国产一级特黄老妇女大片免费 | 99视频有精品| 欧美三级视频在线观看 | 日韩欧美一区二区三区久久 | 亚洲精品一线观看 | 日本巨乳中文字幕 | 嫩草视频网站 | 免费看一级黄色毛片 | 久久久久国产精品免费免费不卡 | 国产精品嫩草影院在线看 | 深夜偷偷看视频在线观看 | 成人福利网址永久在线观看 | 久久综合久美利坚合众国 | 午夜精品在线 | 欧美一区不卡二区不卡三区 | 亚洲无限乱码一二三四区 | igao视频天堂| 欧美日韩一区二区三区在线 | 九九九精品视频免费 | 国产高清在线精品一区在线 | 精品国产亚洲一区二区三区 | 精品牛牛影视久久精品 | 丝瓜着色的视频 | 国产亚洲精品热视频在线观看 | 国产网曝手机视频在线观看 | 国产精品亚洲专区在线播放 | 国产人成久久久精品 | 日本亚洲成高清一区二区三区 | 色婷婷六月丁香七月婷婷 | 国产精品酒店视频免费看 | 成人黄色免费在线观看 | 高清对白精彩国产国语 | 国产精品伦子一区二区三区 | 青青草国产97免久久费观看 | 亚洲国产成人va在线观看网址 | 精品亚洲永久免费精品 |