Java分页实例之-分页类

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;
    }

}
 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值