Java分页类 Page

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONObject;

/**
 * 分页类
 * @author rubekid
 *
 * @param <T>
 */
public class Page<T> extends QueryParameter {

    public static final int PAGE_SIZE = 20;
    private List<T> result = null;
    private long totalCount = -1;
    private JSONObject queryParams = new JSONObject();

    public Page() {
        setPageSize(PAGE_SIZE);
    }

    public Page(int pageSize) {
        setPageSize(pageSize);
    }

    public Page(int pageSize, boolean autoCount) {
        setPageSize(pageSize);
        this.autoCount = autoCount;
    }

    /**
     * 获取反向排序
     * @return
     */
    public String getInverseOrder() {
        if (order.endsWith("desc")){
            return "asc";
        }
        return "desc";
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    public long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public long getTotalPages() {
        if (totalCount < 0){
            return -1;
        }

        long count = totalCount / pageSize;
        if (totalCount % pageSize > 0){
            count ++;
        }
        return count;
    }

    public boolean isHasNext() {
        return (pageNo + 1 <= getTotalPages());
    }

    public int getNextPage() {
        if (isHasNext()){
            return pageNo + 1;
        }
        return this.pageNo;
    }

    public boolean isHasPre() {
        return (pageNo - 1 >= 1);
    }

    public int getPrePage() {
        if (isHasPre()){
            return pageNo - 1;
        }
        return pageNo;
    }

    public void addQueryParam(String key, Object value) {
        queryParams.put(key, value);
    }

    @SuppressWarnings("unchecked")
    public String getQueryString() throws UnsupportedEncodingException{
        String result = "";
        for (Iterator<String> iterator = queryParams.keys(); iterator.hasNext();) {
            String key = iterator.next();
            result += key + "=" + URLEncoder.encode(queryParams.getString(key), "utf-8") + "&";
        }

        if (result.length() > 0){
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    public String getQueryParamString() {
        return queryParams.toString();
    }

    public void setQueryString(String queryString) {
        queryParams = JSONObject.fromObject(queryString);
    }

    public JSONObject getQueryParams() {
        return queryParams;
    }
}
时间: 2024-11-05 05:55:54

Java分页类 Page的相关文章

(转)ThinkPHP使用心得分享-分页类Page的用法

转之--http://www.jb51.net/article/50138.htm ThinkPHP中的Page类在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page类: 复制代码代码如下: import('ORG.Util.Page'); //Page类的引入$db = M('abc');//实例化数据表abc$where = array('id'=>'2';);//条件语句$where,例表中字段id的值为2$count

page分页类

<?php /** file: Page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; //SQL语句使用limit从句,限制获取记录个数 private $uri; //自动获取url的请求地址 private $pageNum; //总页数 private $page; //当前页 private $config = arr

php分页类

<?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; //SQL语句使用limit从句,限制获取记录个数 private $uri; //自动获取url的请求地址 private $pageNum; //总页数 private $page; //当前页 private $config = arr

分页类

<?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; //SQL语句使用limit从句,限制获取记录个数 private $uri; //自动获取url的请求地址 private $pageNum; //总页数 private $page; //当前页 private $config = arr

封装的完美分页类

<?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; //SQL语句使用limit从句,限制获取记录个数 private $uri; //自动获取url的请求地址 private $pageNum; //总页数 private $page; //当前页 private $config = arr

完美分页类

1 <?php 2 header("Content-Type:text/html;charset=utf-8"); 3 //加载分页类 4 include "page.class.php"; 5 6 //定义总数 7 $total =0; 8 9 //1.造连接对象 10 $db = new mysqli("localhost","root","123","mydb"); 11 1

加载分页类(实例并优化)

1 <html> 2 <head> 3 <style type="text/css"> 4 .p1 5 { 6 color:yellow; 7 background-color:#00F; 8 font-size:14px; 9 } 10 </style> 11 </head> 12 <body> 13 <?php 14 header("Content-Type:text/html;charset=

PHP+jQuery 实现可选择url和ajax方式的分页类

1 /* 2 ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.min.js + Smarty 3.1.18 ******* 3 ******* Date:2014-10-11 4 ******* Author:小dee5 ******* Blog:http://www.cnblogs.com/dee0912/6 */ 做了一个分页的功能模块,主要的文件包括

封装 分页类

<?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; //SQL语句使用limit从句,限制获取记录个数 private $uri; //自动获取url的请求地址 private $pageNum; //总页数 private $page; //当前页 private $config = arr