SpringbootJPA分页 PageRequest过时的替代方法

 更新时间:2021年06月15日 08:39:35   作者:伟大的格尔夫斯  
这篇文章主要介绍了SpringbootJPA分页 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入的坑,自己记录一下,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java逗号分隔String字符串及数组、集合相互转换

    java逗号分隔String字符串及数组、集合相互转换

    我们在日常开发时会经常遇到将一个字符串按照逗号进行分割,这篇文章主要给大家介绍了关于java逗号分隔String字符串及数组、集合相互转换的相关资料,文中给出了详细的代码示例,需要的朋友可以参考下
    2024-02-02
  • spring boot定时器实现定时同步数据的操作步骤

    spring boot定时器实现定时同步数据的操作步骤

    在Java中,@Scheduled注解是用于指定定时任务的执行规则的,这篇文章给大家介绍spring boot定时器实现定时同步数据的操作步骤,感兴趣的朋友一起看看吧
    2023-12-12
  • k8s部署springboot实现前后端分离项目

    k8s部署springboot实现前后端分离项目

    本文主要介绍了k8s部署springboot实现前后端分离项目,主要包括配置文件、镜像构建和容器编排等方面,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • 一文探究ArrayBlockQueue函数及应用场景

    一文探究ArrayBlockQueue函数及应用场景

    这篇文章主要为大家介绍了一文探究ArrayBlockQueue函数及应用场景,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Java实现图书管理系统的示例代码

    Java实现图书管理系统的示例代码

    这篇文章将通过Java实现一个简答的图书管理系统,本图书管理系统用对象数组的方式来提供操作方法,比较特别,建议新手学习,这对理解Java面向对象有很大帮助
    2022-11-11
  • Java经典设计模式之适配器模式原理与用法详解

    Java经典设计模式之适配器模式原理与用法详解

    这篇文章主要介绍了Java经典设计模式之适配器模式,简单说明了适配器模式的概念、原理,并结合实例形式分析了java适配器模式的用法与相关注意事项,需要的朋友可以参考下
    2017-08-08
  • JAVA发送http get/post请求,调用http接口、方法详解

    JAVA发送http get/post请求,调用http接口、方法详解

    这篇文章主要介绍了Java发送http get/post请求调用接口/方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Java Mybatis一级缓存和二级缓存

    Java Mybatis一级缓存和二级缓存

    缓存是内存当中一块存储数据的区域,目的是提高查询效率,降低服务器和数据库的压力,这篇文章主要介绍了Mybatis一级缓存和二级缓存,感兴趣的同学可以参考阅读本文
    2023-04-04
  • Java实现解出世界最难九宫格问题

    Java实现解出世界最难九宫格问题

    这篇文章主要介绍了Java实现解出世界最难九宫格问题,芬兰数学家因卡拉花费3个月设计出了世界上迄今难度最大的数独游戏,而且它只有一个答案,本文使用Java实现解出,需要的朋友可以参考下
    2015-01-01
  • javaSwing写关闭窗口的提示框实例

    javaSwing写关闭窗口的提示框实例

    这篇文章主要介绍了javaSwing写关闭窗口的提示框实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12

最新评论