1. 程式人生 > >oracle,mysql,sqlserver分頁查詢,附實體類

oracle,mysql,sqlserver分頁查詢,附實體類

 最近簡單的對oracle,mysql,sqlserver2005的資料分頁查

       (一)、 mysql的分頁查詢

        mysql的分頁查詢是最簡單的,藉助關鍵字limit即可實現查詢,查詢語句通式:

複製程式碼

/*

* sql:可以是單表的查詢語句,也可以是多表的聯合查詢語句

* firstIndex:其實的索引

* pageSize:每頁顯示的記錄數

*/

select o.* from (sql) o limit firstIndex,pageSize

複製程式碼

如下面的截圖,每頁顯示的記錄數為20:

                                                  查詢(1-20)這20條記錄

                                             查詢(21-40)這20條記錄

        mysql的分頁查詢就這麼簡單......

  (二)、sqlserver2005的分頁查詢

    在sqlserver2005之前一直藉助top關鍵字來實現分頁查詢,不過效率低,在sqlserver2005及其之後的版本都使用row_number()解析函式來完成分頁查詢,效率有了很大的提高,不過sql語句比較複雜,下面給出分頁查詢的通式:

複製程式碼

/*

* firstIndex:起始索引


* pageSize:每頁顯示的數量

* orderColumn:排序的欄位名

* sql:可以是簡單的單表查詢語句,也可以是複雜的多表聯合查詢語句

*/

select top pageSize o.* from (select row_number() over(order by orderColumn) as rownumber,* from(sql) as o where rownumber>firstIndex;

複製程式碼

 下面看截圖,每頁顯示20條記錄數:

                                                             查詢(1-20)這20條記錄

                                                         查詢(21-40)這20條記錄

    知道了sqlserver中的row_number函式,分頁也就簡單了.....

  (三)、oracle分頁查詢

    接下來重點說說oracle的分頁查詢,oracle的分頁查詢方法相對來說要多點,ROWNUM、row_number(),今天主要將兩種效率稍好的分頁查詢語句。

    ①ROWNUM查詢分頁通式:

複製程式碼

/*

* firstIndex:起始索引

* pageSize:每頁顯示的數量

* sql:可以是簡單的單表查詢語句,也可以是複雜的多表聯合查詢語句

*/
select * from(select a.*,ROWNUM rn from(sql) a where ROWNUM<=(firstIndex+pageSize)) where rn>firstIndex

複製程式碼

以下截圖是以這種方式進行的查詢語句:

                                                          查詢(1-21)這20條記錄*****(沒有ID=6的記錄,所以查詢到的最大ID為21)

                                                       查詢(22-41)這20條記錄*****(沒有ID=6的記錄,所以開始查詢到的ID為22,以及最大ID為41)

 

    ②row_number()解析函式分頁查詢通式:

複製程式碼

 /*

 * firstIndex:起始索引

 * pageSize:每頁顯示的數量

 * orderColumn:排序的欄位名

 * sql:可以是簡單的單表查詢語句,也可以是複雜的多表聯合查詢語句

 */
select * from(select * from(select t.*,row_number() over(order by orderColumn) as rownumber from(sql) t) p where p.rownumber>firstIndex) where rownum<=pageSize

複製程式碼

 以下截圖是使用row_number()方式的分頁查詢效果:

                                                          查詢(1-21)這20條記錄*****(沒有ID=6的記錄,所以查詢到的最大ID為21)

                                                    查詢(22-41)這20條記錄*****(沒有ID=6的記錄,所以開始查詢到的ID為22,以及最大ID為41)

      對於oracle的分頁查詢,特地選出這兩種實現方式是因為這兩者各有千秋

     首先, 我們知道在ROWNUM查詢的方式中,在第二層的sql語句中有個"where ROWNUM<firstIndex+pageSize",根據oracle的原則,第二層查詢語句會嵌入到最內層中進行查詢,也就是說,最開始執行的查詢語句類似於:select * from wyuse where rownum<(firstIndex+pageSize) order by id asc,從資料表中查詢出(firstIndex+pageSize)條記錄,所以如果這個值很小的話,效率會很好,如果對於大資料量的表單,這個值如果是上千,比如:select * from wyuse where rownum<(5000) order by id asc,這樣一開始會選出5000條記錄,效率自然會慢很多....

     不過,相對於ROWNUM,row_number()方式可能通過簡化可以少一層巢狀,不過貌似對於大數量的查詢,效率也高不到哪裡去.....不過,對於大數量如果為表建立索引再結合row_number()效果會很好(未測試)

 

下面是分頁的實體類

public class PageBean<T> {
	
	//當前頁
	private int currentPage;
	//當前頁顯示的條數
	private int currentCount;
	//總條數
	private int totalCount;
	//總頁數
	private int totalPage;
	//每頁顯示的資料
	private List<T> productList = new ArrayList<T>();
	
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getCurrentCount() {
		return currentCount;
	}
	public void setCurrentCount(int currentCount) {
		this.currentCount = currentCount;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List<T> getProductList() {
		return productList;
	}
	public void setProductList(List<T> productList) {
		this.productList = productList;
	}
}

業務層實現的程式碼,簡單的一個例子

//分頁操作
	public PageBean findPageBean(int currentPage,int currentCount) throws SQLException  {
		
		ProductDao dao = new ProductDao();
		
		//目的:就是想辦法封裝一個PageBean 並返回
		PageBean pageBean = new PageBean();
		//1、當前頁private int currentPage;
		pageBean.setCurrentPage(currentPage);
		//2、當前頁顯示的條數private int currentCount;
		pageBean.setCurrentCount(currentCount);
		//3、總條數private int totalCount;
		int totalCount = dao.getTotalCount();
		pageBean.setTotalCount(totalCount);
		//4、總頁數private int totalPage;
		/*
		 * 總條數		當前頁顯示的條數	總頁數
		 * 10		4				3
		 * 11		4				3
		 * 12		4				3
		 * 13		4				4
		 * 
		 * 公式:總頁數=Math.ceil(總條數/當前顯示的條數)
		 * 
		 */
		int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
		pageBean.setTotalPage(totalPage);
		//5、每頁顯示的資料private List<T> productList = new ArrayList<T>();
		/*
		 * 頁數與limit起始索引的關係
		 * 例如 每頁顯示4條
		 * 頁數		其實索引		每頁顯示條數
		 * 1		0			4
		 * 2		4			4
		 * 3		8			4
		 * 4		12			4
		 * 
		 * 索引index = (當前頁數-1)*每頁顯示的條數
		 * 
		 */
		int index = (currentPage-1)*currentCount;
		
		List<Product> productList = dao.findProductListForPageBean(index,currentCount);
		pageBean.setProductList(productList);
		
		return pageBean;
	}

前端程式碼,簡單例子(基於bootstrap做的)

<!--分頁 -->
	<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
		<ul class="pagination" style="text-align: center; margin-top: 10px;">
			<!-- 上一頁 -->
			<!-- 判斷當前頁是否是第一頁 -->
			<c:if test="${pageBean.currentPage==1 }">
				<li class="disabled">
					<a href="javascript:void(0);" aria-label="Previous">
						<span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			</c:if>
			<c:if test="${pageBean.currentPage!=1 }">
				<li>
					<a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage-1}" aria-label="Previous">
						<span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			</c:if>	
			
			
			
		
			<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
				<!-- 判斷當前頁 -->
				<c:if test="${pageBean.currentPage==page }">
					<li class="active"><a href="javascript:void(0);">${page}</a></li>
				</c:if>
				<c:if test="${pageBean.currentPage!=page }">
					<li><a href="${pageContext.request.contextPath }/productList?currentPage=${page}">${page}</a></li>
				</c:if>
			
			</c:forEach>
			
			<!-- 判斷當前頁是否是最後一頁 -->
			<c:if test="${pageBean.currentPage==pageBean.totalPage }">
				<li class="disabled">
					<a href="javascript:void(0);" aria-label="Next"> 
						<span aria-hidden="true">&raquo;</span>
					</a>
				</li>
			</c:if>
			<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
				<li>
					<a href="${pageContext.request.contextPath }/productList?currentPage=${pageBean.currentPage+1}" aria-label="Next"> 
						<span aria-hidden="true">&raquo;</span>
					</a>
				</li>
			</c:if>
		
		</ul>
	</div>
	<!-- 分頁結束 -->