Fork me on GitHub

后端分页工具类

后端分页实现

作下面的分页查询

后台代码如下:

/**
* 查询
*/
@RequestMapping(method = RequestMethod.POST, value = "/query")
public void query(Database db) {
DetachedCriteria criteria=getPagination().getCriteria();
if(!StringUtils.isEmpty(db.getInstanceName())) {
criteria.add(Restrictions.like("database_name",db.getInstanceName().trim(),MatchMode.ANYWHERE));
}

if(!StringUtils.isEmpty(db.getIsConnect())) {
criteria.add(Restrictions.like("isConnect",db.getIsConnect().trim(),MatchMode.ANYWHERE));
}

databaseService.find(getPagination());
printJson(getPagination());

}

getPagination代码如下:

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.core.ResolvableType;
import com.cloud.icenter.common.utils.Pagination;
public abstract class ModelAction<T> extends BaseAction {

private Class<T> clazz;
public ModelAction() {
ResolvableType resolvableType = ResolvableType.forClass(getClass());
this.clazz=(Class<T>) resolvableType.getSuperType().getGeneric(0).resolve();
}

/**
* 获取页号
* @return
*/
public int getPage() {
return getIntParameter("page", 1);
}

/**
* 获取每页查询多少行
* @return
*/
public int getPageSize() {
return getIntParameter("rows", Pagination.DEFAULT_PAGE_SIZE);
}

/**
* 获取分页器
* @return
*/
public Pagination<T> getPagination() {
Pagination<T> pagin=(Pagination<T>) getAttribute("pagin");
if(pagin==null) {
pagin=new Pagination<T>(getPage(),getPageSize());
pagin.buildCriteria(clazz);
setAttribute("pagin", pagin);
}
return pagin;
}

/**
* 获取分页器
* @return
*/
public <E> Pagination<E> newPagination(Class<E> e) {
Pagination<E> pagin=new Pagination<E>(getPage(),getPageSize());
pagin.buildCriteria(e);
return pagin;
}

public T getModel() {
T model=(T) getAttribute("model");
if(model==null) {
try {
model = clazz.newInstance();
BeanUtils.populate(model, getRequest().getParameterMap());
setAttribute("model", model);
} catch (Exception e) {
throw new RuntimeException("获取模型对象失败!", e);
}
}
return model;
}

/**
* 快速获取id参数
* @return
*/
protected String getId() {
return getParameter("id");
}
}

Pagination代码如下:

/**
* 分页器
* 页号从1开始,page==1就是第一页
* 行号从0开始
* @author zhangle
*/
public class Pagination<T> {

public static final int DEFAULT_PAGE_SIZE=20;    //默认分页行数

private int page;    //当前页号,从1开始
private int pageSize;    //每页多少行
private long totalCount;    //总共有多少行
private List<T> dataList = new ArrayList<T>();    //数据列表
private DetachedCriteria criteria; //查询条件

public Pagination(int page) {
this(page,DEFAULT_PAGE_SIZE);
}
public Pagination(int page,int pageSize) {
this.page=page;
this.pageSize=pageSize;
}

public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}

/**
* 获得首页页号
* @return
*/
public int getFirstPage() {
return 1;
}
/**
* 获得上一页页号
* @return
*/
public int getPrevPage() {
return page<=getFirstPage()?getFirstPage():page-1;
}
/**
* 获得下一页页号
* @return
*/
public int getNextPage() {
return page>=getLastPage()?getLastPage():page+1;
}
/**
* 获得最后一页页号
* @return
*/
public int getLastPage() {
return ((int)Math.ceil(((double)totalCount)/((double)pageSize)));
}
/**
* 判断是否第一页
* @return
*/
public boolean getIsFirstPage() {
return page<=getFirstPage();
}
/**
* 判断是否最后一页
* @return
*/
public boolean getIsLastPage() {
return page>=getLastPage();
}
/**
* 获取当前页的起始行号
* @return
*/
public int getStartResult() {
return (page-1)*pageSize;
}
/**
* 获取当前页的结束行号
* @return
*/
public long getEndResult() {
int endResult=page*pageSize;
if(endResult>=totalCount) return totalCount;
return endResult;
}
/**
* 获取总共多少页
* @return
*/
public int getPageCount() {
return getLastPage();
}

/**
* 获取查询条件对象
* @return
*/
public DetachedCriteria getCriteria() {
return criteria;
}
/**
* 设置查询条件对象
* @param criteria
*/
public void setCriteria(DetachedCriteria criteria) {
this.criteria = criteria;
}
/**
* 构造一个查询条件对象,并返回
* @param clazz
* @return
*/
public DetachedCriteria buildCriteria(Class<T> clazz) {
criteria=DetachedCriteria.forClass(clazz);
return criteria;
}
}

分页工具类另一种写法

/**
* @description 自定义分页
* @author wangbin
* @date 2015/12/22
*
*/
public class PagingUtil {

public static final int MAX_PAGE_SIZE = 50;// 每页最大记录数限制

private int page = 1;// 当前页
private int pages = 0;// 总页数
private int rows = 15;// 每页记录数
private int total = 0;// 总记录数

private int pageStart;// 分页开始数

private List<Object> list;// 数据List

public int getPage() {
return page;
}

public void setPage(int page) {
if (page < 1) {
page = 1;
}
this.page = page;
}

public int getPages() {
pages = this.getTotal() / this.getRows();
if (this.getTotal() % this.getRows() > 0) {
pages++;
}
return pages;
}

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

public int getRows() {
return rows;
}

public void setRows(int rows) {
if (rows < 1) {
rows = 1;
} else if (rows > MAX_PAGE_SIZE) {
rows = MAX_PAGE_SIZE;
}
this.rows = rows;
}

public int getTotal() {
return total;
}

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

public int getPageStart() {
return (page - 1) * rows;
}

public void setPageStart(int pageStart) {
this.pageStart = pageStart;
}

public List<Object> getList() {
return list;
}

public void setList(List<Object> list) {
this.list = list;
}

/**
*
* @param pageNo 当前页码
* @param pageSize 页数
* @param list 所有集合
* @return
* @throws Exception
*/
public static List<?> page(int pageNo, int pageSize, List<?> list)
throws Exception {
List<Object> result = new ArrayList<Object>();
if (list != null && list.size() > 0) {
int allCount = list.size();
int pageCount = (allCount + pageSize - 1) / pageSize;
if (pageNo >= pageCount) {
pageNo = pageCount;
}
int start = (pageNo - 1) * pageSize;
int end = pageNo * pageSize;
if (end >= allCount) {
end = allCount;
}
for (int i = start; i < end; i++) {
result.add(list.get(i));
}
}
return result;
}

}