oracle,mysql,sqlserver分页查询,附实体类

2 篇文章 0 订阅
1 篇文章 0 订阅

 最近简单的对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>
	<!-- 分页结束 -->

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值