Spring分页实现PageImplT类
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()*/@Overridepublic 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)*/@Overridepublic 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()*/@Overridepublic 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");省略======================
转载于:https://www.cnblogs.com/kuoAT/p/6944162.html
Spring分页实现PageImplT类相关推荐
- 【struts2+hibernate+spring项目实战】分页功能的完整的实现(通用分页、基类实现)
一.概述 今天自己做了个项目练练,然后有一些分页的功能,自己把分页的功能做了一个简单的总结,然后,为了以后能够方便自己的开发,做了一个baseDao的实现. 二.代码实现 2.1.分页的实体类page ...
- C【C#公共帮助类】分页逻辑处理类
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace Commo ...
- 访问指定html页面,Spring boot的Controller类是如何指定HTML页面的
Spring boot的Controller类是指定HTML页面的实现的方法如下: 1.在spring boot中借鉴servlet的方法输出html: @RequestMapping(value=& ...
- SpringBoot中在普通类里面加载Spring容器中的类
前言 在我们的工作中,经常会遇到需要在普通类中使用放在Spring容器中的类的情况.最常见的情况大概就是有一个类他的属性的是通过spring的配置文件读取的.这样这个类必然要交给Spring容器进行管 ...
- [zz]Spring MVC 之 SimpleFormController类別
[zz]Spring MVC 之 SimpleFormController类別 SimpleFormController类別 Spring附有一些简单的窗体处理handler,这些handler都实现 ...
- Spring 的优秀工具类盘点---转
第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ 文件资源操作 文件资源的 ...
- Spring boot的配置类
@Configuration 指明当前类是一个配置类 来替代之前的Spring配置文件 Spring boot的配置类 相当于Spring的配置文件 容器添加组件 Spring,通过配置文件添加组件 ...
- SpringBoot中操作spring redis的工具类
场景 SpringBoot+Vue+Redis实现前后端分离的字典缓存机制: https://blog.csdn.net/badao_liumang_qizhi/article/details/108 ...
- Spring 的优秀工具类盘点
Spring 的优秀工具类盘点---转 第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring- ...
最新文章
- Mybatis 使用的 9 种设计模式,真是太有用了
- 什么是XLNet,它为什么比BERT效果好?
- 腾讯大数据平台,要“没人管”了
- 解决 VCENTER ROOT 密码过期无法登陆 USER PASSWORD EXPIRED
- HDU 1261 字串数
- 基于opencv的霍夫方法和RANSAC方法两种圆检测
- freemarker写入word【未完,待续】
- 求素数的方法完整归纳,学的不仅是“求素数”!
- apereo cas mysql_Apereo CAS 5.0.X 默认提供的数据库认证的四种方式
- 快速排序c语言实现,快速排序的C语言代码实现
- 解决placeholder样式设置无效问题,更改placeholder默认样式颜色
- 父组件向子组件传递数据
- 数据库系统概论速成?
- 二分图匹配问题之km算法代码
- SpringMVC+VUE开发环境搭建
- Android自定义一个时间轴,通过ListView来实现时间轴的效果
- SE96X、CM500-I27条码扫描引擎使用经验
- python自动排版_你熟悉Python的代码规范吗?如何一键实现代码排版
- Android隐藏虚拟按键
- [Obsidian]懒人必备插件附使用教程