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");省略======================
时间: 2024-08-28 08:46:33

Spring分页实现PageImpl<T>类的相关文章

使用spring注解,注入sessionFactory类

简述 目前使用spring hibernate作为项目的框架,并且使用注解方式进行对象装载.在装载Dao对象的时候常常需要注入sessionFactory对象,通常的做法是Dao继承至HibernateDaoSuppor,t然后在Dao中添加setSuperSessionFactory 方法进行注入的,这几天网上又看到一种更好的方法,所以这里就把这两种方法都记录一下. 方法一(继承HibernateDaoSupport) 这个是比较常用的方法,看到很多文章中都使用这种方式. 前置条件: sess

java实现Spring在XML配置java类

1.创建自己的bean文件:beans.xml <?xml version="1.0" encoding="UTF-8"?> <busi-beans> <beans> <bean id="SysHelloImpl" type="com.cxm.test.SysHello"> <desc>test</desc> <impl-class>com.c

Spring常用的接口和类(二)

七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostProcessor接口时,需要实现以下两个方法: postProcessBeforeInitialization 在受管bean的初始化动作之前调用 postProcessAfterInitialization 在受管bean的初始化动作之后调用容器中的每个Bean在创建时都会恰当地调用它们.代码展示如下

Spring源码解析—— ClassPathResource类

一,简单介绍Spring中资源处理相关类 BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); 在Spring中,定义了接口InputStreamSource,这个类中只包含一个方法: public interface InputStreamSource { /** * Return an {@link InputStream}. * <p>I

获取Spring容器Bean对象工具类

在开发中,总是能碰到用注解注入不了Spring容器里面bean对象的问题.为了解决这个问题,我们需要一个工具类来直接获取Spring容器中的bean.因此就写了这个工具类,在此记录一下,方便后续查阅.废话不多说,直接上代码. 一.代码 package com.zxy.demo.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext;

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

验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginController控制器中判断验证码是否正确并且判断登陆是否成功 文件上传 用到的知识点: 1.文件上传的时候,要设置表单的enctype属性 2.$_FILE[名字][]用来接收文件的信息 第二维的字段: name size error type tmp_name 3.move_uploaded_fil

Hibernate+SpringMVC+Spring+分页实现留言管理项目

项目结构: 这里使用到了Mysql数据库 所用到的包:略. 首先进行springmvc.xml的配置,注意数据库密码要改为自己的. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema

8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource

8.3.1 Resource实现类------ClassPathResource : 访问类加载路径下的资源的实现类 2.访问类加载路径下的资源 ClassPathResource 用来访问类加载路径下的资源,相对于其他的Resource实现类,其主要优势是方便访问类加载路径下的资源,尤其对于Web应用,ClassPathResource可自动搜索位于WEB-INF/classes下的资源文件,无须使用绝对路径访问. /*创建一个Resource对象,从类加载路径下读取资源*/ ClassPat

MyBatis Spring整合配置映射接口类与映射xml文件

Spring整合MyBatis使用到了mybatis-spring,在配置mybatis映射文件的时候,一般会使用MapperScannerConfigurer,MapperScannerConfigurer会自动扫描basePackage指定的包,找到映射接口类和映射XML文件,并进行注入.配置如下: [html] view plain copy <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v