一个标准的分页类(用于处理结果集)

package  com.util;

@SuppressWarnings(
" unused " )
public   class  Page
{
    
private int currentPage = 1//当前页
    private int rowPerPage = 5;  //每页几条
    private int totalPage;      //总页数
    private int totalRow;       //总行数
    private boolean hasFirstPage;  //是否有首页
    private boolean hasNextPage;   //是否有下一页
    private boolean hasPreviousPage;  //是否有上一页
    private boolean hasLastPage;     //是否有末页

    
public void init(int currentPage,int totalRow)
    
{
        
this.currentPage = currentPage;
        
this.totalRow = totalRow;
    }

    
    
public int getCurrentPage()
    
{
        
return currentPage;
    }


    
public void setCurrentPage(int currentPage)
    
{
        
this.currentPage = currentPage;
    }

    
//总页数
    public int getTotalPage()
    
{
        
return this.totalRow%this.rowPerPage == 0 ? this.totalRow/this.rowPerPage : this.totalRow/this.rowPerPage + 1;
    }


    
public void setTotalPage(int totalPage)
    
{
        
this.totalPage = totalPage;
    }


    
public int getRowPerPage()
    
{
        
return rowPerPage;
    }


    
public void setRowPerPage(int rowPerPage)
    
{
        
this.rowPerPage = rowPerPage;
    }


    
public int getTotalRow()
    
{
        
return totalRow;
    }


    
public void setTotalRow(int totalRow)
    
{
        
this.totalRow = totalRow;
    }

    
//是否有首页
    public boolean isHasFirstPage()
    
{
        
if(this.getCurrentPage() == 1)
            
return false;
        
return true;
    }


    
public void setHasFirstPage(boolean hasFirstPage)
    
{
        
this.hasFirstPage = hasFirstPage;
    }

    
//是否有最后一页
    public boolean isHasLastPage()
    
{
        
if(this.currentPage == this.getTotalPage())
            
return false;
        
return true;
    }


    
public void setHasLastPage(boolean hasLastPage)
    
{
        
this.hasLastPage = hasLastPage;
    }


    
public boolean isHasPreviousPage()
    
{
        
if(this.isHasFirstPage())
            
return true;
        
return false;
    }


    
public void setHasPreviousPage(boolean hasPreviousPage)
    
{
        
this.hasPreviousPage = hasPreviousPage;
    }


    
public boolean isHasNextPage()
    
{
        
if(this.isHasLastPage())
            
return true;
        
return false;
    }


    
public void setHasNextPage(boolean hasNextPage)
    
{
        
this.hasNextPage = hasNextPage;
    }

}

示例:一个查询所有用户的方法
public  List < User >  findAllUser( int  currentPage,  int  rowPerPage)
    
{
        Session session 
= this.getHibernateTemplate().getSessionFactory().openSession();
        Query query 
= session.createQuery("from User u");
        
int startRow = (currentPage -1* rowPerPage;
        query.setFirstResult(startRow);
        query.setMaxResults(rowPerPage);
        
        
把当前页和每页的要显示的行数传入即可. 164694.html

々上善若水々 2007-12-02 20:35 发表评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值