java hibernate分页_Java项目中基于Hibernate分页总结

本文介绍了如何使用Java编写分页逻辑,通过创建`Pager`和`PageList`wrapper类,展示了如何在DAO中运用`BaseHibernateDaoImpl`进行分页操作,包括`findPageListWithHql`方法和`getCount`、`findPageList`的细节。涉及到了ORM框架下的数据库查询和分页控制技术。
摘要由CSDN通过智能技术生成

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值