一、MySQL中的分页
格式:select * from 表 limit ?,?;
参数1:开始索引start,默认值:0。必须是正数
参数2:每页显示个数 pageSize
例如:
select * from products limit 0,5; #第一页,每页显示5条
select * from products limit 5,5; #第二页,每页显示5条
select * from products limet 10,5; #第三页.每页显示5条
select * from products limit ?,5; #第currentPage页,每页显示5条
start = (currentPage-1)*pageSize;
二、PageBean的设计
public class PageBean {
// 当前页数(浏览器传递)
private Integer currentPage;
// 每页显示条数(固定值,也可以是浏览器传递)
private Integer pageSize;
// 总记录数(数据库查询)
private Integer totalCount;
// 总页数
private Integer totalPage;
// 分页列表数据(数据库查询)
private List list;
public PageBean(Integer currentPage,Integer totalCount,Integer pageSize){
this.totalCount=totalCount;
this.pageSize=pageSize;
this.currentPage=currentPage;
if(this.currentPage==null){
// 如果页面没有指定显示哪一页,显示第一页
this.currentPage=1;
}
if(this.pageSize==null){
// 如果页面没有指定显示条数,显示3条
this.pageSize=3;
}
// 计算总页数
this.totalPage=(this.totalCount+this.pageSize-1)/this.pageSize;
// 判断当前页数是否超出范围
// 不能小于1
if(this.currentPage<1){
this.currentPage=1;
}
// 不能大于总页数
if(this.currentPage>this.totalPage){
this.currentPage=this.totalPage;
}
}
// 计算起始索引
public int getStart(){
return (this.currentPage-1)*this.pageSize;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "PageBean [currentPage=" + currentPage + ", pageSize=" + pageSize + ", totalCount=" + totalCount
+ ", totalPage=" + totalPage + ", list=" + list + "]";
}
}