Hibernate 分页

public class Pager {
	
	
	private int rows;					//总行数
	private int pages;					//总页数
	private int currentPages;			//当前页
	private int maxResults;				//当前显示的行数
	private int firstResults;			//当前页的首记录
	private int currentResults;			//当前页的末记录
	private int endPages;				//当前页显示的最后页数
	private int startPages;				//当前页显示的起始页数
	
	

	
	
	public Pager() {   
	}   							
	
	/**
	 * 
	 * @param _totalRows 总记录数
	 * @param _pageSize	每页显示的行数
	 * @param _pageShow	要显示的页数
	 * @param _currentPage 当前页
	 */
	public Pager(int _totalRows, int _pageSize, int _pageShow, int _currentPage) { 
		rows = _totalRows;			//查询的总记录数
		maxResults = _pageSize;
		currentPages = _currentPage;

		if(rows==0){				//总记录数为零将所有的记录都赋为零
			pages = 0;				//总页数
			maxResults = 0;			//每页显示的行数
			currentPages = 0; 		//当前页
			firstResults = 0;		//当前页的首记录
			currentResults = 0;		//当前页的末记录
		}else{
			pages = rows/_pageSize;   //总记录/每页要显示的记录=总页数
			int mod = rows%_pageSize; //总记录和每页要显示的记录求余结果大于0总页数加1  
			if(mod>0){   
				pages++;   
			}   
			firstResults = 1;   	//设置当前第一条记录为零
		}
		
		
		if(pages<_pageShow && pages>=1) {					//如果总页数小于显示的页数 
			endPages = pages;								//则: 要显示的页数=总页数
			startPages = 1;									//则:当前起始页为1 
			endPages = _pageShow;
		}else if(pages>_pageShow){							//如果总页数大于显示的页数
			if(currentPages-_pageShow/2<1) {				//如果当前页-显示页数/2<1
				endPages = _pageShow;						//则:这个页最后显示的页数为 _pageShow
				startPages = 1;								//则:起始页为1 
			}else if (currentPages+_pageShow/2>pages){		//如果当前页+显示页数/2<pages
				endPages = pages;							//则:显示的页数为 pages
				startPages = pages-_pageShow+1;				//则:起始页为pages-_pageShow 
			}else{
				endPages = currentPages+_pageShow/2;		//显示的页数
				startPages = currentPages-_pageShow/2+1;	//起始页 
				
			}
		}else if(pages==_pageShow){
			if(currentPages-_pageShow/2<1) {				//如果当前页-显示页数/2<1
				endPages = _pageShow;						//则:这个页最后显示的页数为 _pageShow
				startPages = 1;								//则:起始页为1 
			}else if (currentPages+_pageShow/2>pages){		//如果当前页+显示页数/2<pages
				endPages = pages;							//则:显示的页数为 pages
				startPages = pages-_pageShow+1;				//则:起始页为pages-_pageShow 
			}else{
				endPages = currentPages+_pageShow/2;		//显示的页数
				startPages = currentPages-_pageShow/2+1;	//起始页 
			}
		}else{
			System.out.println("错误");
			endPages = 0;									//显示的页
			currentPages = 0;								//当前页
			startPages = 0;									//起始页
		}

		
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPages() {
		return pages;
	}
	public void setPages(int pages) {
		this.pages = pages;
	}
	public int getCurrentPages() {
		return currentPages;
	}
	public void setCurrentPages(int currentPages) {
		this.currentPages = currentPages;
	}
	public int getMaxResults() {
		return maxResults;
	}
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}
	public int getFirstResults() {
		return firstResults;
	}
	public void setFirstResults(int firstResults) {
		this.firstResults = firstResults;
	}
	public int getCurrentResults() {
		return currentResults;
	}
	public void setCurrentResults(int currentResults) {
		this.currentResults = currentResults;
	}
	public int getEndPages() {
		return endPages;
	}
	public void setEndPages(int endPages) {
		this.endPages = endPages;
	}
	public int getStartPages() {
		return startPages;
	}
	public void setStartPages(int startPages) {
		this.startPages = startPages;
	} 
	
	
	public void first(){
		currentPages = 1;								//当前页为1
		firstResults = 1;								//首记录为1
		if(rows/maxResults >1) {						//总行数/每页显示的最大结果数大于1
			currentResults = maxResults;				//当前页的麽记录=每页显示的最大结果数
		} else {										//总行数/每页显示的最大结果数小于1
			currentResults = rows%maxResults;			//当前页的麽记录=总行数/每页要显示最大结果的余数
		}
	}
	
	public void last(){
		firstResults = (pages-1)*maxResults;			//当前页的首记录=总页数减一乘以每页要显示的最大结果数
		currentPages = pages;							//当前页就等于总页数
		currentResults = rows;							//当前页的麽记录=总行数 
	}
	
	public void next(){
		if(currentPages==pages){
			this.last();
		}else{
			currentPages = currentPages + 1;				//当前页加1
			firstResults = (currentPages - 1) * maxResults +1;			//当前页的首记录 = 上一页的首记录+每页显示的最大结果
			if(pages > currentPages) {						//如果总页数大于当前页
				currentResults = firstResults + maxResults - 1;	//当前页的麽记录 = 当前页的首记录+每页显示的最大结果
			} else {										//负责
				if(rows%maxResults==0) {
					currentResults = rows;					//当前页的麽记录 = 当前页的首记录 + 总行数/每页要显示最大结果的余数
				}else{
					currentResults = firstResults + rows%maxResults-1;//当前页的麽记录 = 当前页的首记录 + 总行数/每页要显示最大结果的余数
				}
			}
		}
	}
	
	public void before(){
		if(currentPages==1){
			this.first();
		}else{
			currentResults = (currentPages - 1) * maxResults-1;					//当前页的麽记录 = 上一页的首记录
			firstResults = currentResults - maxResults;			//当前页的首记录 = 上一页的首记录 - 每页显示的最大结果数
			currentPages--;
		}
	}
	
	public void changePage(){
		firstResults = (currentPages-1)*maxResults+1;		//当前页的首记录 = (当前页-1)*每要显示的最大结果数
		if(pages > currentPages) {						//如果总页数大于当前页
			currentResults = firstResults + maxResults-1;	//当前页的麽记录 = 当前页的首记录 + 每页显示的最大结果数
		} else {
			if(rows%maxResults==0) {
				currentResults = firstResults+maxResults-1;					//当前页的麽记录 = 当前页的首记录 + 总行数/每页要显示最大结果的余数
			}else{
				currentResults = firstResults + rows%maxResults-1;//当前页的麽记录 = 当前页的首记录 + 总行数/每页要显示最大结果的余数
			}
		}
	}
	
	public void refresh(int _currentPage){
		currentPages = _currentPage;   
		if (currentPages > pages) {   
		last();   
		} 
	}
	
	
	
	

}

public class HelpPager {
	
	
	/**
	 * 
	 * @此方法描述的是:分页查询
	 * @param request	
	 * @param _totalRows	总记录数
	 * @param _pageSize		每页显示多少条
	 * @param _pageShow		要显示的多少页
	 * @return
	 * Pager
	 */
	public static Pager getPager(HttpServletRequest request,   
			int _totalRows, int _pageSize, int _pageShow) { 
		

			
			
		
			//定义pager对象,用于传到页面
			int _currentPage = 1;
			String currentPage = request.getParameter("currentPages");   
			if(null!=currentPage && !"".equals(currentPage)){
				_currentPage = Integer.valueOf(currentPage);
			}
		
			Pager pager = new Pager(_totalRows, _pageSize, _pageShow, _currentPage);   
			//从Request对象中获取当前页号   
			//如果当前页号为空,表示为首次查询该页   
			//如果不为空,则刷新pager对象,输入当前页号等信息   
//			if (currentPage != null) {   
//				pager.refresh(Integer.parseInt(currentPage));   
//			}   
			//获取当前执行的方法,首页,前一页,后一页,尾页。   
			String pagerMethod = request.getParameter("pageFlag");   
			if (pagerMethod != null) {   
				if (pagerMethod.equals("first")) {   
					pager.first();   
				} else if (pagerMethod.equals("last")) {   
					pager.last();   
				} else if (pagerMethod.equals("next")) {   
					pager.next();   
				} else if (pagerMethod.equals("before")) {   
					pager.before();   
				} else if(pagerMethod.equals("changePage")){
					pager.changePage();
				}
			}else{
				pager.refresh(_currentPage);
			}   
			return pager;   
	}   

}
public class OrderAction extends BaseAction{

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("查询翻译清单");
		ITrOrder iTrOrder = (ITrOrder) BeanHelp.getBean("iTrOrder");
		Date date = null;
		
		int n = iTrOrder.getHoTranslateOrderRow("one", "two", date, "four", "five", "six");
		Pager pager = HelpPager.getPager(request, n, 2, 2);
		List list = iTrOrder.getHoTranslateOrderList(2, pager.getFirstResults(), "one", "two", date, "four", "five", "six");
		request.setAttribute("list", list);
		request.setAttribute("pager", pager);
		return mapping.findForward("back");
	}

}


jsp:
<div class="page">
     	每页<span class="color_red">30</span>条/共<span class="color_red">${pager.rows}</span class="color_red">条  
     	第<span class="color_red">${pager.currentPages}</span>页/共<span>${pager.pages}</span>页
     	<!-- 
     	<a href="#">【首页】</a>
     	<a href="#">【上页】</a>
     	<a href="#">【下页】</a>
     	<a href="#">【末页】</a> 
     	 -->
     	<html:link action="/bid.do?pageFlag=first" paramName="pager" paramProperty="currentPages" paramId="currentPages">【首页】</html:link>
     	<html:link action="/bid.do?pageFlag=before" paramName="pager" paramProperty="currentPages" paramId="currentPages">【上页】</html:link>
     	<html:link action="/bid.do?pageFlag=next" paramName="pager" paramProperty="currentPages" paramId="currentPages">【下页】</html:link>
     	<html:link action="/bid.do?pageFlag=last" paramName="pager" paramProperty="currentPages" paramId="currentPages">【末页】</html:link>
     	转到 <input type="text" class="txt" id="jp" value="${pager.currentPages}"/> 页 <input type="button" value="GO" onclick="jumppage()"/> </div>
     	<script type="text/javascript">
     		var path = "";
			var ua = navigator.userAgent.split(";"); 
			if(ua[2].indexOf("Windows")!=-1) { 
				path = "/soho/"; 
			}else { 
				path = "http://soho.hereonline.com/"; 
			}
     		
     		function jumppage(){
     			var jp = document.getElementById("jp").value;
     			if(jp>${pager.pages}){
     				alert("超出页数范围");
     				return false;
     			}
     			if(jp<1){
     				alert("超出页数范围");
     				return false;
     			}
     			window.location=path+"bid.do?pageFlag=changePage&currentPages="+jp;
     		}
     	</script>
  </div>


DAO and Service:


/**
	 * 
	 * @此方法描述的是:查询 记录
	 * @param maxResults
	 * @param startResult
	 * @param one
	 * @param two	 
	 * @param three	
	 * @param four	
	 * @param five	 
	 * @param six	 
	 * @return
	 * List
	 */
public List getHoTranslateOrderList(int maxResults, int startResult, String one,
			String two, Date three, String four, String five, String six); 


	public List getHoTranslateOrderList(int maxResults, int startResult, String one,
			String two, Date three, String four, String five, String six) {
		String SQL = "select * from HereOnLine.HO_TranslateOrder ho";
			return queryBySQL(SQL, HoTranslateOrder.class, maxResults, startResult);
		
		
	}
	
	/**
	 * 
	 * @此方法描述的是:查询一共多少条记录
	 * @param one			 
	 * @param two			 
	 * @param three			 
	 * @param four			
	 * @param five			 
	 * @param six			 
	 * @return
	 * int
	 */
public int getHoTranslateOrderRow(String one, String two, Date there, String four, String five, String six);

public int getHoTranslateOrderRow(String one, String two, Date three,
			String four, String five, String six) {
		String SQL = "select count(*) from HO_TranslateOrder ho";
		return queryBySQLRows(SQL);

	}

Hibernate:

public List queryBySQL(String SQL, Class EntityClass, int maxResults, int startResults, Date startDate, Date endDate) {
		Session session = this.getHibernateSession();
		try {
			return session.createSQLQuery(SQL).addEntity(EntityClass)
											  .setFirstResult(startResults-1)
											  .setMaxResults(maxResults)
											  .setDate(0, startDate)
											  .setDate(2, endDate)
											  .list();
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}finally{
			session.close();
		}
	}

public int queryBySQLRows(String SQL){
		Session session = this.getHibernateSession();
		try {
			return Integer.valueOf(((session.createSQLQuery(SQL).list().get(0))).toString());
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}finally{
			session.close();
		}
	}

你可能感兴趣的:(DAO,sql,windows,Hibernate,jsp)