SQL分页

1、oracle的分页

PageMode类:

 
 
  1. public class PageModel { 
  2.      
  3.     private List list; 
  4.      
  5.     private int totalRecords; 
  6.      
  7.     private int pageSize; 
  8.      
  9.     private int pageNo; 
  10.      
  11.     /** 
  12.      * 总页数 
  13.      * @return 
  14.      */ 
  15.     public int getTotalPages() { 
  16.         return (this.totalRecords + this.pageSize - 1)/this.pageSize; 
  17.     } 
  18.      
  19.     /** 
  20.      * 取得首页 
  21.      * @return 
  22.      */ 
  23.     public int getTopPageNo() { 
  24.         return 1
  25.     } 
  26.      
  27.     /** 
  28.      * 上一页 
  29.      * @return 
  30.      */ 
  31.     public int getPreviousPageNo() { 
  32.         if (this.pageNo <= 1){ 
  33.             return 1
  34.         } 
  35.         return this.pageNo - 1
  36.     } 
  37.      
  38.     /** 
  39.      * 下一页 
  40.      * @return 
  41.      */ 
  42.     public int getNextPageNo() { 
  43.         if (this.pageNo >= this.getBottomPageNo()) { 
  44.             return this.getBottomPageNo(); 
  45.         } 
  46.         return this.pageNo + 1
  47.     } 
  48.      
  49.     /** 
  50.      * 尾页 
  51.      * @return 
  52.      */ 
  53.     public int getBottomPageNo() { 
  54.         return this.getTotalPages(); 
  55.     } 
  56.      
  57.     public List getList() { 
  58.         return list; 
  59.     } 
  60.  
  61.     public void setList(List list) { 
  62.         this.list = list; 
  63.     } 
  64.  
  65.     public int getTotalRecords() { 
  66.         return totalRecords; 
  67.     } 
  68.  
  69.     public void setTotalRecords(int totalRecords) { 
  70.         this.totalRecords = totalRecords; 
  71.     } 
  72.  
  73.     public int getPageSize() { 
  74.         return pageSize; 
  75.     } 
  76.  
  77.     public void setPageSize(int pageSize) { 
  78.         this.pageSize = pageSize; 
  79.     } 
  80.  
  81.     public int getPageNo() { 
  82.         return pageNo; 
  83.     } 
  84.  
  85.     public void setPageNo(int pageNo) { 
  86.         this.pageNo = pageNo; 
  87.     } 

Oracle分页,SQL语句3层嵌套,Mysql则使用limit完成,更加的方便

 
 
  1. select * from  
  2.        ( 
  3.           select rownum rn, t.* from  
  4.            ( 
  5.                select * from t_user where user_id<>'root' order by user_id  
  6.            ) t where rownum <=? 
  7.        ) where rn>?;  

具体分页方法:

 
 
  1. /** 
  2.  * 分页查询 
  3.  * @param pageNo 
  4.  * @param pageSize 
  5.  * @return 
  6.  */ 
  7. public PageModel findAllUser(int pageNo, int pageSize) { 
  8.     StringBuffer sbSql = new StringBuffer(); 
  9.     sbSql.append("select * from "
  10.         .append("("
  11.             .append("select rownum rn, t.* from "
  12.             .append("("
  13.                 .append("select * from t_user where user_id<>'root' order by user_id "
  14.                 .append(") t where rownum <=?"
  15.         .append(") where rn>?"); 
  16.     Connection conn = null
  17.     PreparedStatement pstmt = null
  18.     ResultSet rs = null
  19.     PageModel pageModel = null
  20.     try { 
  21.         conn = DB.getConnection(); 
  22.         pstmt = conn.prepareStatement(sbSql.toString()); 
  23.         pstmt.setInt(1, pageNo * pageSize); 
  24.         pstmt.setInt(2, (pageNo -1)*pageSize); 
  25.         rs = pstmt.executeQuery(); 
  26.         List userList = new ArrayList(); 
  27.         while (rs.next()) { 
  28.             User user = new User(); 
  29.             user.setUserId(rs.getString("user_id")); 
  30.             user.setUserName(rs.getString("user_name")); 
  31.             user.setPassword(rs.getString("password")); 
  32.             user.setContactTel(rs.getString("contact_tel")); 
  33.             user.setEmail(rs.getString("email")); 
  34.             user.setCreateDate(rs.getTimestamp("create_date")); 
  35.             userList.add(user); 
  36.         } 
  37.         pageModel = new PageModel(); 
  38.         pageModel.setPageSize(pageSize); 
  39.         pageModel.setPageNo(pageNo); 
  40.         pageModel.setList(userList); 
  41.         pageModel.setTotalRecords(getTotalRecords(conn)); 
  42.     }catch(SQLException e) { 
  43.         e.printStackTrace(); 
  44.     }finally { 
  45.         DB.close(rs); 
  46.         DB.close(pstmt); 
  47.         DB.close(conn); 
  48.     }  
  49.     return pageModel; 

 


本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/678353,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值