通用分页查询

要实现通用的分页查询要实现两个JavaBean

第一个是封装客户机带过来的分页请求信息

第二个是封装客户机要显示的分页信息

JavaBean的代码如下:

  1. package cn.test.domain;  
  2.   
  3. //封装客户机带过来的分页请求信息  
  4. public class PageQuery {  
  5.   
  6.     private int currentpage = 1;  
  7.     private int pagesize = 9;  
  8.     private String condition;  //记住客户机带过来的查询条件  category_id  
  9.     private String value;   //记住客户机带过来的查询条件的值  3  
  10.     private int startindex;  
  11.     private String where;   //where category_id=?  
  12.       
  13.     public int getStartindex() {  
  14.         this.startindex = (this.currentpage-1)*this.pagesize;  
  15.         return startindex;  
  16.     }  
  17.     public String getWhere() {  
  18.         if(this.condition==null || this.condition.trim().equals("")){  
  19.             this.where = null;  
  20.         }else{  
  21.             this.where = "where " + condition + "=?";  
  22.         }  
  23.         return where;  
  24.     }  
  25.       
  26.     public int getCurrentpage() {  
  27.         return currentpage;  
  28.     }  
  29.     public void setCurrentpage(int currentpage) {  
  30.         this.currentpage = currentpage;  
  31.     }  
  32.     public int getPagesize() {  
  33.         return pagesize;  
  34.     }  
  35.     public void setPagesize(int pagesize) {  
  36.         this.pagesize = pagesize;  
  37.     }  
  38.     public String getCondition() {  
  39.         return condition;  
  40.     }  
  41.     public void setCondition(String condition) {  
  42.         this.condition = condition;  
  43.     }  
  44.     public String getValue() {  
  45.         return value;  
  46.     }  
  47.     public void setValue(String value) {  
  48.         this.value = value;  
  49.     }  
  50.       
  51.       
  52. }  

 

  1. package cn.test.domain;  
  2.   
  3. import java.util.List;  
  4.   
  5. //封装客户机要显示的分页信息  
  6. public class PageBean {  
  7.       
  8.     private List list;  
  9.     private int totalrecord;  
  10.     private int pagesize;  
  11.       
  12.     private int totalpage;  
  13.     private int currentpage;  
  14.       
  15.     private int previouspage;  
  16.     private int nextpage;  
  17.     private int[] pagebar;  
  18.     public List getList() {  
  19.         return list;  
  20.     }  
  21.     public void setList(List list) {  
  22.         this.list = list;  
  23.     }  
  24.     public int getTotalrecord() {  
  25.         return totalrecord;  
  26.     }  
  27.     public void setTotalrecord(int totalrecord) {  
  28.         this.totalrecord = totalrecord;  
  29.     }  
  30.     public int getPagesize() {  
  31.         return pagesize;  
  32.     }  
  33.     public void setPagesize(int pagesize) {  
  34.         this.pagesize = pagesize;  
  35.     }  
  36.     public int getTotalpage() {  
  37.           
  38.         if(this.totalrecord%this.pagesize==0){  
  39.             thisthis.totalpage = this.totalrecord/this.pagesize;  
  40.         }else{  
  41.             thisthis.totalpage = this.totalrecord/this.pagesize + 1;  
  42.         }  
  43.           
  44.         return totalpage;  
  45.     }  
  46.       
  47.     public int getCurrentpage() {  
  48.         return currentpage;  
  49.     }  
  50.     public void setCurrentpage(int currentpage) {  
  51.         this.currentpage = currentpage;  
  52.     }  
  53.     public int getPreviouspage() {  
  54.         if(this.currentpage-1>0){  
  55.             thisthis.previouspage = this.currentpage-1;  
  56.         }else{  
  57.             this.previouspage = 1;  
  58.         }  
  59.         return previouspage;  
  60.     }  
  61.     public int getNextpage() {  
  62.         if(this.currentpage+1>this.totalpage){  
  63.             thisthis.nextpage = this.totalpage;  
  64.         }else{  
  65.             thisthis.nextpage = this.currentpage + 1;  
  66.         }  
  67.         return nextpage;  
  68.     }  
  69.     public int[] getPagebar() {  
  70.         this.pagebar = new int[getTotalpage()];  
  71.         for(int i=1;i<=this.totalpage;i++){  
  72.             this.pagebar[i-1] = i;  
  73.         }  
  74.         return pagebar;  
  75.     }     
  76.       
  77. }  



在MySql中可以很好的支持分页查询比如:

select * from book limit 0,9

这条sql语句代表的意思是从book表中第一行数据开始查,往下数九行,也就是每页显示九条数据,如果是第二页的话只改变第一个数值就可以了也就是“select * from book limit 9,9”

原文地址http://blog.csdn.net/furongkang/article/details/6776605
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值