hibernate分页

   需要一个辅助类,记录总页数,每页显示记录数,当前页,总记录数。

import java.io.Serializable;

public class PageView implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3121477018541456187L;

	/** 总页数 **/
	private int totalpage = 1;
	/** 每页显示记录数 **/
	private int maxresult = 1000;
	/** 当前页 **/
	private int currentpage = 1;
	/** 总记录数 **/
	private int totalrecord;

	/** 要获取记录的开始索引 **/
	public int getFirstResult() {
		return (this.currentpage - 1) * this.maxresult;
	}

	public PageView() {
	}

	public PageView(int maxresult, int currentpage) {
		this.maxresult = maxresult;
		this.currentpage = currentpage;
	}

	public long getTotalrecord() {
		return totalrecord;
	}

	public void setTotalrecord(int totalrecord) {
		this.totalrecord = totalrecord;
		this.totalpage = this.totalrecord % this.maxresult == 0 ? this.totalrecord
				/ this.maxresult : this.totalrecord / this.maxresult + 1;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public int getMaxresult() {
		return maxresult;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
}

 

  

createCriteria  这个接口代表对一个特定的持久类的查询。

 

/**
	 * 分页查询
	 * @param scope    offset获取记录的开始索引     length 一页显示的记录数目
	 * @return
	 */
	public List getDDList(final CreditOrderScope scope, final int offset, final int length){
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(org.hibernate.Session session) {
				Criteria c = session.createCriteria(RetailDD.class);
				c.add(Expression.in(STATE, scope.getState()));
				if(Boolean.TRUE.equals(scope.getReplaceFlag())){
					c.add(Expression.isNull(RECON_FLAG));
				}
				if(scope.getResult() != null){
					c.add(Expression.eq(RESULT, scope.getResult()));
				}
				Criteria p = c.createCriteria("DPackage");
				if(scope.getStartDate() != null){
					p.add(Expression.ge("dcDate",scope.getStartDate()));
				}
				if(scope.getEndDate() != null){
					p.add(Expression.le("dcDate",scope.getEndDate()));
				}
				if(scope.getFileName() != null && !"".equals(scope.getFileName())){
					p.add(Expression.eq(DDPackageDAO.FILE_NAME, scope.getFileName()));
				}
				p.addOrder(Order.asc("dcDate"));
				c.addOrder(Order.asc(IN_ID));
				//分页***************
				c.setFirstResult(offset);
				c.setMaxResults(length);
				return c.list();
			}
		});
		return list;
	}

 

   代码调用查询逻辑

 

//总记录数
				int totalrecord = dd1.size();
				//计算总金额
				RetailDD fp = null;
				BigDecimal amt1 = new BigDecimal("0.0");
				//可选文件名称
				String ag1 = "";
				String ag2 = "";
				StringBuffer fn = new StringBuffer();				
				//分页显示
				PageView pv = null;
				List dd = null;
				if(scope.getNextPage()>1) {
					pv = new PageView(1000,scope.getNextPage());
					pv.setTotalrecord(totalrecord);
					dd = serv.getDDListPage(scope,pv.getFirstResult(),1000);
				} else {
					//首页显示
					pv = new PageView(1000,1);
					pv.setTotalrecord(totalrecord);
					dd = serv.getDDListPage(scope,pv.getFirstResult(),1000);
				}
				//当前页
				rsp.setCurrentPage(pv.getCurrentpage());
				//总记录
				rsp.setTotalrecord(totalrecord);
				//总页数
				rsp.setTotalpage(pv.getTotalpage());
				//每页显示数
				rsp.setPageSize(dd.size());
				//总金额
				rsp.setTotalMoney(amt1.doubleValue());
				//可选文件名
				rsp.setFileName(fileName);
				rsp.setFplist(dd);

 界面主要部分代码

 

 

    	         function jumpPage(selObj){
			var pageNumber=selObj.options[selObj.selectedIndex].value;
			document.query.nextPage.value = pageNumber;
			document.query.outExcel.value = false;
			document.query.submit();
		}
		function gotoPage(no,fno,lno){
			if(lno==0){
				return;
			}
			if(no==fno && no!=lno) {
				alert("已经是第一页啦!");
				return;
			}
			if(no==lno && no==fno) {
				alert("已经是最后一页啦!");
				return;
			}
		
			document.query.nextPage.value = no;
			document.query.outExcel.value = false;
			document.query.submit();
			//document.gatherOrder.action = "ra_01_0101.do?orderType=22&nextPage="+no;
        	//document.gatherOrder.submit();
		}
		function doExportExcel(){
		frmExport.action = "excel2.jsp";
		frmExport.target = "excel2";
		frmExport.submit();
	}
   <%
	//当前页
	int currentPage = ((Integer)request.getAttribute("currentPage")).intValue();
	//总记录
	int totalrecord = ((Integer)request.getAttribute("totalrecord")).intValue();
	//总页数
	int totalpage = ((Integer)request.getAttribute("totalpage")).intValue();
	//每页显示数
	int pageSize = ((Integer)request.getAttribute("pageSize")).intValue();
	//总金额
	Double totalMoney = (Double)request.getAttribute("totalMoney");
	//可选文件名
	String fileName = (String)request.getAttribute("fileName");
	
	List fpdds = (List)request.getAttribute("fpdcs");
	if(fpdds == null) fpdds = new ArrayList();
	Set fileSet = new TreeSet();
%>
 <table border="0"  cellspacing="0"  cellpadding="0"  width="80%" >
		<tr>
		  <td>每页1000笔/共<%=totalrecord%>笔 第<%=currentPage%>页/共<%=totalpage%>页   总金额:<nstc:out money="<%=totalMoney%>"/>元
		  <span style="width:40px;">&nbsp;</span>
		    [<a href="javascript:gotoPage(1,<%=currentPage %>,<%=totalpage %>)">首页</a>][<a href="javascript:gotoPage(<%=currentPage>=2?currentPage-1:currentPage%>,<%=currentPage %>,<%=totalpage %>)">上一页</a>][<a href="javascript:gotoPage(<%=(currentPage+1)>totalpage?totalpage:(currentPage+1)%>,<%=currentPage %>,<%=totalpage %>)">下一页</a>][<a href="javascript:gotoPage(<%=totalpage%>,<%=currentPage %>,<%=totalpage %>)">末页</a>]
		      转第
		      <select name="jump" οnchange="jumpPage(this)">
		      <%
		      	for (int i = 1; i <= totalpage; i++) {
			        if (i == currentPage) {%>
			          <option selected value="<%=i%>"><%=i%></option>
			          <%}
			          else{%>
			          <option value="<%=i%>"><%=i%></option>
			          <%}
		        }%>
		      </select>
		      页
		    </td>
		  </tr>
		</table>
	 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值