Java分页实例之-分页类

来源:互联网 发布:著名的中文域名有哪些 编辑:程序博客网 时间:2024/05/02 02:25
package com.dbtemplate.domain;

import org.apache.log4j.Logger;

/**
 * The Class PageInfo.
 
*/

public class PageInfo {

    
/** The logger. */
    
static Logger logger = Logger.getLogger(PageInfo.class.getName());

    
/** The total records. */
    
private int totalRecords;

    
/** The total pages. */
    
private int totalPages;

    
/** The page size. */
    
private int pageSize = 10;

    
/** The current page no. */
    
private int currentPageNo;

    
/** The previous page no. */
    
private int previousPageNo;

    
/** The next page no. */
    
private int nextPageNo;

    
/** The is first page. */
    
private boolean isFirstPage;

    
/** The is last page. */
    
private boolean isLastPage;

    
/** The has previous pag. */
    
private boolean hasPreviousPage;

    
/** The has next page. */
    
private boolean hasNextPage;    
    
    
/**
     * Instantiates a new page info.
     * 
     * 
@param totalRecords the total records
     
*/

    
public PageInfo(int totalRecords, int pageSize) {
        logger.debug(
"Start to initialize the page info.");
        
// 设置总记录数
        if(totalRecords >= 0{
            
this.totalRecords = totalRecords;
        }
 else {
            totalRecords 
= 0;
        }

        
// 设置每页记录数
        setPageSize(pageSize);        
        
// 设置总页面数
        if(totalRecords % pageSize == 0{
            totalPages 
= totalRecords/pageSize;
        }
 else {
            totalPages 
= (totalRecords/pageSize) + 1
        }

        
// 设置当前页
        currentPageNo = 1;
    }


    
/**
     * Gets the current page no.
     * 
     * 
@return the current page no
     
*/

    
public int getCurrentPageNo() {
        
return currentPageNo;
    }


    
/**
     * Sets the current page no.
     * 
     * 
@param currentPageNo the new current page no
     
*/

    
public void setCurrentPageNo(int currentPageNo) {
        
// 设置当前页
        if(currentPageNo < 1{
            
this.currentPageNo = 1;
        }
 else if(currentPageNo > totalPages) {
            
this.currentPageNo = totalPages;
        }
 else {
            
this.currentPageNo = currentPageNo;
        }

        
// 每次设置当前页时更新上一页、下一页、是否首页、是否末页、是否有上一页、是否有下一页标志
        isFirstPage = (currentPageNo == 1)?true:false;
        isLastPage 
= (currentPageNo == totalPages)?true:false;
        hasPreviousPage 
= (currentPageNo == 1)? false:true;
        hasNextPage 
= (currentPageNo == totalPages)? false:true;        
        previousPageNo 
= (hasPreviousPage)? (currentPageNo - 1):currentPageNo;
        nextPageNo 
= (hasNextPage)? (currentPageNo + 1):currentPageNo;
    }


    
/**
     * Gets the current page size.
     * 
     * 
@return the current page size
     
*/

    
public int getCurrentPageSize() {
        
if(totalRecords == 0{
            
return 0;
        }
 else if(currentPageNo < totalPages) {
            
// 非末页
            return pageSize;
        }
 else {
            
// 末页
            return (totalRecords - (currentPageNo -1* pageSize);
        }

    }
    

    
/**
     * Gets the current page start record.
     * 
     * 
@return the current page start record
     
*/

    
public int getCurrentPageStartRecord() {
        
return (currentPageNo - 1* pageSize + 1;
    }

    
    
/**
     * Gets the current page end record.
     * 
     * 
@return the current page end record
     
*/

    
public int getCurrentPageEndRecord() {
        
return (currentPageNo -1* pageSize + getCurrentPageSize();
    }
    
    
    
/**
     * Checks if is has next page.
     * 
     * 
@return true, if is has next page
     
*/

    
public boolean isHasNextPage() {
        
return hasNextPage;
    }

    
    
/**
     * Checks if is has previous pag.
     * 
     * 
@return true, if is has previous pag
     
*/

    
public boolean isHasPreviousPage() {
        
return hasPreviousPage;
    }

    
    
/**
     * Checks if is first page.
     * 
     * 
@return true, if is first page
     
*/

    
public boolean isFirstPage() {
        
return isFirstPage;
    }


    
/**
     * Checks if is last page.
     * 
     * 
@return true, if is last page
     
*/

    
public boolean isLastPage() {
        
return isLastPage;
    }


    
/**
     * Gets the page size.
     * 
     * 
@return the page size
     
*/

    
public int getPageSize() {
        
return pageSize;
    }


    
/**
     * Sets the page size.
     * 
     * 
@param pageSize the new page size
     
*/

    
public void setPageSize(int pageSize) {
        
this.pageSize = (pageSize <= 0)?10:pageSize;
    }


    
/**
     * Gets the previous page no.
     * 
     * 
@return the previous page no
     
*/

    
public int getPreviousPageNo() {
        
return previousPageNo;
    }


    
/**
     * Gets the next page no.
     * 
     * 
@return the next page no
     
*/

    
public int getNextPageNo() {
        
return nextPageNo;
    }
    
    
    
/**
     * Gets the total pages.
     * 
     * 
@return the total pages
     
*/

    
public int getTotalPages() {
        
return totalPages;
    }


    
/**
     * Gets the total records.
     * 
     * 
@return the total records
     
*/

    
public int getTotalRecords() {
        
return totalRecords;
    }

}
 
原创粉丝点击