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>类的更多相关文章

  1. Spring统一返回Json工具类,带分页信息

    前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...

  2. Spring常用的接口和类(二)

    七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...

  3. spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法

    spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到

  4. Mybatis包分页查询java公共类

    Mybatis包分页查询java公共类   分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...

  5. Spring中的JDBC模板类入门

    1.Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2.提供了JDBC模板,Spring框架提供的 *JdbcTemplate类 3.Spring框架可以整合Hib ...

  6. 获取Spring容器Bean对象工具类

    在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅. ...

  7. Spring中的@Transactional 放在 类级别 和 方法级别 上有什么不同?

    Spring中的@Transactional 放在类级别 和 方法级别 上有什么不同? @Transactional放在类级别上是否等同于该类的每个方法都放上了@Transactional? 是的一般 ...

  8. 【Spring Boot】Spring Boot之使用ImportSelector类实现动态注册Bean

    一.ImportSelector类介绍     可以通过指定的选择条件来决定哪些类被注册到Spring中.与ImportBeanDefinitionRegistrar类功能相似,通过@Import的方 ...

  9. ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)

    验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...

随机推荐

  1. LeetCode--链表

    1.使用常量空间复杂度在O(n log n)时间内对链表进行排序. 思路: 因为题目要求复杂度为O(nlogn),故可以考虑归并排序的思想. 归并排序的一般步骤为: 1)将待排序数组(链表)取中点并一 ...

  2. 哲学家就餐问题 C语言实现

    场景: 原版的故事里有五个哲学家(不过我们写的程序可以有N个哲学家),这些哲学家们只做两件事--思考和吃饭,他们思考的时候不需要任何共享资源,但是吃饭的时候就必须使用餐具,而餐桌上的餐具是有限的,原版 ...

  3. RT-Thread--内核基础

    内核介绍 内核处于硬件层之上,内核部分包括内核库.实时内核实现. 实时内核的实现包括:对象管理.线程管理及调度器.线程间通信管理.时钟管理及内存管理等等,内核最小的资源占用情况是 3KB ROM,1. ...

  4. mysql 数据库的相关操作

    #coding=gbk #数据库的连接语句 import pymysql try: conn=pymysql.connect( host='127.0.0.1', port=3306, user='r ...

  5. 【NOIP/CSP2019】D2T1 Emiya 家今天的饭

    这个D2T1有点难度啊 原题: 花了我一下午的时间,作为D2T1的确反常 条件很奇怪,感觉不太直观,于是看数据范围先写了个暴力 写暴力的时候我就注意到了之前没有仔细想过的点,烹饪方式必须不同 虽然a很 ...

  6. bloomberg bulkFile解析

    文章导航 bloomberg bulkfile解析 bloomberg bulkfile 在oracle的存储 准备工作: Bloomberg 提供了以下文件 1 . fields.csv 下载地址: ...

  7. 浅谈linux用户与用户组的概念

    原文链接;http://linuxme.blog.51cto.com/1850814/347086 作者:linuxme1.用户 用户是能够获取系统资源的权限的集合. .linux用户组的分类: a. ...

  8. TCP中的长连接和短连接(转载)

    原文地址:http://www.cnblogs.com/onlysun/p/4520553.html 次挥手,所以说每个连接的建立都是需要资源消耗和时间消耗的  示意图:               ...

  9. python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch

    协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...

  10. Luogu5298 [PKUWC2018]Minimax

    太久没写博客了,过来水一发. 题目链接:洛谷 首先我们想到,考虑每个叶节点的权值为根节点权值的概率.首先要将叶节点权值离散化. 假设现在是$x$节点,令$f_i,g_i$分别表示左/右节点的权值$=i ...