要实现通用的分页查询要实现两个JavaBean
第一个是封装客户机带过来的分页请求信息
第二个是封装客户机要显示的分页信息
JavaBean的代码如下:
- package cn.test.domain;
- //封装客户机带过来的分页请求信息
- public class PageQuery {
- private int currentpage = 1;
- private int pagesize = 9;
- private String condition; //记住客户机带过来的查询条件 category_id
- private String value; //记住客户机带过来的查询条件的值 3
- private int startindex;
- private String where; //where category_id=?
- public int getStartindex() {
- this.startindex = (this.currentpage-1)*this.pagesize;
- return startindex;
- }
- public String getWhere() {
- if(this.condition==null || this.condition.trim().equals("")){
- this.where = null;
- }else{
- this.where = "where " + condition + "=?";
- }
- return where;
- }
- public int getCurrentpage() {
- return currentpage;
- }
- public void setCurrentpage(int currentpage) {
- this.currentpage = currentpage;
- }
- public int getPagesize() {
- return pagesize;
- }
- public void setPagesize(int pagesize) {
- this.pagesize = pagesize;
- }
- public String getCondition() {
- return condition;
- }
- public void setCondition(String condition) {
- this.condition = condition;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- }
- package cn.test.domain;
- import java.util.List;
- //封装客户机要显示的分页信息
- public class PageBean {
- private List list;
- private int totalrecord;
- private int pagesize;
- private int totalpage;
- private int currentpage;
- private int previouspage;
- private int nextpage;
- private int[] pagebar;
- public List getList() {
- return list;
- }
- public void setList(List list) {
- this.list = list;
- }
- public int getTotalrecord() {
- return totalrecord;
- }
- public void setTotalrecord(int totalrecord) {
- this.totalrecord = totalrecord;
- }
- public int getPagesize() {
- return pagesize;
- }
- public void setPagesize(int pagesize) {
- this.pagesize = pagesize;
- }
- public int getTotalpage() {
- if(this.totalrecord%this.pagesize==0){
- thisthis.totalpage = this.totalrecord/this.pagesize;
- }else{
- thisthis.totalpage = this.totalrecord/this.pagesize + 1;
- }
- return totalpage;
- }
- public int getCurrentpage() {
- return currentpage;
- }
- public void setCurrentpage(int currentpage) {
- this.currentpage = currentpage;
- }
- public int getPreviouspage() {
- if(this.currentpage-1>0){
- thisthis.previouspage = this.currentpage-1;
- }else{
- this.previouspage = 1;
- }
- return previouspage;
- }
- public int getNextpage() {
- if(this.currentpage+1>this.totalpage){
- thisthis.nextpage = this.totalpage;
- }else{
- thisthis.nextpage = this.currentpage + 1;
- }
- return nextpage;
- }
- public int[] getPagebar() {
- this.pagebar = new int[getTotalpage()];
- for(int i=1;i<=this.totalpage;i++){
- this.pagebar[i-1] = i;
- }
- return pagebar;
- }
- }
在MySql中可以很好的支持分页查询比如:
select * from book limit 0,9
这条sql语句代表的意思是从book表中第一行数据开始查,往下数九行,也就是每页显示九条数据,如果是第二页的话只改变第一个数值就可以了也就是“select * from book limit 9,9”
原文地址http://blog.csdn.net/furongkang/article/details/6776605