1.创建一个Page类
package com.hotel.util;
public class Page {
private int nextPage=1;
private int previouPage=1;
private int lastPage=1;
private int firstPage=1;
private int currentPage=1;
private int nextPage=1;
private int previouPage=1;
private int lastPage=1;
private int firstPage=1;
private int currentPage=1;
public Page(int nowPage,int pageSize,int total){
this.currentPage=nowPage;
calcuPage(nowPage,pageSize,total);
}
public void calcuPage(int nowPage,int pageSize,int total) {
if((nowPage-1)*pageSize>=total){//total为空时
nowPage-=1;
currentPage=nowPage;
}
if(nowPage*pageSize<total){
nextPage=nowPage+1;
}
else{
nextPage=nowPage;
}
if(nowPage<=1){
previouPage=1;
}
else{
previouPage=nowPage-1;
}
firstPage=total==0?0:1;
lastPage=total%pageSize==0?total/pageSize:total/pageSize+1;
}
public int getNextPage() {
return nextPage;
}
public int getPreviouPage() {
return previouPage;
}
public int getLastPage() {
return lastPage;
}
public int getFirstPage() {
return firstPage;
}
public int getCurrentPage() {
return currentPage;
}
this.currentPage=nowPage;
calcuPage(nowPage,pageSize,total);
}
public void calcuPage(int nowPage,int pageSize,int total) {
if((nowPage-1)*pageSize>=total){//total为空时
nowPage-=1;
currentPage=nowPage;
}
if(nowPage*pageSize<total){
nextPage=nowPage+1;
}
else{
nextPage=nowPage;
}
if(nowPage<=1){
previouPage=1;
}
else{
previouPage=nowPage-1;
}
firstPage=total==0?0:1;
lastPage=total%pageSize==0?total/pageSize:total/pageSize+1;
}
public int getNextPage() {
return nextPage;
}
public int getPreviouPage() {
return previouPage;
}
public int getLastPage() {
return lastPage;
}
public int getFirstPage() {
return firstPage;
}
public int getCurrentPage() {
return currentPage;
}
}
2.根据当前页和每页大小,查询数据库中的表
核心sql语句(以emp表为例)
select * from (select e.*,rownum row_num from emp e where rownum < =currentpage*pagesize) where row_num > (currentpage-1)*pagesize;
如果要求查询出的数据进行了排序,可以分为3步走
1.select * from emp where order by empno;记为临时表A
2.select A.*,rownum row_num from A where rownum <= currentpage*pagesize;记为临时表B
3.select * from B where row_num >(currentpage-1)*pagesize;
合起来即为
select * from (select A.*,rownum row_num from (select * from emp order by empno ) A where rownum <= 10) where row_num > 0;
转载于:https://blog.51cto.com/zhiyuan1111/417405