分页,是一种将所有数据分段展示给用户的技术.用户每次看到的不 是全部数据,而是其中的一部分,如果在其中没有找到自己想要的内容,用户可以通过指定页码或是点上/下一页的方式进行翻页。
本例演示静态分页,也就是先设置好每页显示10行,再根据总行数,来算出总页数,将所有页数的页号都显示出来。
相关算法(技术):
总行数(num): select count(1) from stud; 每页显示的行数(n): 固定值---已知的一个常量 页数: pageSize= num/n +( (num%n==0)?0:1 ) 当前页号: currentPage 当前要显示的页面数据的起始行号和终止行号 startN: (currentPage-1)*pageSize 如何显示从startN开始的pageSize条记录 select * from stud limit startN, pageSize;
像这样:
点击哪一页就显示哪一页的内容。
数据库数据:
数据库的表和数据在这一篇博客中已经准备好了:
http://blog.csdn.net/qq_26525215/article/details/52212571
create table person( id varchar(30) primary key, name varchar(30), address varchar(30), age int );
DAO层:
接口:
package cn.hncu.dao; import java.sql.SQLException; import java.util.Map; public interface IPageDAO { public Map<String, Object> query(Integer pageNo) throws NumberFormatException, SQLException; }
实现类:
package cn.hncu.dao; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import org.junit.Test; import cn.hncu.pubs.C3p0Pool; public class PageJdbc implements IPageDAO { //每页显示的行数 private final int pageSize = 10; @Override public Map<String, Object> query(Integer pageNo) throws NumberFormatException, SQLException { Map<String, Object> result = new HashMap<String, Object>(); //获取总页数 pageCount = rows/pageSize + ((rows%pageSize==0)?0:1) //总行数 rows String sql = "select count(1) from person"; QueryRunner run = new QueryRunner(C3p0Pool.getDataSource()); int rows =Integer.parseInt(""+run.query(sql, new ScalarHandler())); //总页数 int pageCount = rows/pageSize + ((rows%pageSize==0)?0:1); result.put("pageCount", pageCount); //分页后的当前页面内容datas //起始行号 int startN = (pageNo-1)*pageSize; sql = "select * from person limit "+startN+" , "+pageSize; List<Map<String, Object>> datas = run.query(sql, new MapListHandler()); result.put("datas", datas);//封装到result return result; } @Test public void test() { try { Map<String, Object> map = query(5); System.out.println(map); } catch (NumberFormatException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
C3p0配置文件c3p0-config.xml:
<c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl"> <![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]> </property> <property name="user">root</property> <property name="password">1234</property> <!-- 初始化池大小 --> <property name="initialPoolSize">2</property> <!-- 最大空闲时间 --> <property name="maxIdleTime">30</property> <!-- 最多有多少个连接 --> <property name="maxPoolSize">10</property> <!-- 最少几个连接 --> <property name="minPoolSize">2</property> <!-- 每次最多可以执行多少个批处理语句 --> <property name="maxStatements">50</property> </default-config> <!-- 命名的配置 --> <named-config name="demo"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl"><![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]></property> <property name="user">root</property> <property name="password">1234</property> <property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 --> <property name="initialPoolSize">100</property> <property name="minPoolSize">50</property> <property name="maxPoolSize">1000</property> <property name="maxStatements">0</property> <property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him --> </named-config> </c3p0-config>
C3p0数据库连接池:
package cn.hncu.pubs; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3p0Pool { private static DataSource pool; private static ThreadLocal<Connection> t = new ThreadLocal<Connection>(); static{ pool = new ComboPooledDataSource(); } public static DataSource getDataSource(){ return pool; } public static Connection getConnection() throws SQLException{ Connection con=t.get(); if(con==null){ con = pool.getConnection(); t.set(con); } return con; } }
service层:
接口:
package cn.hncu.service; import java.sql.SQLException; import java.util.Map; public interface IPageService { public Map<String, Object> query(Integer pageNo) throws NumberFormatException, SQLException; }
实现类
package cn.hncu.service; import java.sql.SQLException; import java.util.Map; import cn.hncu.dao.IPageDAO; import cn.hncu.dao.PageJdbc; public class PageServiceImpl implements IPageService{ //注入dao IPageDAO dao = new PageJdbc(); @Override public Map<String, Object> query(Integer pageNo) throws NumberFormatException, SQLException { return dao.query(pageNo); } }
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <c:redirect url="/PageServlet"></c:redirect> </body> </html>
show.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>演示数据分页显示</title> <link rel="stylesheet" href="<c:url value='/css/table.css' />" /> <script type="text/javascript"> function sub(obj){ window.location.href="<c:url value='/PageServlet?page=' />"+obj.value; } </script> </head> <body> <h3>当前页的内容:</h3> <table> <tr><th>学号</th><th>姓名</th></tr> <c:forEach items="${result.datas}" var="map"> <tr> <td>${map.id}</td> <td>${map.name}</td> </tr> </c:forEach> </table> <c:if test="${result.currentPage!=1}"> <span class="pc"> <a href='<c:url value="/PageServlet?page=${result.currentPage-1}"></c:url>'>上一页</a> </span> </c:if> <c:forEach begin="1" end="${result.pageCount}" var="idx"> <c:if test="${idx==result.currentPage}" var="isNow"> <span class=now>${idx}</span> </c:if> <c:if test="${!isNow }"> <span class="pc"> <a href='<c:url value="/PageServlet?page=${idx}"></c:url>'>${idx}</a> </span> </c:if> </c:forEach> <c:if test="${result.currentPage!=result.pageCount}"> <span class="pc"> <a href="<c:url value='/PageServlet?page=${result.currentPage+1}'></c:url>">下一页</a> </span> </c:if> <br/><br/> <!-- 复选框 --> <select onchange="sub(this)"> <c:forEach begin="1" end="${result.pageCount}" var="idx"> <option <c:if test="${idx==result.currentPage}">selected="selected"</c:if> value="${idx}" > 第${idx}页 </option> </c:forEach> </select> </body> </html>