日期:2014-05-20 浏览次数:20753 次
// 分页方法 public List getMoreSql(String hql, Pager pager) { if (pager == null) pager = new Pager(); String sortStr = ""; int sortIndex = hql.indexOf("order by"); if (sortIndex > 0) { sortStr = hql.substring(sortIndex + 8).trim();//截取order by 以后的字符串 ,即排序的字段 hql = hql.substring(0, sortIndex);//截取order by 以前的字符串 } if (null != pager.getSortStr() && !"".equals(pager.getSortStr())) sortStr = pager.getSortStr(); pager.setSortStr(sortStr); String newSql = ""; if ("".equals(sortStr)) newSql = hql; //无排序字段 else newSql = hql + " order by " + sortStr;//有排序字段 Session session = getSession(); String str = newSql.substring(newSql.indexOf("from "));//截取from 开始到最后的所有字符,为查询总共的行数 int count = calCountSql(hql);//总共的行数 pager.setRows(count);//设置行数 int limit = 0;//当前查询的起始行数 if (pager.getPageIndex() > 0) limit = pager.getPageSize() * (pager.getPageIn if (pager.getPageIndex() > pager.getPageCounts()) { limit = pager.getPageSize() * (pager.getPageCounts() - 1); pager.setPageIndex(pager.getPageCounts()); } limit = limit < 0 ? 0 : limit; List list = session.createSQLQuery(newSql).setFirstResult(limit).setMaxResults(pager.getPageSize()).list(); releaseSession(session); return list; } // 分页对象 package hzl.input.util; public class Pager { /**总行数*/ private int rows; /**总页数*/ private int pageCounts; /**第几页*/ private int pageIndex; /**每页显示行数*/ private int pageSize; private String pageStr; /**排序字段*/ private String sortStr; public Pager() { super(); this.rows = 0; this.pageCounts = 0; this.pageIndex = 1; this.pageSize = 8; } public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public int getPageCounts() { pageCounts = getRows() / getPageSize() + ((getRows() % pageSize) > 0 ? 1 : 0); pageCounts = pageCounts < 1 ? 1 : pageCounts; return pageCounts; } public void setPageCounts(int pageCounts) { this.pageCounts = pageCounts; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public String getSortStr() { return sortStr; } public void setSortStr(String sortStr) { this.sortStr = sortStr; } }