为了利于良好的页面布局,使显示的效率更高,常常会用到分页技术,使多条数据按页码管理起来。
关键点:
分页的sql语句:SELECT * FROM admin LIMIT ?,?(两个占位符需要传入两个参数,1.查询的起始行数2.查询的行数)
前提准备:
这里的数据库使用的是mysql数据库,另外使用dbutils和c3p0连接池等组件,所有需要导入相应的文件
(1)引入jar文件以及配置文件
a.数据库驱动包
b.C3P0连接池jar文件 及 配置文件
c.dbutils组件的jar文件
(2)写一个通用的工具类jdbcUtils.java(此工具类主要是用来初始化连接池、获取dbutils的核心工具类对象)
public class jdbcUtils {
/**
* 初始化C3P0连接池
*/
private static ComboPooledDataSource ds;
static{
ds = new ComboPooledDataSource();
}
/**
* 创建dbutils核心工具类对象
*
*/
public static QueryRunner getQueryrunner(){
//创建QueryRunner对象时传入连接池(数据源对象)对象,如果已经传入了数据源对象,那么
//在使用QueryRunner对象的方法时就不需要传入连接对象(connection),会自动从数据源中获取连接。
QueryRunner qr = new QueryRunner(ds);
return qr;
}
}
到这里准备工作已经做好了,就开始进行相关的设计。
1.pageBean的设计(封装分页的参数)
/**
* 封装分页的参数
* @author Administrator
*
*/
public class pageBean<T> {
private int currentPage = 1; //当前页码,默认是第一页
private int totalPage; //总页数
private int pageCount = 4; //每页显示的行数
private int totalCount; //总记录数
private List<T> pageData; //返回分页查询的数据
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
//这里加入业务逻辑
if(totalCount%totalPage==0){
totalPage=totalCount/pageCount;
}else{
totalPage=totalCount/pageCount+1;
}
this.totalPage = totalPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<T> getPageData() {
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
}
2.数据访问对象 dao接口设计与实现
(1)接口设计
/**
* 数据访问层的接口设计
* @author Administrator
*
*/
public interface adminDao {
//显示分页查询的数据
public void findAll(pageBean<Admin> pb);
public int getTotalcount();
}
(2)接口实现
public class adminImps implements adminDao{
//显示分页查询的数据
public void findAll(pageBean<Admin> pb) {
// TODO Auto-generated method stub
/**
* 1. 获取当前页面的页数、分页查询的起始条数和每页显示的行数
*/
//获取当前页面的页数
int currentPage = pb.getCurrentPage();
//获得每页显示的信息行数
int pageCount = pb.getPageCount();
//获取分页查询的起始条数
int start = (currentPage-1)*pageCount;
/**
* 2.获取所有的记录数
*
*/
int totalCount = this.getTotalcount();
pb.setTotalCount(totalCount);
/**
* 3.分页查询数据,将查询到的数据封装到pageBean对象中。
*
*/
String sql = "SELECT * FROM admin LIMIT ?,?";
//创建DbUtils核心工具类对象
QueryRunner qr = jdbcUtils.getQueryrunner();
try {
//根据当前页页码,查询当前页数据
List<Admin> list = qr.query(sql, new BeanListHandler<Admin>(Admin.class), start,pageCount);
//将查询的当前页数据封装到pageBean对象中
pb.setPageData(list);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
//获取所有的记录数
public int getTotalcount() {
// TODO Auto-generated method stub
String sql = "SELECT COUNT(*) FROM admin";
//创建DbUtils核心工具类对象
QueryRunner qr = jdbcUtils.getQueryrunner();
//ScalarHandler 查询返回结果记录的第一行的第一列,获取总记录数
try {
Long count = qr.query(sql, new ScalarHandler<Long>());
return count.intValue();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
3.service业务逻辑层的设计和servlet控制层的设计
将业务逻辑从servlet控制层里面分离出来利于代码的扩展性也易于管理。
(1)service层功能的实现
public class adminserviceImps implements adminService{
private adminDao ad = new adminImps();
public void findAll(pageBean<Admin> pb) {
// TODO Auto-generated method stub
try {
ad.findAll(pb);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
(2)servlet层的设计(获取pageBean所需的数据,封装到pageBean中,通过域对象传到jsp页面进行展示)
/**
* 控制层设计
*
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Admin;
import service.adminService;
import service.imps.adminserviceImps;
import utils.pageBean;
public class indexServlet extends HttpServlet {
//创建service实例
private adminService as = new adminserviceImps();
String uri = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//获取当前页页码
String curPage = request.getParameter("currentPage");
if(curPage==null||"".equals(curPage.trim())){
curPage = "1";
}
//把string转换成int
int currentPage = Integer.parseInt(curPage);
//将获取的当前页数据封装到pageBean中
pageBean<Admin> pb = new pageBean<Admin>();
pb.setCurrentPage(currentPage);
as.findAll(pb);
request.setAttribute("pageBean", pb);
//跳转到jsp页面
uri = "/index.jsp";
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace(); //测试用的
uri = "/error.jsp";
//跳转到错误页面,给用户看
//request.getRequestDispatcher(uri).forward(request, response);
}
request.getRequestDispatcher(uri).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
4.jsp(视图层)
<html>
<head>
<title>分页查询</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table border ="1" width = "80%" align="center" cellpadding="5" cellspacing="1">
<tr>
<td>编号id</td>
<td>用户名</td>
<td>密码</td>
</tr>
<!-- 迭代数据 -->
<c:choose>
<c:when test="${not empty requestScope.pageBean.pageData}">
<c:forEach var="adm" items="${requestScope.pageBean.pageData}" varStatus="vs">
<tr>
<td>${vs.count }</td>
<td>${adm.userName }</td>
<td>${adm.pwd }</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="3" align="center">没有要找的数据</td>
</tr>
</c:otherwise>
</c:choose>
<tr >
<td colspan="3" align="center">
当前页码${pageScope.pageBean.currentPage }/${pageScope.pageBean.totalPage} 页
<a href = "${pageContext.request.contextPath }/indexServlet?currentPage=1">首页</a>
<a href = "${pageContext.request.contextPath }/indexServlet?currentPage=${pageScope.pageBean.currentPage-1}">上一页</a>
<a href = "${pageContext.request.contextPath }/indexServlet?currentPage=${pageScope.pageBean.currentPage+1}">下一页</a>
<a href = "${pageContext.request.contextPath }/indexServlet?currentPage=${pageScope.pageBean.totalCount}">末页</a>
</td>
</tr>
</table>
</body>
</html>