真分页查询

前段时间在javaeye上看到一位朋友 http://qxmcool.iteye.com/blog/333024  写的分页,感觉蛮好的就拿过来学习了,我在事例数据库中存有W条数据,结果玩完了,没想到这个哥们和我开了个玩笑,这原来是个假分页,所谓假分页就是他讲所有的数据放在容器中,每次都得将表中的数据全都查询出来,结果可想而知。在他原有的基础上我将其代码加以改进。以达到真分页的效果。

 

package com.pms.util;

import java.util.List;

/**
 * Pagination.java
 * utils class
 * @author fanfq 2009-6-7
 * 
 * */
public class Pagination<T> {//这里我使用的范型
	
	private int currentPage = 1; // 当前页数
	private int pageCount = 20; // 每页数据的条数
	private int pageSize; // 总页数
	private int valueCount; // 总数据的条数
	private List<T> pageList;// 分页集合
	private int previousPageCount;// 上一页的页数
	private int nextPagecount; // 下一页的页数
	
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		// 上一页
		previousPageCount = currentPage - 1;
		// 下一页
		nextPagecount = currentPage + 1;
	}
	
	public void setPageList(List<T> pageList) {
		this.pageList = pageList;
		pageSize = valueCount % pageCount == 0 ? valueCount / pageCount
				: valueCount / pageCount + 1;
	}	
	
	public int getCurrentPage() {
		return currentPage;
	}
	
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getValueCount() {
		return valueCount;
	}
	public void setValueCount(int valueCount) {
		this.valueCount = valueCount;
	}
	public List<T> getPageList() {
		return pageList;
	}
	
	public int getPreviousPageCount() {
		return previousPageCount;
	}
	public void setPreviousPageCount(int previousPageCount) {
		this.previousPageCount = previousPageCount;
	}
	public int getNextPagecount() {
		return nextPagecount;
	}
	public void setNextPagecount(int nextPagecount) {
		this.nextPagecount = nextPagecount;
	}
	
	

}

 

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				
		
		//分页查询操作
		String currentpage = request.getParameter("pageindex"); 
		if(currentpage == null){ 
			currentpage = "1"; 
		} 
		int pageindex = Integer.parseInt(currentpage); 
		Pagination<Dept> pc =  new Pagination<Dept>(); //这里我使用的范型
		int count = pc.getPageCount();
		int cursor = count * (pageindex-1);
		pc.setValueCount(new DeptDao().getDeptCount());
		List<Dept> allList = new DeptDao().getAllDeptByPagenation(cursor,count);//关键之处定位查询
		pc.setValueCount(new DeptDao().getDeptCount());
		pc.setPageList(allList); 
		pc.setCurrentPage(pageindex); 
		request.setAttribute("pc", pc);
		this.getServletContext().getRequestDispatcher("/page/xx_list.jsp").forward(request, response); 
		
	}

 

	/**定位查询*/
	public List<Dept> getAllDeptByPagenation(int cursor,int rows) {
		String sql = "SELECT * FROM dept limit " + cursor + "," + rows;
		ResultSet rs = DBPool.exeQuery(sql);
		List<Dept> list = new ArrayList<Dept>();
		try {
			while (rs.next()) {
				Dept fDept = new Dept();
				fDept.setDeptid(rs.getInt(1));
				fDept.setDeptname(rs.getString(2));
				fDept.setDeptbesc(rs.getString(3));
				fDept.setDeptmanager(rs.getInt(4));
				list.add(fDept);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBPool.closeConnection();
		return list;
	}
	
	/**获得部门数*/
	public int getDeptCount(){
		String sql = "select count(*) from dept";
		ResultSet rs = DBPool.exeQuery(sql);
		int count = 0 ;
		try {
			if(rs.next()){
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBPool.closeConnection();
		return count;
	}

   

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  <body>
    <div class="title">
	<h1>所有部门信息</h1>
	<table><tr><td>
		相关操作:
	</td></tr></table>
	</div>
	
<table>
  <thead>
    <th width="10%">部门编号</th>
    <th width="20%">部门名称</th>
    <th width="20%">部门概述</th>
    <th width="20%">部门经理</th>
    <th width="10%">操作</th>
  </thead>
  
  <c:forEach var="pc" items="${pc.pageList}">

	
	
	<tr class="a1" align="center" οnmοusemοve="color=this.style.backgroundColor;this.style.backgroundColor='rgb(214,229,249)'" style="width: 529px" οnmοuseοut="this.style.backgroundColor='white'">	  	
	 <td>${pc.deptid}</td>
	 <td>${pc.deptname}</td>
	 <td>${pc.deptbesc}</td>
	 <td><a href="#" target="">${pc.deptmanager}</a></td>
	 <td><a target="" onClick="Myopen(User,${pc.deptid})">修改</a></td>
	</tr>
	
	  
  </c:forEach>

</table>

<div class="title">
	
	<table><tr align="right"><td>
		第${pc.currentPage}/${pc.pageSize}页&nbsp;&nbsp;
		<a href="DeptServlet?pageindex=1">首页</a> 
		<c:if test="${pc.previousPageCount > 0}" var="true"> 
		<a href="DeptServlet?pageindex=${pc.previousPageCount}">上一页</a> 
		</c:if> 
		<c:if test="${pc.nextPagecount <= pc.pageSize}" var="true"> 
		<a href="DeptServlet?pageindex=${pc.nextPagecount}">下一页</a> 
		</c:if> 
		<a href="DeptServlet?pageindex=${pc.pageSize}">尾页</a> 
	</td></tr></table>
	<h1>**fanfq.iteye.com**</h1>
</div>

</body>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值