java分页类

  最近学到一中比较好的java分页类,共享一下。希望各位留下一些建议!

 

  package com.demo.util;

public class FenYe { 
 private int pagesize;//每页显示的条数
 private int recordCount;//总行数
 private int currentpage;//当前页
 public FenYe(int pagesize,int recordCount,int currentpage){
  this.pagesize=pagesize;
  this.recordCount=recordCount;
  setCurrentPage(currentpage); 
 }
    //构造方法
    public FenYe(int pageSize, int recordCount) {
       this(pageSize, recordCount, 1);
    } 
    //得到每页显示的条数
    public int getPageSize() {
       return pagesize;
   }//设置每页显示的条数
    public void setPageSize(int pagesize) {
       this.pagesize = pagesize;
   }
  //  得到总的行数
    public int getRecordCount() {
        return recordCount;
     }//设置总共的条数
     public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }
     
     //得到当前页
     public int getCurrentPage() {
        return currentpage;
     }
  //设置当前页
   public void setCurrentPage(int currentPage) {
   int validPage =currentPage<=0?1:currentPage;
   validPage=validPage>getRecordCount()?getRecordCount():validPage;
   this.currentpage=validPage;   
  } 
  //总页数 
  public int getPageCount(){
    int size = recordCount/pagesize;//总条数/每页显示的条数=总页数
    int mod = recordCount%pagesize;//最后一页的条数
     if(mod != 0)
            size++;
           return recordCount == 0 ? 1 : size;
  }    
       //包含,起始索引为0
       public int getFromIndex() {
          //System.out.println("from index:"+(currentPage-1) * pageSize);
          return (currentpage-1) * pagesize;
      }
      //不包含
       public int getToIndex() {
          //System.out.println("to index:"+Math.min(recordCount, currentPage * pageSize));
          return  Math.min(recordCount, currentpage * pagesize);
      }
}

 

你可能感兴趣的:(java)