算法如下:
currentPage = (pageSize - 1) * pageSize //取得当前页
pageCount = (totalrecords + pageSize - 1) / pageSize //取得所有页数
oracle 三层查询 完成分页 sql:
select * from ( select rownum rn ,e.* from ( select * from emp where deptno=1 order by empno desc ) e where rownum <=(currentPage * pageSize) ) where rn>(currentPage - 1) * pageSize);
mssql 二层查询 完成分页 sql:
select top 5 * from UserInfo where UserId not in ( select top 5 UserID from UserInfo order by UserID asc ) order by UserID asc select top (pageSize) * from UserInfo where UserId not in ( select top (currentPage-1)*pageSize UserID from UserInfo order by UserID asc ) order by UserID asc
mysql 一层查询 完成分页 sql:
select * from user where typeid=1 order by id desc limit (pageSize - 1) * pageSize,pageSize ;
最后就是用pageModel这个类完成分页的其他功能!(java类) php 和 C# 有自己的分页类
package com.fafa.mvc.tools; import java.util.List; /* * 分页模型 */ public class PageModel { /** * 保存结果集 */ private List<?> list; /** * 总记录数 */ private int totalRecords; /** * 每页显示数 */ private int pageSize; /** * 当前页 */ private int currentPage; public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<?> getList() { return list; } public void setList(List<?> list) { this.list = list; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } /** * 首页 * @return */ public int getIndexPage() { return 1; } /** * 上一页 * @return */ public int getPrePage() { //如果当前页小于等于1 那么=1 if (this.currentPage <= 1){ return 1 ; } return this.currentPage - 1 ; } /** * 下一页 * @return */ public int getNextPage() { //如果当前页大于等于最后一页那么=最后一页 if (this.currentPage >= this.getEndPage()){ return getEndPage() ; } return this.currentPage + 1; } /** * 尾页 * @return */ public int getEndPage() { return this.getPageCount(); } /** * 总页数 * @return */ public int getPageCount() { // 总数 + 每页数量-1 / 每页数量 return (this.totalRecords + this.pageSize - 1) / this.pageSize; } }