package log.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* 数据分页.
*
* @author flyxxxxx@163.com
*
*/
public class Paging implements java.io.Serializable{
private static final long serialVersionUID = 3189661608659403995L;
/**
* 当前页变量名
*/
public static final String CURRENT_PAGE = "currentPage";
/**
* 总页数变量名
*/
public static final String PAGES = "pages";
/**
* 总记录数变量名
*/
public static final String AMOUNT = "amount";
/**
* 开始数据位置变量名(从1开始)
*/
public static final String START = "pageStart";
/**
* 结束数据位置变量名(从1开始)
*/
public static final String END = "pageEnd";
/**
* 每页记录数变量名
*/
public static final String PAGE_SIZE = "pageSize";
/**
* 分页数据集合变量名
*/
public static final String COLLECTION = "collection";
/**
* 排序变量名
*/
public static final String ORDER = "order";
/**
* 默认的每页记录数
*/
public static final int DEFAULT_PAGE_SIZE = 10;
/**
* 每一页的大小.
*/
private int pageSize;
/**
* 当前页数
*/
private int currentPage;
/**
* 总记录数
*/
private int amount = 0;
/**
* 总页数
*/
private int pages;
/**
* 分页后的数据
*/
private Collection<?> data;
private String order;
public Paging(){
this(DEFAULT_PAGE_SIZE,1,null);
}
/**
* 构造分页对象
*
* @param pageSize
* 每页大小
* @param currentPage
* 当前页
*/
public Paging(int pageSize, int currentPage) {
this(pageSize, currentPage, null);
}
/**
* 构造分页对象
*
* @param pageSize
* 每页大小
* @param currentPage
* 当前页
* @param order
* 排序
*/
public Paging(int pageSize, int currentPage, String order) {
if (pageSize < 1) {
pageSize = 10;
}
if (currentPage < 1) {
currentPage = 1;
}
this.pageSize = pageSize;
this.currentPage = currentPage;
this.order = order;
}
/**
* 构造分页对象(使用默认页面大小)
*
* @param currentPage
* 当前页数
*/
public Paging(int currentPage) {
this(Paging.DEFAULT_PAGE_SIZE, currentPage, null);
}
/**
* 设定每一页的大小
*
* @param pageSize
* 每一页的大小
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 设定分页后的数据
*
* @param data
* List
*/
public void setData(Collection<?> data) {
this.data = data;
}
/**
* 设定总记录数
*
* @param amount
* 总记录数
*/
public void setAmount(int amount) {
if (amount < 0) {
throw new IllegalArgumentException(
"Param amount must be more than or equals zero.");
}
this.amount = amount;
this.pages = getPages(this.amount, this.pageSize);
}
/**
* 得到总页数
*
* @param amount
* 总记录数.
* @param pageSize
* 每页记录数
* @return 总页数
*/
public static int getPages(int amount, int pageSize) {
if (amount > 0) {
int k = amount % pageSize;
return k > 0 ? (amount - k) / pageSize + 1 : amount / pageSize;
}
return 1;
}
/**
* 得到当前页数
*
* @return 当前页数
*/
public int getCurrentPage() {
return this.currentPage;
}
/**
* 得到每一页的大小
*
* @return 每一页的大小
*/
public int getPageSize() {
return this.pageSize;
}
/**
* 得到分页后的数据
*
* @return Collection 分页后的数据
*/
public Collection<?> getData() {
return this.data == null ? new ArrayList<Object>() : data;
}
/**
* 得到总页数
*
* @return 总页数
*/
public int getPages() {
return this.pages == 0 ? 1 : this.pages;
}
/**
* 得到总记录数
*
* @return 总记录数
*/
public int getAmount() {
return this.amount;
}
/**
* 得到当前页数据量
*
* @return 当前页数据量
*/
public int getDataSize() {
if (this.data != null) {
return this.data.size();
}
return 0;
}
/**
* 数据开始位置
*
* @return 从1开始的数据开始位置
*/
public int getDataStart() {
return (this.currentPage - 1) * this.pageSize + 1;
}
/**
* 数据结束位置
*
* @return 从1开始的数据结束位置
*/
public int getDataEnd() {
return getDataStart() + getDataSize() - 1;
}
/**
* 对数组分页
*
* @param array
* 数组
*/
public void paging(Object[] array) {
if (array != null) {
setAmount(array.length);
int start = (this.currentPage - 1) * this.pageSize;
int size = array.length - start;
if (size > pageSize) {
size = pageSize;
}
List<Object> rs = new ArrayList<Object>();
for (int i = 0; i < size; i++) {
rs.add(array[start + i]);
}
this.data = rs;
}
}
/**
* 对集合分页
*
* @param list
* 集合
*/
public void paging(Collection<?> list) {
if (list != null) {
paging(list.toArray());
}
}
/**
* 得到排序字符串
*
* @return 排序字符串
*/
public String getOrder() {
return order;
}
/**
* 设定当前为第几页(从1开始)
*
* @param currentPage
* 当前为第几页
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
com.lanyuan.rar (31个子文件)
com.lanyuan
pom.xml 2KB
target
test-classes
org
clzps
main
MybatisSpringTest.class 2KB
classes
log
dao
WebUtils.class 2KB
LogService.class 314B
LogDao.class 306B
model
LogType.class 1KB
Log.class 2KB
util
Paging.class 4KB
impl
LogDaoImpl.class 2KB
LogServiceImpl.class 1KB
ibatis
LogMapper.xml 641B
sqlMapConfig.xml 344B
spring-jdbc.xml 673B
applicationContext-log-context.xml 1KB
.settings
org.maven.ide.eclipse.prefs 218B
org.eclipse.jdt.core.prefs 243B
src
test
resources
java
org
clzps
main
MybatisSpringTest.java 1KB
main
resources
ibatis
LogMapper.xml 641B
sqlMapConfig.xml 344B
spring-jdbc.xml 673B
applicationContext-log-context.xml 1KB
java
log
dao
LogDao.java 571B
LogService.java 534B
WebUtils.java 2KB
model
LogType.java 233B
Log.java 1KB
util
Paging.java 5KB
impl
LogServiceImpl.java 697B
LogDaoImpl.java 795B
.project 565B
.classpath 709B
共 31 条
- 1
资源评论
- lreis20102015-11-29实在是,没有想象的那么好啊。。。
- a4496461302013-07-29实在是,没有想象的那么好啊。。。
蓝缘
- 粉丝: 1086
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功