分页方法
package cn.et.web.utils;
import java.util.List;
/**
* 分页
* @author root
*
*/
public class PageTools {
/**
* 构造参数
* @param curpage 页面传入的当前页
* @param totalCount 数据库查询的总记录数
* @param pageCount 每页显示的条数
*/
public PageTools(Integer curPage,Integer totalCount,Integer pageCount) {
this.curPage = curPage;
this.totlaCount = totalCount;
this.pageCount = pageCount==null?this.pageCount:pageCount;
this.prePage =(curPage==1?1:curPage-1);
this.totalPage=totalCount%pageCount==0?totalCount/pageCount:totalCount/pageCount+1;
this.nextPage=(curPage==totalPage)?totalPage:(curPage+1);
this.startIndex = (curPage-1)*pageCount+1;
this.endIndex = curPage*pageCount;
}
/**
* 当前页
*/
private Integer curPage;
public Integer getStartIndex() {
return startIndex;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public Integer getEndIndex() {
return endIndex;
}
public void setEndIndex(Integer endIndex) {
this.endIndex = endIndex;
}
/**
* 每页显示条数
*/
private Integer pageCount;
/**
* 上一页
*/
private Integer prePage;
/**
* 下一页
*/
private Integer nextPage;
/**
* 总页数
*/
private Integer totalPage;
/**
* 总记录数(从数据库查询)
*/
private Integer totlaCount;
/**
* 开始索引
*/
private Integer startIndex;
/**
* 结束索引
*/
private Integer endIndex;
private List data;
public Integer getCurPage() {
return curPage;
}
public void setCurPage(Integer curPage) {
this.curPage = curPage;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public Integer getPrePage() {
return prePage;
}
public void setPrePage(Integer prePage) {
this.prePage = prePage;
}
public Integer getNextPage() {
return nextPage;
}
public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotlaCount() {
return totlaCount;
}
public void setTotlaCount(Integer totlaCount) {
this.totlaCount = totlaCount;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
}
mysql分页语句
//pageTools.getStartIndex()-1 当前从哪开始pageTools.getPageCount()显示多少条数据
String sql = "select * from goods limit "+(pageTools.getStartIndex()-1)+","+pageTools.getPageCount();
oracle分页语句
String sql ="select * from (select d.*,rownum rn from food d) where rn >= "+pt.getStartIndex()+" and rn<="+pt.getEndIndex()+" and foodname like '%"+dname+"%'";
1.根据rowid来分
select * from t_xiaoxi where rowid in (select rid from (select rownum rn, rid from(select rowid rid, cid from t_xiaoxi order by cid desc) where rownum<10000) where rn>9980) order by cid desc;
执行时间0.03秒
2.按分析函数来分
select * from (select t.*, row_number() over(order by cid desc) rk from t_xiaoxi t) where rk<10000 and rk>9980;
执行时间1.01秒
3.按rownum来分
select * from (select t.*,rownum rn from(select * from t_xiaoxi order by cid desc)t where rownum<10000) where rn>9980;
执行时间0.1秒
其中t_xiaoxi为表名称,cid为表的关键字段,取按cid降序排序后的第9981-9999条记录,t_xiaoxi表有70000多条记录。
个人感觉1的效率最好,3次之,2最差。