81,092
社区成员
发帖
与我相关
我的任务
分享
public class Page<T> {
// -- 分页参数 --//
/**
* 当前页
*/
protected int pageNum = 1;
/**
* 每页的记录条数
*/
protected int pageSize = 10;
protected int totalPages=1;
protected List<Sort> orders = new ArrayList<Sort>();
protected boolean autoCount = true;
// -- 返回结果 --//
protected List<T> rows = new ArrayList<T>();
/**
* 总记录数
*/
protected long total = -1;
// -- 构造函数 --//
public Page() {
}
public Page(int pageSize) {
this.pageSize = pageSize;
}
public Page(int pageSize, int currentPage, long total) {
this.pageSize = pageSize;
this.pageNum = currentPage;
this.total = total;
}
// -- 访问查询参数函数 --//
/**
* 获得当前页的页号,序号从1开始,默认为1.
*/
public int getPageNum() {
return pageNum;
}
/**
* 设置当前页的页号,序号从1开始,低于1时自动调整为1.
*/
public void setPageNum(final int page) {
this.pageNum = page;
if (page < 1) {
this.pageNum = 1;
}
}
public Page<T> page(final int thePage) {
setPageNum(thePage);
return this;
}
/**
* 获得每页的记录数量,默认为1.
*/
public int getPageSize() {
return pageSize;
}
/**
* 设置每页的记录数量,低于1时自动调整为1.
*/
public void setPageSize(final int pageSize) {
this.pageSize = pageSize;
if (pageSize < 1) {
this.pageSize = 1;
}
}
public Page<T> pageSize(final int thePageSize) {
setPageSize(thePageSize);
return this;
}
/**
* 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
*/
public int getFirst() {
return ((pageNum - 1) * pageSize);
}
/**
* 获得排序方向.
*/
public Sort[] getOrders() {
Sort[] sorts = new Sort[orders.size()];
return orders.toArray(sorts);
}
/**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final Sort order) {
orders.add(order);
return this;
}
/**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final String field) {
orders.add(Sort.add(field, Sort.OrderStyle.ASC));
return this;
}
/**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final String field, final Sort.OrderStyle style) {
orders.add(Sort.add(field, style));
return this;
}
/**
* 是否已设置排序字段,无默认值.
*/
public boolean isOrderBySetted() {
return orders.size() > 0;
}
/**
* 查询对象时是否自动另外执行count查询获取总记录数, 默认为false.
*/
public boolean isAutoCount() {
return autoCount;
}
/**
* 查询对象时是否自动另外执行count查询获取总记录数.
*/
public Page<T> setAutoCount(final boolean autoCount) {
this.autoCount = autoCount;
return this;
}
public Page<T> autoCount(final boolean theAutoCount) {
setAutoCount(theAutoCount);
return this;
}
// -- 访问查询结果函数 --//
/**
* 取得页内的记录列表.
*/
public List<T> getRows() {
return rows;
}
/**
* 设置页内的记录列表.
*/
public Page<T> setRows(final List<T> rows) {
this.rows = rows;
return this;
}
/**
* 取得总记录数, 默认值为-1.
*/
public long getTotal() {
return total;
}
/**
* 设置总记录数.
*/
public Page<T> setTotal(final long total) {
this.total = total;
return this;
}
/**
* 根据pageSize与totalCount计算总页数, 默认值为-1.
*/
public long getTotalPages() {
if (total < 0)
return -1;
long count = total / pageSize;
if (total % pageSize > 0) {
count++;
}
return count;
}
/**
* 是否还有下一页.
*/
public boolean isHasNext() {
return (pageNum + 1 <= getTotalPages());
}
/**
* 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
*/
public int getNextPage() {
return isHasNext() ? pageNum + 1 : pageNum;
}
/**
* 是否还有上一页.
*/
public boolean isHasPre() {
return (pageNum - 1 >= 1);
}
/**
* 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
*/
public int getPrePage() {
return isHasPre() ? pageNum - 1 : pageNum;
}
}
public String show()
{
int pagesize = 5; //分页尺度
if(currentpage==0) currentpage=1;
if(name==null) name="";
if(sex==null) sex="";
//分页显示
page.getPages(currentpage, pagesize, "from Ryb where name like '%"+name+"%' and sex like '%"+sex+"%'");
pages = page.gettotalPages();//获取总页数
list = page.findByPage();
// list = rdao.findAll();
return "show";
}
private Zmxpage page;//分页
public Zmxpage getPage() {
return page;
}
public void setPage(Zmxpage page) {
this.page = page;
}
package common;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class Zmxpage extends HibernateDaoSupport {
private int first;//从哪开始
private int pageSize;//一页存在多少数据
private String hql;//查询语句
private String hql1;
public String getHql1() {
return hql1;
}
public void setHql1(String hql1) {
this.hql1 = hql1;
}
/*
* Zmxpage z = new Zmxpage();
* z.getPages(1,10"from BeanName");
* List list = z.findByPage();
*/
// 分页方法
public void getPages(int currentPage,int pageSize,String hql)
{
this.first = (currentPage-1)*pageSize;
this.pageSize = pageSize;
this.hql = hql;
}
public List findByPage()
{
try {
List result = (List)getHibernateTemplate().execute(
new HibernateCallback()
{
public Object doInHibernate(Session sess)throws HibernateException
{
List tmp = sess.createQuery(hql)
.setFirstResult(first)
.setMaxResults(pageSize)
.list();
return tmp;
}
});
return result;
} catch (RuntimeException re) {
throw re;
}
}
public int gettotalPages()
{//获取总页数
try {
List list = getHibernateTemplate().find("select count(*) "+hql);
int j=0;
j = ((Long)list.get(0)).intValue();
int i = 0;
if(j%pageSize==0) i = j/pageSize;
else i = j/pageSize+1;
return i;
} catch (RuntimeException re) {
throw re;
}
}
}