当前位置: 移动技术网 > IT编程>开发语言>Java > Java分页工具类及其使用(示例分享)

Java分页工具类及其使用(示例分享)

2019年07月22日  | 移动技术网IT编程  | 我要评论

pager.java

package pers.kangxu.datautils.common;
import java.io.serializable;
import java.util.list;
/**
 * 
 * <b> 分页通用类 </b>
 * 
 * @author kangxu
 * @param <t>
 * 
 */
public class pager<t> implements serializable {
 /**
 * 
 */
 private static final long serialversionuid = 4542617637761955078l;
 /**
 * currentpage 当前页
 */
 private int currentpage = 1;
 /**
 * pagesize 每页大小
 */
 private int pagesize = 10;
 /**
 * pagetotal 总页数
 */
 private int pagetotal;
 /**
 * recordtotal 总条数
 */
 private int recordtotal = 0;
 /**
 * previouspage 前一页
 */
 private int previouspage;
 /**
 * nextpage 下一页
 */
 private int nextpage;
 /**
 * firstpage 第一页
 */
 private int firstpage = 1;
 /**
 * lastpage 最后一页
 */
 private int lastpage;
 /**
 * content 每页的内容
 */
 private list<t> content;
 // 以下set方式是需要赋值的
 /**
 * 设置当前页 <br>
 * 
 * @author kangxu
 * 
 * @param currentpage
 */
 public void setcurrentpage(int currentpage) {
 this.currentpage = currentpage;
 }
 /**
 * 设置每页大小,也可以不用赋值,默认大小为10条 <br>
 * 
 * @author kangxu
 * 
 * @param pagesize
 */
 public void setpagesize(int pagesize) {
 this.pagesize = pagesize;
 }
 /**
 * 设置总条数,默认为0 <br>
 * 
 * @author kangxu
 * 
 * @param recordtotal
 */
 public void setrecordtotal(int recordtotal) {
 this.recordtotal = recordtotal;
 otherattr();
 }
 /**
 * 设置分页内容 <br>
 * 
 * @author kangxu
 * 
 * @param content
 */
 public void setcontent(list<t> content) {
 this.content = content;
 }
 /**
 * 设置其他参数
 * 
 * @author kangxu
 * 
 */
 public void otherattr() {
 // 总页数
 this.pagetotal = this.recordtotal % this.pagesize > 0 ? this.recordtotal / this.pagesize + 1 : this.recordtotal / this.pagesize;
 // 第一页
 this.firstpage = 1;
 // 最后一页
 this.lastpage = this.pagetotal;
 // 前一页
 if (this.currentpage > 1) {
 this.previouspage = this.currentpage - 1;
 } else {
 this.previouspage = this.firstpage;
 }
 // 下一页
 if (this.currentpage < this.lastpage) {
 this.nextpage = this.currentpage + 1;
 } else {
 this.nextpage = this.lastpage;
 }
 }
 // 放开私有属性
 public int getcurrentpage() {
 return currentpage;
 }
 public int getpagesize() {
 return pagesize;
 }
 public int getpagetotal() {
 return pagetotal;
 }
 public int getrecordtotal() {
 return recordtotal;
 }
 public int getpreviouspage() {
 return previouspage;
 }
 public int getnextpage() {
 return nextpage;
 }
 public int getfirstpage() {
 return firstpage;
 }
 public int getlastpage() {
 return lastpage;
 }
 public list<t> getcontent() {
 return content;
 }
 @override
 public string tostring() {
 return "pager [currentpage=" + currentpage + ", pagesize=" + pagesize
 + ", pagetotal=" + pagetotal + ", recordtotal=" + recordtotal
 + ", previouspage=" + previouspage + ", nextpage=" + nextpage
 + ", firstpage=" + firstpage + ", lastpage=" + lastpage
 + ", content=" + content + "]";
 }
}

使用 pagertester.java

package pers.kangxu.datautils.utils;
import java.util.arraylist;
import java.util.list;
import pers.kangxu.datautils.common.pager;
/**
 * 分页数据测试
 * <b>
 *
 * </b>
 * @author kangxu
 *
 */
public class pagertester {
 public static void main(string[] args) {
 pager<string> pager = new pager<string>();
 list<string> content = new arraylist<string>();
 content.add("str1");
 content.add("str2");
 content.add("str3");
 content.add("str4");
 content.add("str5");
 content.add("str6");
 content.add("str7");
 content.add("str8");
 content.add("str9");
 content.add("str10");
 pager.setcurrentpage(1);
 pager.setpagesize(10);
 pager.setrecordtotal(62);
 pager.setcontent(content);
 system.out.println(pager);
 }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网