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 extends BaseBO { private static final int DEFAULT_PAGE_SIZE = 10; private int totalRows; private int pageSize = 10; private int currentPage; private int totalPages; private int startRow; 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 extends BaseBO { /** 分页信息 */ private Pager pager; /** 页记录列表 */ private List 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 * * @param countHql * find the number of result by HQL * @param qryHql * find the result by HQL * @param pageSize * the number of result by each page * @param currentPage * the page number * @return */ protected PageList findPageListWithHql(String countHql, String qryHql, int pageSize, int currentPage) { return findPageListWithHql(countHql, qryHql, null, pageSize, currentPage); } protected PageList findPageListWithHql(String countHql, String qryHql, List paramList, int pageSize, int currentPage) { PageList list = null; int totalRows = getCount(countHql, paramList); //get the number of result
if (totalRows < 1) { return list; }
list = new PageList(); Pager pager = new Pager(totalRows, pageSize, currentPage); //create a new instance of the page model list.setPager(pager); list.setList(this.findPageList(qryHql, paramList, pager.getStartRow(), pager.getPageSize()));
return list; }
then,let's learn the method called getCount() to get the number of result, this method is very easy.
protected int getCount(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 new RuntimeException(ex); } }
then ,let's see the last method called findPageList that to find the result :
protected List findPageList(String hql, List paramList, int startRow, int pageSize) { 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); return query.list(); } catch (HibernateException ex) { throw new RuntimeException(ex); } }