Mysql学习总结(32)——MySQL分页技术详解

1.什么是数据分页:数据分页就是将很多条记录像书本一样分页,每页显示多少行记录;

2.为什么要数据分页:当我们进行sql语句查询时,假如数据有成千上万行记录,如果在同一个页面去显示,那这个页面得有多大,数据就要很多,而我们所需的记录又很少,不使用分页,查看起来那么繁琐,而且一不小心容易看着眼花。使用数据分页,就行书本一样,有页数,一目了然。相当简洁。

3.核心sql语句:SELECT * FROM stud LIMIT m,n ————m表示要显示的页数,n表示显示的记录行数

4.核心思想:

  • 总行数(rows): select count(1) from stud;
  • 每页显示的行数(PAGE_SIZE): 固定值---已知的一个常量
  • 页数: pageSize= num/n + (num%n==0)?0:1 
  • 当前页号: currentPage
  • 当前要显示的页面数据的起始行号和终止行号 :startRow: (currentPage-1)*pageSize
  • 如何显示从startN开始的pageSize条记录  select * from stud limit startN, pageSize;
  • 当前显示的开始页号:showStart=currentPage-showSize/2;
  • 当前显示的结束页号:showEnd=showStart+showSize-1;
  • 模糊查询:select count(*) from stud where 1=1 and........

5.成果图:


6.代码实现

需要的包和配置文件:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. myConUtil.jar----自己写的c3p0pool工具类  
  2. commons-dbutils-1.4.jar  
  3. mysql-connector-java-5.1.34-bin.jar  
  4. c3p0-0.9.1.2.jar  
  5. c3p0-config.xml  

index.jsp

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.   </head>  
  7.   <body>  
  8.     <a href='<c:url value="/PageServlet"></c:url>'>查看分页技术</a>  
  9.   </body>  
  10. </html>  

show,jsp

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5. <head>  
  6. <title>演示数据分页</title>  
  7. <link rel="stylesheet" href='<c:url value="/css/table.css"></c:url>'  
  8.     media="screen">  
  9.     <script type="text/javascript">  
  10.         function onsub(obj){  
  11.             window.location.href="<c:url value='/PageServlet?page='></c:url>"+obj.value;  
  12.         }  
  13.     </script>  
  14. </head>  
  15.   
  16. <body>  
  17.     <h3>以下是当前页的内容</h3>  
  18.       
  19.     <form action="<c:url value='/PageServlet'/>" method="post" >  
  20.         请输入要查询的关键字:<br/>  
  21.         学号:<input type="text" name="serachId" value="${stud.id }"><br/>  
  22.         姓名:<input type="text" name="serachName" value="${stud.name }"><br/>  
  23.         <input type="submit" value="搜索">  
  24.     </form>  
  25.     <table>  
  26.         <c:if test="${!empty map.datas}">  
  27.             <tr>  
  28.                 <th>学号</th>  
  29.                 <th>姓名</th>  
  30.             </tr>  
  31.         </c:if>  
  32.         <c:forEach items="${map.datas}" var="stud">  
  33.             <tr>  
  34.                 <td>${stud.id }</td>  
  35.                 <td>${stud.name }</td>  
  36.             </tr>  
  37.         </c:forEach>  
  38.   
  39.   
  40.     </table>  
  41.     <c:if test="${map.currentPage!=1}" var="boo">  
  42.         <a href="<c:url value='/PageServlet?page=${map.currentPage-1}'></c:url>" >上一页</a>  
  43.                  
  44.     </c:if>  
  45.     <c:forEach var="idx" begin="${map.showStart }" end="${map.showEnd }">  
  46.         <c:if test="${map.currentPage==idx}" var="boo">  
  47.             <font face="STCAIYUN"><a  
  48.                 href="<c:url value='/PageServlet?page=${idx}'></c:url>">${idx}</a>  
  49.             </font>  
  50.                  
  51.         </c:if>  
  52.         <c:if test="${!boo}">  
  53.             <a href="<c:url value='/PageServlet?page=${idx}'></c:url>">${idx}</a>  
  54.                  
  55.         </c:if>  
  56.     </c:forEach>  
  57.     <c:if test="${map.currentPage!=map.pageCount}" var="boo">  
  58.         <a href="<c:url value='/PageServlet?page=${map.currentPage+1}'></c:url>">下一页</a>  
  59.                  
  60.     </c:if>  
  61.     <br/>  
  62.     <br/>  
  63.     <br/>  
  64.     <select onchange="onsub(this)">  
  65.         <c:forEach var="i" begin="1" end="${map.pageCount }">  
  66.             <option <c:if test="${i==map.currentPage }" >selected="selected" </c:if>  value="${i}" >  
  67.                     <a href="<c:url value='/PageServlet?page=${i}'></c:url>">第 ${i } 页</a>  
  68.             </option>  
  69.         </c:forEach>  
  70.     </select>  
  71. </body>  
  72. </html></span>  

table.css

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;">table{  
  2.     color: green;  
  3.     border: 1px solid blue;  
  4.     border-collapse: collapse;  
  5.     width:500px;  
  6.     margin: auto;  
  7. }  
  8. td{  
  9.     border: 1px solid blue;  
  10. }  
  11. th{  
  12.     border: 1px solid blue;  
  13. }  
  14. body{  
  15.     text-align: center;  
  16. }</span>  

PageServlet.Java

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;">package cn.hncu.page1.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.SQLException;  
  5. import java.util.Map;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import cn.hncu.page1.domain.Stud;  
  13. import cn.hncu.page1.service.IPageService;  
  14. import cn.hncu.page1.service.PageService;  
  15.   
  16. public class PageServlet extends HttpServlet {  
  17.     private IPageService service=new PageService();  
  18.   
  19.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  20.             throws ServletException, IOException {  
  21.   
  22.         doPost(request, response);  
  23.     }  
  24.   
  25.   
  26.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  27.             throws ServletException, IOException {  
  28.         request.setCharacterEncoding("utf-8");  
  29.         //这里是搜索区域的操作  
  30.         Stud stud=null;  
  31.         if(request.getMethod().equals("POST")){  
  32.             if (stud==null) {  
  33.                 stud = new Stud();  
  34.             }  
  35.             String serachId = request.getParameter("serachId");  
  36.             String serachName = request.getParameter("serachName");  
  37.             stud.setId(serachId);  
  38.             stud.setName(serachName);  
  39.             request.getSession().setAttribute("stud", stud);  
  40.         }else{  
  41.             stud=(Stud) request.getSession().getAttribute("stud");  
  42.             if (stud==null) {  
  43.                 stud = new Stud();  
  44.             }  
  45.         }  
  46.           
  47.           
  48.           
  49.         //封装studs对象  
  50.         int currentPage=1;  
  51.         try {  
  52.             currentPage = Integer.parseInt(request.getParameter("page"));  
  53.         } catch (NumberFormatException e) {  
  54.             currentPage=1;  
  55.         }  
  56.         Map<String, Object> map=null;  
  57.         try {  
  58.             map=service.query(currentPage,stud);  
  59.         } catch (SQLException e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         map.put("currentPage", currentPage);  
  63.         //显示滚动页号  
  64.         int showStart=0;//从第几个页号开始显示  
  65.         int showEnd=0;//从第几个页号结束显示  
  66.         int showSize=10;//显示多少页数  
  67.         int pageCount=Integer.parseInt(""+map.get("pageCount"));  
  68.         if(showSize>pageCount){//显示页数大于于总页数  
  69.             showStart=1;  
  70.             showEnd=pageCount;  
  71.         }else{  
  72.             if(currentPage<=showSize/2){  
  73.                 showStart=1;  
  74.                 showEnd=showSize;  
  75.             }else{  
  76.                 showStart=currentPage-showSize/2;  
  77.                 showEnd=showStart+showSize-1;  
  78.             }  
  79.         }  
  80.         if(showEnd>pageCount){  
  81.             showEnd=pageCount;  
  82.             showStart=showEnd-showSize;  
  83.         }  
  84.         map.put("showStart", showStart);  
  85.         map.put("showEnd", showEnd);  
  86.   
  87.         request.setAttribute("map", map);  
  88.         request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);  
  89.     }  
  90.   
  91. }</span>  

IPageService.java

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;">package cn.hncu.page1.service;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.Map;  
  5.   
  6. import cn.hncu.page1.domain.Stud;  
  7.   
  8. public interface IPageService {  
  9.     public Map<String, Object> query(int currentPage, Stud stud) throws SQLException;  
  10.   
  11. }</span>  
  12.    
PageService.java

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;"> package cn.hncu.page1.service;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.Map;  
  5.   
  6. import cn.hncu.page1.dao.PageDao;  
  7. import cn.hncu.page1.dao.PageDaoJdbc;  
  8. import cn.hncu.page1.domain.Stud;  
  9.   
  10. public class PageService implements IPageService{  
  11.     private PageDao dao=new PageDaoJdbc();  
  12.     @Override  
  13.     public Map<String, Object> query(int currentPage, Stud stud)  
  14.             throws SQLException {  
  15.         return dao.query(currentPage,stud);  
  16.     }  
  17.       
  18. }</span>  
PageDao.java

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;">package cn.hncu.page1.dao;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.Map;  
  5.   
  6. import cn.hncu.page1.domain.Stud;  
  7.   
  8. public interface PageDao {  
  9.     public Map<String, Object> query(int currentPage, Stud stud) throws SQLException;  
  10.       
  11. }</span>  
PageDaoJdbc.java

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;">package cn.hncu.page1.dao;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import javax.sql.DataSource;  
  9.   
  10. import org.apache.commons.dbutils.QueryRunner;  
  11. import org.apache.commons.dbutils.handlers.MapListHandler;  
  12. import org.apache.commons.dbutils.handlers.ScalarHandler;  
  13. import org.junit.Test;  
  14.   
  15. import cn.hncu.page1.domain.Stud;  
  16. import cn.hncu.page1.service.IPageService;  
  17. import cn.hncu.pool.C3p0Pool;  
  18.   
  19. public class PageDaoJdbc implements PageDao{  
  20.     private static final int PAGE_SIZE=10;  
  21.     @Override  
  22.     public Map<String, Object> query(int currentPage, Stud stud) throws SQLException {  
  23.         Map<String, Object> map=new HashMap<String, Object>();  
  24.         DataSource pool=C3p0Pool.getPool();  
  25.         QueryRunner qr=new QueryRunner(pool);  
  26.         String sql="select count(*) from stud where 1=1 ";  
  27.         if(stud.getId()!=null&&stud.getId().trim().length()>0){  
  28.             sql+="and id like '%"+stud.getId()+"%'";  
  29.         }  
  30.         if(stud.getName()!=null&&stud.getName().trim().length()>0){  
  31.             sql+="and name like '%"+stud.getName()+"%'";  
  32.         }  
  33.           
  34.         int rows=Integer.parseInt(""+ qr.query(sql, new ScalarHandler()));  
  35.         int pageCount=rows/PAGE_SIZE+((rows%PAGE_SIZE==0)?0:1);  
  36.         map.put("pageCount", pageCount);  
  37.           
  38.         int startRow=(currentPage-1)*PAGE_SIZE;  
  39.         map.put("startRow", startRow);  
  40.           
  41.         String sql2="select * from stud where 1=1  ";//这种判断方法,很不错  
  42.         if(stud.getId()!=null&&stud.getId().trim().length()>0){  
  43.             sql2+="and id like '%"+stud.getId()+"%'";  
  44.         }  
  45.         if(stud.getName()!=null&&stud.getName().trim().length()>0){  
  46.             sql2+="and name like '%"+stud.getName()+"%' ";  
  47.         }  
  48.         sql2+="limit "+startRow+" , "+PAGE_SIZE;  
  49.         List<Map<String, Object>> datas=qr.query(sql2, new MapListHandler());  
  50.         map.put("datas", datas);  
  51.         return map;  
  52.     }  
  53.       
  54.       
  55. }</span>  

Stud.java

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:12px;">package cn.hncu.page1.domain;  
  2.   
  3. public class Stud {  
  4.     private String id;  
  5.     private String name;  
  6.     public String getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(String id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     @Override  
  19.     public String toString() {  
  20.         return "Stud [id=" + id + "name=" + name + "]";  
  21.     }  
  22.       
  23. }  
posted @ 2016-08-19 09:33  一杯甜酒  阅读(580)  评论(0编辑  收藏  举报