hibernate中分页的实现

来源:互联网 发布:淘宝助理如何下架宝贝 编辑:程序博客网 时间:2024/05/19 04:54

 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>
原创粉丝点击