package com.sfpay.framework.base.pagination;
import java.io.Serializable;
import java.util.Collection;
//Ipage接口
public interface IPage
extends Serializable
{
public abstract long getTotal();
public abstract int getSize();
public abstract int getIndex();
public abstract long getTotalRecord();
public abstract Collection getData();
}
package com.sfpay.framework.base.pagination.impl;
import com.sfpay.framework.base.pagination.IPage;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
//Ipage实现类
public class Page
implements IPage
{
public Page()
{
}
public Page(Collection data, long totalRecord, int index, int size)
{
this.data = ((Collection) (data != null ? data : ((Collection) (new ArrayList(0)))));
this.totalRecord = totalRecord;
this.size = size;
this.index = index;
total = (totalRecord - 1L) / (long)size + 1L;
}
public void setTotalRecord(long totalRecord)
{
this.totalRecord = totalRecord;
}
public long getTotalRecord()
{
return totalRecord;
}
public void setTotal(int total)
{
this.total = total;
}
public int getSize()
{
return size;
}
public long getTotal()
{
return total;
}
public int getIndex()
{
return index;
}
public Collection getData()
{
return data;
}
public String toString()
{
StringBuilder buf = new StringBuilder();
Method methods[] = getClass().getMethods();
boolean isFirst = true;
int i = 0;
for(int n = methods.length; i < n; i++)
try
{
Method method = methods[i];
if((method.getModifiers() & 1) == 1 && method.getDeclaringClass() != java/lang/Object && (method.getParameterTypes() == null || method.getParameterTypes().length == 0))
{
String methodName = method.getName();
String property = null;
if(methodName.startsWith("get"))
property = (new StringBuilder(String.valueOf(methodName.substring(3, 4).toLowerCase()))).append(methodName.substring(4)).toString();
else
if(methodName.startsWith("is"))
property = (new StringBuilder(String.valueOf(methodName.substring(2, 3).toLowerCase()))).append(methodName.substring(3)).toString();
if(property != null)
{
Object value = method.invoke(this, new Object[0]);
if(isFirst)
isFirst = false;
else
buf.append(",");
buf.append(property);
buf.append(":");
if(value instanceof String)
buf.append("\"");
buf.append(value);
if(value instanceof String)
buf.append("\"");
}
}
}
catch(Exception exception) { }
return (new StringBuilder("{")).append(buf.toString()).append("}").toString();
}
private int size;
private int index;
private long total;
private long totalRecord;
private Collection data;
}
//PageSplit接口
public interface PageSplit<T> {
/**
* 方法说明:
* 查询总条数
* @return
* int
*/
public int queryTotalCount();
/**
* 方法说明:
* 查询返回的数据集合
* @param pageStart
* 起始行
* @param pageSize
* 行数
* @return
* List<T>
*/
public List<T> queryPageList(int pageStart, int pageSize);
}
//PageHandler
public class PageHandler {
private final static Logger logger = LoggerFactory
.getLogger(PageHandler.class);
/**
* 方法说明:
* @param pageSplit
* 分页接口
* @param pageSize
* 每一页记录条数
* @param pageNo
* 页码
* @return
* IPage<T> 分页取得的记录
*/
public static <T> IPage<T> execute(PageSplit<T> pageSplit, int pageSize, int pageNo) {
List<T> dataList = null;
IPage<T> page = null;
try {
int totalRecord = pageSplit.queryTotalCount();
if (totalRecord > 0) {
pageNo = (pageNo <= 0) ? 1 : pageNo;
pageSize = (pageSize <= 0) ? 10 : pageSize;
int pageStart = (pageNo - 1) * pageSize;
dataList = pageSplit.queryPageList(pageStart, pageSize);
page = new Page<>(dataList, totalRecord, pageNo, pageSize);
}
if (null == page) {
page = new Page<>(new ArrayList<>(), 0, 1, 1);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return page;
}
}
//service层调用
public void queryInvoiceInput(BeanBase bean) {
bean.getExpData().put("page",
PageHandler.execute(new PageSplit<Map<String, Object>>() {
@Override
public int queryTotalCount() {
Map<String, String> value = bean.getDatas().get(0);
PurviewUtil.getUserDeptString(bean);
return invoiceInputDao.queryInvoiceInputCount(value);
}
@Override
public List<Map<String, Object>> queryPageList(
int pageStart, int pageSize) {
return invoiceInputDao.queryInvoiceInput(bean.getDatas().get(0), pageStart, pageSize);
}
}, bean.getRows(), bean.getPage()));
}
mysql分页
最新推荐文章于 2022-07-22 17:27:34 发布