JDBC分页

不知道大家做项目做到最后有什么感觉没有,其实大家做来做去就是做一个列表加上分页和多条件的查询(http://xdwangiflytek.iteye.com/blog/1358261),只是不同的项目业务流程不一样而已,所以今天我想说说这里的分页。

1、 大家需要了解的是为什么我们需要分页?

因为当数据量太大时,会影响查询和传输的性能,并且我们从用户角度来考虑的话,如果让用户一次性看到成千上万条记录那用户也会疯掉的。

2、 对我们来说有哪些可实现的分页技术?

a、 存储过程分页,即在数据库中创建一个存储过程,传入SQL和页码获取当前页的记录,这个需要大家对存储过程有比较好的认识(我这块不行),当然这个从性能上来说是最好的,但是不能跨数据库平台。

b、 使用数据库专有SQL语句进行分页(Oracle的rownum、MSSQL的top、MySql的limit等),性能也很好,但是还是不能跨数据库(其实真实项目中没那么多项目要求都跨数据库)。

c、 JDBC分页,通过Statement的statement.setMaxRow(endIndex)和resultSet.absoulte(beginIndex)取得当前页范围内的记录。此种方式的性能依赖于厂商对JDBC规范的实现。这种方式的通用性是最好的,性能也不错,完全与数据库平台无关了。

d、 根据数据库类型自动生成数据库专有特性的sql语句,其实说白了也就是Hibernate的实现方式,这个自己也写过一个,其实就是根据数据库类型生成不同的数据库SQL专有语句而已。

 

下面我们需要写一个分页工具类,在写之前我们需要弄明白分页的原理。为了能够取得指定页码所对应的记录,我们是不是需要两个关键的参数:总记录数和每页的记录数;

每页的记录数我们可以设置一个默认值,10、15、20、25都无所谓,根据实际需求。

总记录数就没办法了,需要额外从数据库中利用Count函数取了,通过这两个参数我们是不是可以计算出总页数。同时我们也可以判断用户传过来的页码是否有效(小于第一页OR超出最后一页),然后我们再根据页码和每页记录数是不是就可以计算出当前页的的开始条数和终止条数了。

 

下面我们就需要来看看分页的工具类了

Java代码  
  1. package com.iflytek.page;  
  2.   
  3. /** 
  4.  * 分页工具类 
  5.  *  
  6.  * @author xudongwang 2012-1-19 
  7.  *  
  8.  *         Email:xdwangiflytek@gmail.com 
  9.  */  
  10. public class Page {  
  11.   
  12.     /** 
  13.      * 总记录数 
  14.      */  
  15.     private int totalRow;  
  16.   
  17.     /** 
  18.      * 每页记录数 
  19.      */  
  20.     private int pageSize = 10;  
  21.   
  22.     /** 
  23.      * 当前页码 
  24.      */  
  25.     private int currentCount;  
  26.   
  27.     /** 
  28.      * 总页数 
  29.      */  
  30.     private int total;  
  31.   
  32.     /** 
  33.      * 起始记录下标 
  34.      */  
  35.     private int beginIndex;  
  36.   
  37.     /** 
  38.      * 截止记录下标 
  39.      */  
  40.     private int endIndex;  
  41.   
  42.     /** 
  43.      * 构造方法,使用总记录数,当前页码 
  44.      *  
  45.      * @param totalRow 
  46.      *            总记录数 
  47.      * @param currentCount 
  48.      *            当前页面,从1开始 
  49.      */  
  50.     public Page(int totalRow, int currentCount) {  
  51.         this.totalRow = totalRow;  
  52.         this.currentCount = currentCount;  
  53.         calculate();  
  54.     }  
  55.   
  56.     /** 
  57.      * 构造方法 ,利用总记录数,当前页面 
  58.      *  
  59.      * @param totalRow 
  60.      *            总记录数 
  61.      * @param currentCount 
  62.      *            当前页面 
  63.      * @param pageSize 
  64.      *            默认10条 
  65.      */  
  66.     public Page(int totalRow, int currentCount, int pageSize) {  
  67.         this.totalRow = totalRow;  
  68.         this.currentCount = currentCount;  
  69.         this.pageSize = pageSize;  
  70.         calculate();  
  71.     }  
  72.   
  73.     private void calculate() {  
  74.         total = totalRow / pageSize + ((totalRow % pageSize) > 0 ? 1 : 0);  
  75.   
  76.         if (currentCount > total) {  
  77.             currentCount = total;  
  78.         } else if (currentCount < 1) {  
  79.             currentCount = 1;  
  80.         }  
  81.   
  82.         beginIndex = (currentCount - 1) * pageSize;  
  83.         endIndex = beginIndex + pageSize;  
  84.         if (endIndex > totalRow) {  
  85.             endIndex = totalRow;  
  86.         }  
  87.     }  
  88.   
  89.     public int getTotalRow() {  
  90.         return totalRow;  
  91.     }  
  92.   
  93.     public int getPageSize() {  
  94.         return pageSize;  
  95.     }  
  96.   
  97.     public int getCurrentCount() {  
  98.         return currentCount;  
  99.     }  
  100.   
  101.     public int getTotal() {  
  102.         return total;  
  103.     }  
  104.   
  105.     public int getBeginIndex() {  
  106.         return beginIndex;  
  107.     }  
  108.   
  109.     public int getEndIndex() {  
  110.         return endIndex;  
  111.     }  
  112.   
  113. }  

 继续

在后台获取前台传进来的页码 //从页面取得页码

Java代码  
  1. int currentPage = 1;    
  2. try {    
  3.     currentPage = Integer.parseInt(request.getParameter("currentPage"));    
  4. } catch (Exception ex) {}    
  5.     
  6. //取得总记录数,创建Page对象    
  7. int totalRow = studentDao.getAllStudents();//通过select count 取得总记录数    
  8. Page page = new Page(totalRow, currentPage);    
  9. studentDao.list(page);    

 

数据访问层,StduentDao.java

Java代码  
  1. public List<Stduent> getStudentsByPage(Page page) {    
  2.         List<Stduent> students = new ArrayList<Stduent>();    
  3.         try {    
  4.             String sql = "SELECT id,name,email FROM tbl_stduent";    
  5.             Connection conn = null;    
  6.             try {    
  7.                 conn = DbUtil.getConnection();    
  8.                 Statement statement = conn.createStatement();    
  9.                 statement.setMaxRows(page.getEndIndex());//关键代码,设置最大记录数为当前页记录的截止下标    
  10.                 ResultSet resultSet = statement.executeQuery(sql);    
  11.                 if (page.getBeginIndex() > 0) {    
  12.                     resultSet.absolute(page.getBeginIndex());//关键代码,直接移动游标为当前页起始记录处    
  13.                 }    
  14.                 while (resultSet.next()) {    
  15.                     Stduent student = new Student();    
  16.                     ……    
  17.                     students.add(student);    
  18.                 }    
  19.                 resultSet.close();    
  20.                 statement.close();    
  21.             } finally {    
  22.                 if (conn != null) {    
  23.                     conn.close();    
  24.                 }    
  25.             }    
  26.         } catch (SQLException e) {    
  27.             e.printStackTrace();    
  28.         }    
  29.         return students;    
  30.     }    

 其实仔细想想JDBC分页的性能与页码有关即与statement.setMaxRows有效,越往后翻,性能越差,因为越往后一次性查询的记录数就多,但是我们从用户的角度来看不会有用户会牛逼的一页一页翻到第n页去,一般都是根据条件来缩小查询范围。所以折中的办法就是将记录数设大一点,另外就是限制用户翻页的范围,其实这些性能的前提都是在数据量非常大的情况下而言的,一般数据量少的话,基本上都可以忽略不计的。

posted @ 2017-10-25 22:05  有梦就能实现  阅读(459)  评论(0编辑  收藏  举报