分页/pagination

package org.pagination;
import java.util.List;

public interface Pagination<T> {
public Integer getTotalElements();
public List<T> getPageList();
}

 

package org.pagination;

import java.util.List;

public interface QueryHandler<T> {
public Integer getCount();

public List<T> getData(Integer pageIndex, Integer pageSize);
}

 

 

package org.pagination.impl;

import java.util.List;

import org.pagination.Pagination;
import org.pagination.QueryHandler;

public class DefaultPagination<T> implements Pagination<T> {
private Integer totalElements;
private List<T> pageList;
private QueryHandler<T> queryHandler;
private Integer pageIndex;
private Integer pageSize;
private Integer totalPages;
public DefaultPagination(Integer pageIndex,Integer pageSize,QueryHandler<T> queryHandler) {
this.queryHandler = queryHandler;
setTotalElements();//初始化总行数
setPageSize(pageSize);//修正页大小
calcTotalPage();//计算总页数
setPageIndex(pageIndex);//修正页码
setPageList();//初始化当前页数据
}
private void setTotalElements() {
totalElements = queryHandler.getCount();
}
private void setPageSize(Integer pageSize) {
if (pageSize < 1) {
this.pageSize = 1;
} else if (pageSize > totalElements && totalElements > 0) {
this.pageSize = totalElements;
} else {
this.pageSize = pageSize;
}
}
private void calcTotalPage(){
if (pageSize > 0) {
totalPages = (totalElements + pageSize - 1) / pageSize;
}

private void setPageIndex(Integer pageIndex) {
if (pageIndex < 1) {
this.pageIndex = 1;
} else if (pageIndex > totalPages && totalPages > 0) {
this.pageIndex = totalPages;
} else {
this.pageIndex = pageIndex;
}
}
private void setPageList() {
pageList = queryHandler.getData(pageIndex, pageSize);
}
@Override
public Integer getTotalElements() {
return totalElements;
}

@Override
public List<T> getPageList() {
return pageList;
}
}

转载于:https://www.cnblogs.com/stickitout/p/5208005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值