Mysql 通用分页

第一步,建立数据库:

Java代码  收藏代码
  1. create table student(  
  2.     id int primary key auto_increment,  
  3.     code varchar(50),  
  4.     name varchar(50),  
  5.     sex varchar(10),  
  6.     age int,  
  7.     political varchar(50),  
  8.     origin varchar(50),  
  9.     professional varchar(50)  
  10. ) ;  
  11.   
  12. insert into student(code, name, sex, age, political, origin, professional)  
  13. values('200820459432''zhangsan''male'24'tuan_yuan','China''SoftWare') ;  
  14. insert into student(code, name, sex, age, political, origin, professional)  
  15. values('200820233442''lisi''femal'23'dang_yuan','China''Computer') ;  

第二步,建立javabean:

Java代码  收藏代码
  1. package com.page.domain ;  
  2.   
  3. public class Student {  
  4.   
  5.     private Integer id ;  
  6.       
  7.     private String code ;  
  8.       
  9.     private String name ;  
  10.       
  11.     private String sex ;  
  12.       
  13.     private Integer age ;  
  14.       
  15.     private String political ;  
  16.       
  17.     private String origin ;  
  18.       
  19.     private String professional ;  
  20.       
  21.     public String toString(){  
  22.         return "id : " + id + "; code : " + code + "; name : " + name + "; sex : " +sex+  
  23.                 "; age : " +age+ "; political : " +political+ "; origin : "  
  24.                 + origin +"; professional : " + professional;  
  25.     } ;  
  26.       
  27.     public int getId() {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(int id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     public String getCode() {  
  36.         return code;  
  37.     }  
  38.   
  39.     public void setCode(String code) {  
  40.         this.code = code;  
  41.     }  
  42.   
  43.     public String getName() {  
  44.         return name;  
  45.     }  
  46.   
  47.     public void setName(String name) {  
  48.         this.name = name;  
  49.     }  
  50.   
  51.     public String getSex() {  
  52.         return sex;  
  53.     }  
  54.   
  55.     public void setSex(String sex) {  
  56.         this.sex = sex;  
  57.     }  
  58.   
  59.     public int getAge() {  
  60.         return age;  
  61.     }  
  62.   
  63.     public void setAge(int age) {  
  64.         this.age = age;  
  65.     }  
  66.   
  67.     public String getPolitical() {  
  68.         return political;  
  69.     }  
  70.   
  71.     public void setPolitical(String political) {  
  72.         this.political = political;  
  73.     }  
  74.   
  75.     public String getOrigin() {  
  76.         return origin;  
  77.     }  
  78.   
  79.     public void setOrigin(String origin) {  
  80.         this.origin = origin;  
  81.     }  
  82.   
  83.     public String getProfessional() {  
  84.         return professional;  
  85.     }  
  86.   
  87.     public void setProfessional(String professional) {  
  88.         this.professional = professional;  
  89.     }  
  90.   
  91. }  

 第三步,写分页工具page.java和domainPage.java

Page.java代码  收藏代码
  1. package com.ext.util;  
  2.   
  3. import java.util.* ;  
  4. public class Page {  
  5.   
  6.     //结果集  
  7.     private List<?> list ;  
  8.       
  9.     //查询总记录数  
  10.     private int totalRecords ;  
  11.       
  12.     //每页多少条数据  
  13.     private int pageSize ;  
  14.       
  15.     //第几页  
  16.     private int pageNo ;  
  17.       
  18.     /**  
  19.      * 总页数  
  20.      * @return  
  21.      */  
  22.     public int getTotalPages(){  
  23.         return (totalRecords + pageSize -1) / pageSize ;  
  24.     }  
  25.       
  26.     /**  
  27.      * 取得首页  
  28.      * @return  
  29.      */  
  30.     public int getTopPageNo(){  
  31.         return 1 ;  
  32.     }  
  33.       
  34.     /**  
  35.      * 上一页  
  36.      * @return  
  37.      */  
  38.     public int getPreviousPageNo(){  
  39.         if(pageNo <= 1){  
  40.             return 1 ;  
  41.         }  
  42.         return pageNo - 1 ;  
  43.     }  
  44.       
  45.     /**  
  46.      * 下一页  
  47.      * @return  
  48.      */  
  49.     public int getNextPageNo(){  
  50.         if(pageNo >= getBottomPageNo()){  
  51.             return getBottomPageNo() ;  
  52.         }  
  53.         return pageNo + 1 ;  
  54.     }  
  55.       
  56.     /**  
  57.      * 取得尾页  
  58.      * @return  
  59.      */  
  60.     public int getBottomPageNo(){  
  61.         return getTotalPages() ;  
  62.     }  
  63.   
  64.     public List<?> getList() {  
  65.         return list;  
  66.     }  
  67.   
  68.     public void setList(List<?> list) {  
  69.         this.list = list;  
  70.     }  
  71.   
  72.     public int getTotalRecords() {  
  73.         return totalRecords;  
  74.     }  
  75.   
  76.     public void setTotalRecords(int totalRecords) {  
  77.         this.totalRecords = totalRecords;  
  78.     }  
  79.   
  80.     public int getPageSize() {  
  81.         return pageSize;  
  82.     }  
  83.   
  84.     public void setPageSize(int pageSize) {  
  85.         this.pageSize = pageSize;  
  86.     }  
  87.   
  88.     public int getPageNo() {  
  89.         return pageNo;  
  90.     }  
  91.   
  92.     public void setPageNo(int pageNo) {  
  93.         this.pageNo = pageNo;  
  94.     }  
  95. }  
 package com.ext.util;
Java代码  收藏代码
  1. import java.lang.reflect.Field;  
  2. import java.sql.Connection;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.ext.util.DBUtil;  
  9. import com.ext.util.Page;  
  10.   
  11. public class DomainPage {  
  12.       
  13.     private static Class<?> c ;  
  14.     private static String tableName ;  
  15.     private static Field[] field ;  
  16.     private static String[] attributes ;  
  17.       
  18.     private static void init(String domainClass) throws Exception{  
  19.         c = Class.forName(domainClass) ;  
  20.         tableName = c.getSimpleName() ;  
  21.         field = c.getDeclaredFields() ;  
  22.         attributes = new String[field.length] ;  
  23.         for(int i=0; i<field.length; i++){  
  24.             attributes[i] = field[i].getName() ;  
  25.         }  
  26.     }  
  27.   
  28.     public static Page getDomainPage(Connection conn, int pageNo, int pageSize, String domainClass, Object sort) throws Exception{  
  29.         Page page = null ;  
  30.         List list = null ;  
  31.         Object domainObj = null ;  
  32.         Object attributeObj = null ;  
  33.         String sql = null ;  
  34.         PreparedStatement pstmt = null ;  
  35.         ResultSet rs = null ;  
  36.           
  37.         init(domainClass) ;  
  38.         sql = getSql() ;  
  39.           
  40.           
  41.         pstmt = conn.prepareStatement(sql) ;  
  42.         pstmt.setObject(1, sort) ;  
  43.         pstmt.setInt(2, (pageNo-1)*pageSize) ;  
  44.         pstmt.setInt(3, pageNo*pageSize) ;  
  45.         rs = pstmt.executeQuery() ;  
  46.         list = new ArrayList() ;  
  47.         while(rs.next()){  
  48.             domainObj = c.newInstance() ;  
  49.             for(int i=0; i<field.length; i++){  
  50.                 field[i].getClass() ;  
  51.                 attributeObj = rs.getObject(field[i].getName()) ;  
  52.                 field[i].setAccessible(true) ;  
  53.                 field[i].set(domainObj, attributeObj) ;  
  54.             }  
  55.             list.add(domainObj) ;  
  56.         }  
  57.         page = new Page() ;  
  58.         page.setList(list) ;  
  59.         page.setTotalRecords(getTotalRecords(conn,tableName)) ;  
  60.         page.setPageNo(pageNo) ;  
  61.         page.setPageSize(pageSize) ;  
  62.       
  63.           
  64.         return page ;  
  65.     }  
  66.       
  67.     private static String getSql(){  
  68.         StringBuffer sbSql = new StringBuffer("select ") ;  
  69.         for(int i=0; i<field.length; i++){  
  70.             sbSql.append(attributes[i]) ;  
  71.             if(i<field.length-1){  
  72.                 sbSql.append(", ") ;  
  73.             }  
  74.         }  
  75.         sbSql.append(" from ")   
  76.              .append(tableName)   
  77.              .append(" order by ? limit ?,?") ;  
  78.         return sbSql.toString() ;  
  79.     }  
  80.       
  81.     private static int getTotalRecords(Connection conn, String tableName) throws Exception{  
  82.         String sql = "select count(*) from " +  tableName;  
  83.         PreparedStatement pstmt = null ;  
  84.         ResultSet rs = null ;  
  85.         int count = 0 ;  
  86.         try{  
  87.             pstmt = conn.prepareStatement(sql) ;  
  88.             rs = pstmt.executeQuery() ;  
  89.             rs.next() ;  
  90.             count = rs.getInt(1) ;  
  91.         }finally{  
  92.             DBUtil.close(rs) ;  
  93.             DBUtil.close(pstmt) ;  
  94.         }  
  95.         return count ;  
  96.     }  
  97. }  

 第四步:写测试代码:

Java代码  收藏代码
  1. package com.domain.manager;  
  2.   
  3. import java.sql.Connection;  
  4. import java.util.Iterator;  
  5.   
  6. import com.ext.util.DBUtil;  
  7. import com.ext.util.DomainPage;  
  8. import com.ext.util.Page;  
  9. import com.page.domain.Student;  
  10.   
  11. public class StudentManager {  
  12.   
  13.     public static void main(String[] args) throws Exception {  
  14.     Student stu = new Student() ;  
  15.     Connection conn = DBUtil.getConnection() ;  
  16.     Page page = DomainPage.getDomainPage(conn, 110, Student.class.getName(), "id") ;  
  17.     for(Iterator<?> iter = page.getList().iterator(); iter.hasNext();){  
  18.         stu = (Student) iter.next() ;  
  19.         System.out.println(stu) ;  
  20.     }  
  21. }  
  22.   
  23. }  

 第五步,就可以看到运行结果了:

Java代码  收藏代码
  1. id : 1;  
  2. code : 200820459432;  
  3. name : zhangsan;  
  4. sex : male;  
  5. age : 24;  
  6. political : tuan_yuan;  
  7. origin : China;  
  8. professional : SoftWare  
  9.   
  10. id : 2;  
  11. code : 200820233442;  
  12. name : lisi;  
  13. sex : femal;  
  14. age : 23;  
  15. political : dang_yuan;  
  16. origin : China;  
  17. professional : Computer  
原文地址:https://www.cnblogs.com/daichangya/p/12959057.html