hibernate中分页的实现

 hibernate中有两个接口可以有来实现分页:  
setFirstResult((currentPage-1)*pageSize+1);  
setMaxResults(pageSize);

 

做分页比较烦你还有定义一个分页工具类,对首页,第一页,下一页,尾页的定义
另外在DAO层就象2L所说的,用到HIBERNATE的那两个方法,然后在ACTION里调用,你可以参考以下代码:
工具类:

Java code
   
   
public class Page { private boolean hasPrevious; private boolean hasNext; private int rowsPerPage = 3 ; private int maxPage = 0 ; private int curPage = 1 ; private int maxRows = 0 ; public void init( int maxRows, int rowsPerPage) { this .maxRows = maxRows; this .rowsPerPage = rowsPerPage; if (maxRows == 0 ) { maxPage = 1 ; } else { maxPage = (maxRows + rowsPerPage - 1 ) / rowsPerPage; } } public void first() { curPage = 1 ; this .setHasPrevious( false ); refresh(); } public void previous() { curPage -- ; refresh(); } public void next() { if (curPage < maxPage) { curPage ++ ; } refresh(); } public void last() { curPage = maxPage; this .setHasNext( false ); refresh(); } public int getCurPage() { return curPage; } public void setCurPage( int curPage) { this .curPage = curPage; refresh(); } public boolean isHasNext() { return hasNext; } public void setHasNext( boolean hasNext) { this .hasNext = hasNext; } public boolean isHasPrevious() { return hasPrevious; } public void setHasPrevious( boolean hasPrevious) { this .hasPrevious = hasPrevious; } public int getMaxPage() { return maxPage; } public void setMaxPage( int maxPage) { this .maxPage = maxPage; refresh(); } public int getMaxRows() { return maxRows; } public void setMaxRows( int maxRows) { this .maxRows = maxRows; refresh(); } public int getRowsPerPage() { return rowsPerPage; } public void setRowsPerPage( int rowsPerPage) { this .rowsPerPage = rowsPerPage; refresh(); } public void refresh() { if (maxPage <= 1 ) { hasPrevious = false ; hasNext = true ; } else if (curPage == 1 ) { hasPrevious = false ; hasNext = true ; } else if (curPage == maxPage) { hasPrevious = true ; hasNext = false ; } else { hasPrevious = true ; hasNext = true ; } } }


DAO层:

Java code
   
   
public List selectProduct( int sortid ,Page pa) { Session sess = HibernateSessionFactory.getSession(); Query q = sess.createQuery( " from Product p where p.sort.sid=? " ); q.setInteger( 0 , sortid); q.setFirstResult((pa.getCurPage() - 1 ) * pa.getRowsPerPage()); q.setMaxResults(pa.getRowsPerPage()); List list = q.list(); sess.close(); return list; }


ACTION:

Java code
   
   
public class ProductAction extends DispatchAction { private Page page = new Page(); public ActionForward productPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ShopMainImpl impl = new ShopMainImpl(); String sortid = request.getParameter( " sid " ); int sid = Integer.parseInt(sortid); int totalRows = impl.getMaxCount(sid); page.setMaxRows(totalRows); page.init(page.getMaxRows(), page.getRowsPerPage()); String viewpage = request.getParameter( " viewpage " ); String action = request.getParameter( " action " ); if (viewpage != null && viewpage != "" ) { page.setCurPage(Integer.parseInt(viewpage)); } if (action != null ) { if (action.equalsIgnoreCase( " first " )) { page.first(); } else if (action.equalsIgnoreCase( " previous " )) { page.previous(); } else if (action.equalsIgnoreCase( " next " )) { page.next(); } else if (action.equalsIgnoreCase( " last " )) { page.last(); } } List product = impl.selectProduct(sid,page); request.setAttribute( " product " , product); request.setAttribute( " page " , page); request.setAttribute( " sid " , sid); return mapping.findForward( " main " ); } }


JSP:

Java code
   
   
< td colspan = " 4 " align = " center " > < c:choose > < c:when test = " <%=pa.getCurPage()==1 %> " > 首页 & nbsp;上一页 </ c:when > < c:otherwise > < a href = " product.do?method=productPage&sid=<%=request.getAttribute( " sid " ) %>&viewpage=&action=first " > 首页 </ a >& nbsp; < a href = " product.do?method=productPage&sid=<%=request.getAttribute( " sid " ) %>&viewpage=&action=previous " > 上一页 </ a >& nbsp; </ c:otherwise > </ c:choose > < c:choose > < c:when test = " <%=pa.getCurPage()==pa.getMaxPage() %> " > 下一页 & nbsp;尾页 </ c:when > < c:otherwise > < a href = " product.do?method=productPage&sid=<%=request.getAttribute( " sid " ) %>&viewpage=&action=next " > 下一页 </ a >& nbsp; < a href = " product.do?method=productPage&sid=<%=request.getAttribute( " sid " ) %>&viewpage=&action=last " > 尾页 </ a >& nbsp; </ c:otherwise > </ c:choose > </ td >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值