1,First of all, we should have a wrapper class for page,this class can calculate the startRow by the number of result,pageSize,and currentPage.
public class Pager extendsBaseBO {private static final int DEFAULT_PAGE_SIZE = 10;private inttotalRows;private int pageSize = 10;private intcurrentPage;private inttotalPages;private intstartRow;public Pager(int _totalRows, int _pageSize, int_currentPage) {
init(_totalRows, _pageSize, _currentPage);
}private void init(int _totalRows, int _pageSize, int_currentPage) {
totalRows=_totalRows;
pageSize=_pageSize;
currentPage=_currentPage;if (totalRows < 0) {
totalRows= 0;
}if (pageSize < 1) {
pageSize=DEFAULT_PAGE_SIZE;
}
totalPages= totalRows /pageSize;int mod = totalRows %pageSize;if (mod > 0) {
totalPages++;
}if (currentPage >totalPages) {
currentPage=totalPages;
}if (currentPage < 1) {
currentPage= 1;
}
startRow= (currentPage - 1) *pageSize;
}
}
then ,we should have a wrapper class for pageList:
public class PageList extendsBaseBO {/**分页信息*/
privatePager pager;/**页记录列表*/
privateList list;
................//omit the method of get and set
}
2,In the DAO,we can see :
public class YfsjIssueDAOImpl extends BaseHibernateDaoImpl{
public PageList findLogicB1(YfsjIssueModel condition){
..............//service code omitted
return findPageListWithHql(countHql, qryHql,pageSize,currentPage);
}
}
from this code,we find that all the DAO extend from the Class of BaseHibernateDaoImpl .And the Class of BaseHibernateDaoImpl declare a method called findPageListWithHql to control paging.
3,then let's learn this paging method declared in BaseHibernateDaoImpl :
abstract public class BaseHibernateDaoImpl extends HibernateDaoSupport implements BaseHibernateDao { /*** find the paging result
*
*@paramcountHql
* find the number of result by HQL
*@paramqryHql
* find the result by HQL
*@parampageSize
* the number of result by each page
*@paramcurrentPage
* the page number
*@return
*/
protected PageList findPageListWithHql(String countHql, String qryHql, int pageSize, intcurrentPage) {return findPageListWithHql(countHql, qryHql, null, pageSize, currentPage);
}protected PageList findPageListWithHql(String countHql, String qryHql, List paramList, int pageSize, intcurrentPage) {
PageList list= null;int totalRows =getCount(countHql, paramList); //get the number of result if (totalRows < 1) {returnlist;
}
list = newPageList(); Pager pager = newPager(totalRows, pageSize, currentPage); //create a new instance of the page modellist.setPager(pager); list.setList(this.findPageList(qryHql, paramList, pager.getStartRow(), pager.getPageSize()));returnlist;
}
then,let's learn the method called getCount() to get the number of result, this method is very easy.
protected intgetCount(String hql, List paramList) {try{
Query query= this.getSession().createQuery(hql);if (paramList != null && paramList.size() > 0) {for (int i = 0; i < paramList.size(); i++) {
query.setParameter(i, paramList.get(i));
}
}
List list=query.list();return ((Integer) list.get(0)).intValue();
}catch(HibernateException ex) {throw newRuntimeException(ex);
}
}
then ,let's see the last method called findPageList that to find the result :
protected List findPageList(String hql, List paramList, int startRow, intpageSize) {try{
Query query= this.getSession().createQuery(hql);if (paramList != null && paramList.size() > 0) {for (int i = 0; i < paramList.size(); i++) {
query.setParameter(i, paramList.get(i));
}
}
query.setFirstResult(startRow);
query.setMaxResults(pageSize);returnquery.list();
}catch(HibernateException ex) {throw newRuntimeException(ex);
}
}