hibernate分頁技術

1.新建項目並配置好包。

2.dao

//分頁查詢hql查詢條件。

public List queryforpage(final String hql,final int offset,final int length);

//查詢所有記錄數hql查詢的條件,總記錄數

public int getAllRowCount(String hql);

daoImpl

public int getAllRowCount(String hql){

   return getHibernateTemplate().find(hql).size();

}

 public List queryforpage(final String hql,final int offset,final int length){

List list=getHibernateTemplate().executeFind(new HibernateCallback()){

   public Object doInHibernate(Session session) throws HibernateException,SQLException{

   Query query=session.createQuery(hql);

  query.setFirstResult(offset);

  query.setMaxResults(length);

  List list=query.list();

return list;

}

});

return list;

}

}

PageBean

public class PageBean implements Serializable{

private LIst list;//要返回的某一頁的記錄列表

private int allRow;  //總記錄數

private int totalPage; //總頁數

private int currentPage;  //當前頁

private int pageSize;  //每頁記錄數

private boolean isFirstPage; //是否爲第一頁

private boolean isLastPage; //是否爲最後一頁

private boolean hasPreviousPage; //是否有前一頁

private boolean hasNextPage; //是否有下一頁

getset

public void init(){//初始化分頁信息

this.isFirstPage=isFirstPage();

this.isLastPage=isLastPage();

this.hasPreviousPage=isHasPreviousPage();

this.hasNextPage=isHasNextPage();

}

public boolean isFirstPage(){//如是當前是第一頁,,判斷頁的信息,只需get方法

return currentPage==1;

}

public boolean isLastPage(){//如果當前頁是最後一頁

return currentPage==totalPage;

}

public boolean isHasPreviousPage(){//只要當前頁不是第一頁

return currentPage!=1;

}

public boolean isHasNextPage(){

return currentPage !=totalPage;//只要當前頁不是最後一頁

}

//計算總頁數,靜態方法,供外部直接通過類名調用

public static int countTotalPage(final int pageSize,final int allRow){

int totalPage=allRow%pageSize==0?allRow/pageSize:allRow/pageSize+1;

return totalPage;

}

//計算當前頁開始記錄

public static int countOffset(final int pageSize,final int currentPage){

final int offset=pageSize*(currentPage-1);

return offset;

}

//計算當前頁,假如爲0或者沒有值使用1代替

public static int countCurrentPage(int page){

final int curPage=(page==0?1:page);

return curPage;

}

}

service//分頁查詢currentPage當前第幾頁,pageSize每頁大小

public PageBean queryforPage(int pageSize,int currentPage);

serviceImpl//分頁查詢currentpage當前第幾頁,pagesize每頁大小,封閉了分頁信息的bean

public PageBean queryforPage(int pageSize,int page){

final String hql="from com**model.u";

int allRow=dao.getAllRowCount(hql);//總記錄數

int totalPage=PageBean.countTotalPage(pageSize,allRow);//總頁數

final int offset=PageBean.countOffset(pageSize,page);//當前頁開始記錄

final int length=pageSize;//每頁記錄數

final int currentPage=PageBean.countCurrentPage(page);

List<modell> list=dao.queryforpage(hql,offset,length);

//把每頁信息保存到bean中

PageBean pageBean=new PageBean();

pageBean.setpageSize(pageSize);

pageBean.setCurrentPage(currentPage);

pageBean.setAllRow(allRow);

pageBean.setTotalPage(totalPage);

pageBean.setList(list);

pageBean.init();

return pageBean;


}

jsp

s:iterator value="pageBean.list">
           
<s:property value="title"/>
           
<a href="getArticle.action?id=<s:property value="id"/>">modify</a>
           
<a href="deleteArticle.action?id=<s:property value="id"/>"οnclick="return askDel()"/>delete</a><br/>
       
</s:iterator>
       
<s:property value="pageBean.allRow"/>條記錄
       
<s:property value="pageBean.totalPage"/>
       
當前第<s:property value="pageBean.currentPage"/><br/>
        
       
<s:if test="%{pageBean.currentPage== 1}">
           
第一頁上一頁
       
</s:if>
       
<s:else>
           
<a href="listMyArticle.action?page=1">第一頁</a>
           
<a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage-1}"/>">上一頁</a>
       
</s:else>
       
<s:if test="%{pageBean.currentPage!= pageBean.totalPage}">
           
<a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage+1}"/>">下一頁</a>

   <a href="listMyArticle.action?page=<s:propertyvalue="pageBean.totalPage"/>">最後一頁</a>
       
</s:if>
       
<s:else>
           
下一頁 最後一頁
       
</s:else>





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章