SpringbootJPA分页 PageRequest过时的替代方法
作者:伟大的格尔夫斯 发布时间:2022-03-10 11:53:13
标签:Springboot,JPA,分页,PageRequest
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()
*/
@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版本常用创建实例方式:
调用静态方法
从源码中看到2x版本的构造器是使用protected修饰的,所有无法通过new的方式去创建实例,只能通过调用static修饰的方法进行创建。
1x版本常用创建实例方式:
直接调用构造器即可
因为1x版本使用的是public修饰的构造器,所以可以直接使用构造器创建实例。
刚使用spring自带的分页工具Pageable入的坑,自己记录一下,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/weixin_41016986/article/details/86623035


猜你喜欢
- 加载资源文件比较常用的有两种:一、用ClassLoader,说到这里就不得不提一下ClassLoader的分类,java内置的ClassLo
- 目录一、C# 多态性二、静态多态性三、函数重载四、C# 运算符重载1、运算符重载的实现2、可重载和不可重载运算符五、动态多态性前言:👻🎄学过
- Spring中BeanFactory FactoryBean和ObjectFactory的三种的区别引言关于FactoryBean 和 Be
- 发现问题在C#连接SQL Server时,明明添加了引用using System.Data.SqlClient;却出现了下面这种情况:原因:
- 本文实例为大家分享了iOS新浪微博分享功能的具体代码,供大家参考,具体内容如下做新浪分享 需先去http://open.weibo.com/
- 本文实例讲述了C#在WinForm中使用WebKit传递js对象实现与网页交互的方法。分享给大家供大家参考,具体如下:有个项目要使用WebB
- 1. private void Form1_Load(object &
- 在Android开发中,录入信息是最基本的操作,使用非常广泛。但是Android对输入法弹出/收起的支持,并不是很好。对弹出,提供了forc
- Android Studio软件下载地址如下:下载:http://www.android-studio.org/index.php/down
- 记录使用Scroller实现平滑滚动,效果图如下:一、自定义View中实现View的平滑滚动public class ScrollerVie
- public class BeanDefinitionHolder implements BeanMetadataElement { &nb
- 现如今,验证码在Android的客户端还是非常普遍的.通过手机账号和验证码直接去注册应用账户的信息.很多应用都以这种方式来完成注
- 在JavaWeb的相关开发中经常会涉及到多级菜单的展示,为了方便菜单的管理需要使用数据库进行支持,本例采用相关算法讲数据库中的条形记录进行相
- 支付宝今年推出了新的转账接口alipay.fund.trans.uni.transfer(升级后安全性更高,功能更加强大) ,老转账接口al
- 在有些情况下,有很多列表不能一次性显示完整,需要对其进行分页处理博主自己也写了一个分页系统,在这里记录下来,方便以后直接拿来使用这篇文章De
- 0. 前言在上一篇中,我故意留下了查询的示范没讲。虽然说可以通过以下代码获取一个DataReader:IDataReader reader
- 什么是 terms set 查询?Terms set 查询根据匹配给定字段的精确术语的最少数量返回文档。terms set 查询与 term
- Java ByteArrayInputStream流一、ByteArrayInputStream流定义API说明:ByteArrayInpu
- 导读Spring Boot应用可以使用spring-boot-maven-plugin快速打包,构建一个可执行jar。Spring Boot
- ListView在实际实用中,一般都会有下新刷新和上拉加载的动态效果,今天要学的就是如何自定义带下拉刷新的ListView。原理解析:一般将