分页
2015-06-04 17:54
429 查看
import java.util.List; public class QueryResultUtil<T> { private int pageNo = 1; private int pageSize = 8; private int totalItems; private List<T> list; public QueryResultUtil() { } public QueryResultUtil(int totalItems, List<T> adverMap) { this.totalItems = totalItems; this.list = adverMap; } public QueryResultUtil(int pageNo, int pageSize, int totalCount, List<T> list) { if (totalCount <= 0) this.totalItems = 0; else { this.totalItems = totalCount; } if (pageSize <= 0) this.pageSize = 20; else { this.pageSize = pageSize; } if (pageNo <= 0) this.pageNo = 1; else { this.pageNo = pageNo; } if ((this.pageNo - 1) * this.pageSize >= totalCount) { this.pageNo = (totalCount / pageSize); } this.list = list; } public int getPageNo() { return this.pageNo; } public int getPageSize() { return this.pageSize; } public int getTotalPage() { int totalPage = this.totalItems / this.pageSize; if (this.totalItems % this.pageSize != 0) { totalPage++; } return totalPage; } public boolean isFirstPage() { return this.pageNo <= 1; } public boolean isLastPage() { return this.pageNo >= getTotalPage(); } public int getNextPage() { if (isLastPage()) { return this.pageNo; } return this.pageNo + 1; } public int getPrePage() { if (isFirstPage()) { return this.pageNo; } return this.pageNo - 1; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getFirstResult() { return (this.pageNo - 1) * this.pageSize; } public int getTotalItems() { return totalItems; } public void setTotalItems(int totalItems) { this.totalItems = totalItems; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
Dao层调用:
return hibernateTemplate.execute(new HibernateCallback<QueryResultUtil<Map<String, Object>>>() { @SuppressWarnings("unchecked") @Override public QueryResultUtil<Map<String, Object>> doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createSQLQuery(countSql); Number number = (Number) query.uniqueResult(); int totalCout = number.intValue(); QueryResultUtil<Map<String, Object>> queryResult = new QueryResultUtil<Map<String, Object>>(startIndex, pageSize, totalCout, null); query = session.createSQLQuery(sql).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP) .setFirstResult(queryResult.getFirstResult()).setMaxResults(queryResult.getPageSize()); queryResult.setList(query.list()); return queryResult; } });
<div class="pageContainer"> <c:if test="${currentpage != 1}"> <a href="/cate/${name}/page/${currentpage-1}">上一页</a> <a href="/cate/${name}/page/1">1</a> <c:if test="${currentpage-1 != 1}"><span>...</span> <a href="/cate/${name}/page/${currentpage-1}">${currentpage-1}</a> </c:if> </c:if> <a href="javascript:;" style="color:#6CF;">${currentpage}</a> <c:if test="${currentpage != pagenum}"> <c:if test="${currentpage+1 != pagenum}"> <a href="/cate/${name}/page/${currentpage+1}">${currentpage+1}</a> <span>...</span> </c:if> <a href="/cate/${name}/page/${pagenum}">${pagenum}</a> <a href="/cate/${name}/page/${currentpage+1}">下一页</a> </c:if> </div>
效果请看:http://dianying.ky620.com/cate/kehuan/page/1
相关文章推荐
- NetBPM工作流的架构设计及实现浅析
- cocos 使用Luajit将Lua脚本编译为bytecode,从而实现加密
- maven nexus linux私服搭建
- 课程评价
- 写接口的体会
- IAP相关资源收集及开发总结,帮助你快速实现IAP功能
- JAVA -- 关键字 abstract、static、final、native、synchronized
- JAVA -- 关键字 abstract、static、final、native、synchronized
- JAVA -- 关键字 abstract、static、final、native、synchronized
- javascript实现SHA1算法
- 实验三 Java敏捷开发与XP实践
- Java实验三
- 团队项目第二阶段冲刺站立会议(6月3日)
- Android ListView 分类显示(二)多布局显示
- @ interface
- MySQL之——优化篇
- UITableView使用详解
- base 64 编解码
- AJAX jsonp 跨域请求code
- .Net连接字符串设置连接池大小显著提高数据库速度