java分页工具类


前言 : 使用 mybatis 分页插件有时会报莫名其妙的错误,有时又不会,所有自己定义了一个分页工具类和分页数据传递类 。


不多说,代码如下:


分页工具类 : 

/**
	 * @Title   paging     
	 * @param   @param page 		当前页
	 * @param   @param allDatas		需要分页的数据集
	 * @param   @param currentSize	每页数据条数
	 * @param   @return      
	 * @return  PageDatas<E>
	 */
	public static <E> PageDatas<E> paging(Integer page, List<E> allDatas , Integer currentSize) {
		try {
			if(allDatas.size() == 0)
				return null;
			if (page == null || page == 0)
				page = 1;
			// 总记录数
			long total = allDatas.size();//21
			// 总页数
			int pages;//2
			// 每页记录数
			if (total < currentSize) {
				pages = 1;
			}else {
				if (total % currentSize == 0) {
					pages = (int) (total / currentSize);
				} else {
					pages = (int) (total / currentSize + 1);
				}
			}
			// 当前页数据第一个索引
			int currentPage ;
			if (page == 1) {
				currentPage = 0;
			}else {
				currentPage = (page - 1) * currentSize;//20
			}
			// 当前页数据最后一个索引
			int finalRecord;
			// 当前页数据集
			List<E> datas = null;
			if (currentSize >= total) {
				finalRecord = (int) total;
			}else if (page == pages) {
				finalRecord = (int) total;
			}else {
				finalRecord = currentPage + currentSize;
			}
			if (currentPage > total || finalRecord > total) {
				return null;
			}
			if (currentSize >= total) {
				datas = allDatas;
			} else {
				datas = allDatas.subList(currentPage, finalRecord);
			}

			PageDatas<E> pageDatas = new PageDatas<E>(total, pages, datas);
			return pageDatas;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}






分页数据传递类 : 


package com.pro.huanbao.common.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * @Title PageDatas.java
 * @Package com.pro.huanbao.common.pojo
 * @author wanpu_ly
 * @dade 2017年10月24日 上午8:56:05
 * @version V1.0 类说明: 分页数据传递类
 */
public class PageDatas<E> implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7216920941926389211L;
	// 总记录数
	private long total;
	// 总页数
	private int pages;
	// 当前页数据
	private List<E> datas;

	public PageDatas(long total, int pages, List<E> datas) {
		super();
		this.total = total;
		this.pages = pages;
		this.datas = datas;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public List<E> getDatas() {
		return datas;
	}

	public void setDatas(List<E> datas) {
		this.datas = datas;
	}

}




有错或者不足之处请指出,谢谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值