Java项目中基于Hibernate分页总结

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

 

 

 

 

转载于:https://www.cnblogs.com/cheeper/p/3304585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值