Spring分页实现PageImpl<T>类
Spring框架中PageImpl<T>类的源码如下:
/*
* Copyright 2008-2013 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
*
* http://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 java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List; /**
* Basic {@code Page} implementation.
*
* @param <T> the type of which the page consists.
* @author Oliver Gierke
*/
public class PageImpl<T> implements Page<T>, Serializable { private static final long serialVersionUID = 867755909294344406L; private final List<T> content = new ArrayList<T>();
private final Pageable pageable;
private final long total; /**
* Constructor of {@code PageImpl}.
*
* @param content the content of this page, must not be {@literal null}.
* @param pageable the paging information, can be {@literal null}.
* @param total the total amount of items available
*/
public PageImpl(List<T> content, Pageable pageable, long total) { if (null == content) {
throw new IllegalArgumentException("Content must not be null!");
} this.content.addAll(content);
this.total = total;
this.pageable = pageable;
} /**
* Creates a new {@link PageImpl} with the given content. This will result in the created {@link Page} being identical
* to the entire {@link List}.
*
* @param content must not be {@literal null}.
*/
public PageImpl(List<T> content) {
this(content, null, null == content ? 0 : content.size());
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getNumber()
*/
public int getNumber() {
return pageable == null ? 0 : pageable.getPageNumber();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getSize()
*/
public int getSize() {
return pageable == null ? 0 : pageable.getPageSize();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getTotalPages()
*/
public int getTotalPages() {
return getSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getSize());
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getNumberOfElements()
*/
public int getNumberOfElements() {
return content.size();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getTotalElements()
*/
public long getTotalElements() {
return total;
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#hasPreviousPage()
*/
public boolean hasPreviousPage() {
return getNumber() > 0;
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#isFirstPage()
*/
public boolean isFirstPage() {
return !hasPreviousPage();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#hasNextPage()
*/
public boolean hasNextPage() {
return getNumber() + 1 < getTotalPages();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#isLastPage()
*/
public boolean isLastPage() {
return !hasNextPage();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#nextPageable()
*/
public Pageable nextPageable() {
return hasNextPage() ? pageable.next() : null;
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#previousOrFirstPageable()
*/
public Pageable previousPageable() { if (hasPreviousPage()) {
return pageable.previousOrFirst();
} return null;
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#iterator()
*/
public Iterator<T> iterator() {
return content.iterator();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getContent()
*/
public List<T> getContent() {
return Collections.unmodifiableList(content);
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#hasContent()
*/
public boolean hasContent() {
return !content.isEmpty();
} /*
* (non-Javadoc)
* @see org.springframework.data.domain.Page#getSort()
*/
public Sort getSort() {
return pageable == null ? null : pageable.getSort();
} /*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() { String contentType = "UNKNOWN"; if (content.size() > 0) {
contentType = content.get(0).getClass().getName();
} return String.format("Page %s of %d containing %s instances", getNumber(), getTotalPages(), contentType);
} /*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) { if (this == obj) {
return true;
} if (!(obj instanceof PageImpl<?>)) {
return false;
} PageImpl<?> that = (PageImpl<?>) obj; boolean totalEqual = this.total == that.total;
boolean contentEqual = this.content.equals(that.content);
boolean pageableEqual = this.pageable == null ? that.pageable == null : this.pageable.equals(that.pageable); return totalEqual && contentEqual && pageableEqual;
} /*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() { int result = 17; result = 31 * result + (int) (total ^ total >>> 32);
result = 31 * result + (pageable == null ? 0 : pageable.hashCode());
result = 31 * result + content.hashCode(); return result;
}
}
在Spring框架中,要实现分页显示数据,可以使用PageImpl<T>这个类:
代码如下:
省略================
String pageindex = "" + (searchable.getPage().getPageNumber() * searchable.getPage().getPageSize() + 1);
String Spagesum = "" + searchable.getPage().getPageSize(); com.pcitc.modules.fos.appstream.wsclient.qry.ResponseBody responseBody = appStreamRepository
.appStreamQry(user, LSUtil.getLsNum(), streamEntity, pageinsex, pagesum);
// 视图部分
model.addAttribute("page", new PageImpl<Qrylist>(responseBody.getQrylist(), searchable.getPage(),
Long.parseLong(responseBody.getSumcount()))); setCommonData(model);
return viewName("moview");
省略======================
Spring分页实现PageImpl<T>类的更多相关文章
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
- Spring常用的接口和类(二)
七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...
- spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法
spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到
- Mybatis包分页查询java公共类
Mybatis包分页查询java公共类 分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...
- Spring中的JDBC模板类入门
1.Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2.提供了JDBC模板,Spring框架提供的 *JdbcTemplate类 3.Spring框架可以整合Hib ...
- 获取Spring容器Bean对象工具类
在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...
- Spring中的@Transactional 放在 类级别 和 方法级别 上有什么不同?
Spring中的@Transactional 放在类级别 和 方法级别 上有什么不同? @Transactional放在类级别上是否等同于该类的每个方法都放上了@Transactional? 是的一般 ...
- 【Spring Boot】Spring Boot之使用ImportSelector类实现动态注册Bean
一.ImportSelector类介绍 可以通过指定的选择条件来决定哪些类被注册到Spring中.与ImportBeanDefinitionRegistrar类功能相似,通过@Import的方 ...
- ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)
验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...
随机推荐
- Ubuntu系统---NVIDIA 驱动安装
Ubuntu系统---NVIDIA 驱动安装 第一次安装“NVIDIA 驱动”,小小的激动,因为终于可以玩GPU了.预想一块GPU,盼望太久,差点放弃,感谢J姐让我捡个漏.但是,第一次新的试错过程,网 ...
- flask参数传递
一. 参数传递两种方式: 1.get请求 request.args.get("key") 获取get请求参数 2.post请求request.form.get("key& ...
- 希尔排序Shell_Sort
概述:听到希尔排序这个名称,心里完全没有任何概念,因为这个名称不能给你提供任何有效的信息.但是它的名字又是那么的特殊,以至于学习过数据结构排序的都知道这种方法的存在.现在我们就来看一下所谓的希尔排序. ...
- Excle导出优化(poi)
搜索词条 1.idea报java.lang.OutOfMemoryError: Java heap space怎么解决? 2.java.lang.OutOfMemoryError: GC overhe ...
- 记一次k8s服务504 timeout
线上服务做集群扩容,调整了节点机器配置,在升级完毕之后,发现某些时候请求较慢,或者直接504 timeout 超时,必现情况,点击几次都是,且并没有代表性. 1.检查istio 日志是否有504 的日 ...
- Codeforces Round #587 (Div. 3) C题 【判断两个矩形是否完全覆盖一个矩形问题】 {补题 [差点上分系列]}
C. White Sheet There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle ...
- 017_STM32程序移植之_AS608指纹模块
STM32程序移植之AS608指纹模块 BUG说明: 硬件接线图如图所示 STM32引脚 指纹模块引脚 功能 3.3V 3.3V PA3 Tx PA2 Rx GND GND PA1 WAK 3.3V ...
- 005_linuxC++之_指针的引入
(一)直接看代码 #include <iostream> using namespace std; int add(int a){ a = a + ; return a; } int ad ...
- 012_Linux驱动之_wait_event_interruptible
1. 首先这篇博客讲解得挺好的,推荐 wait_event_interruptible 使用方法 2 .函数原型: #define wait_event_interruptible(wq, condi ...
- Poj 2976 Dropping tests(01分数规划 牛顿迭代)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Description In a certain course, you take n t ...