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---github下载太慢的解决办法
git clone --recursive https://github.com/tensorflow/tensorflow 下载太慢了,20:05-21:17 才下载了17%,等的太急人了. ** ...
- How Many Answers Are Wrong(带权并查集)
题目 带权并查集的博客~ 题目: 多组输入数据.n,m.你不知道[1,n]内任意区间内值的和. m次询问,a b 是端点,都在n的范围以内 : v表示 [a,b]的区间内值的和.对每次询问,判断v是否 ...
- throttle(节流函数) 与 debounce(防抖动函数)理解与实现
我们会对一些触发频率较高的事件进行监听,(如:resize scroll keyup事件) 如果在回调里执行高性能消耗的操作(如反复操作dom, 发起ajax请求等),反复触发时会使得性能消耗提高,浏 ...
- 【H5】 经纬度位置获取navigator.geolocation.getCurrentPosition
navigator.geolocation.getCurrentPosition(function(){})经度 : coords.longitude 纬度 : coords.latitude 准确度 ...
- hexo主题next 7.X版本配置美化
我们主要对next主题进行了如下配置操作.效果可以前往https://www.ipyker.com 查看. 也可以前往https://github.com/ipyker/hexo-next-theme ...
- pip报错:解决pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application
如果pip安装后提示依然没有pip命令,需在在添加环境变量 # vim /etc/profile 在文档最后,添加: export PATH="/usr/local/python2.7/bi ...
- 类对象传输到jsp页面。需要转换为js的json对象时,这么做。
场景:要从一个列表中选择信息,填写入父页面的表单中,但是字段非常多... 后台查询,得到结果,放在列表中. 效果:点击选择产品.. 弹出页面:点击后面的选择产品 选择产品后:信息自动填充.. 实现:点 ...
- 037_自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)
#!/bin/bash#设置变量定义需要添加到防火墙规则的服务和端口号#使用 firewall-cmd --get-services 可以查看 firewall 支持哪些服务 service=&quo ...
- DOS窗口启动tomact,运用startup.bat/shutdown.bat命令启动/关闭tomcat
设置CATALINA_HOME环境变量1.CATALINA_HOME是TOMCAT安装路径的别名,目的是为了方便使用TOMCAT2.计算机>属性>环境变量, 新建环境变量.变量名为CATA ...
- luogu P4859 已经没有什么好害怕的了
嘟嘟嘟 题中给的\(k\)有点别扭,我们转换成\(a > b\)的对数是多少,这个用二元一次方程解出来是\(\frac{n + k}{2}\). 然后考虑dp,令\(dp[i][j]\)表示前\ ...