分页样式如:
Jsp文件
第一种
采用Hibernate、Hql分页时,数据库需要的几个关键信息:
起始索引 fromIndex = (currentPage-1)*pageSize 每次查询最大返回记录数 pageSize
query.setFirstResult (fromIndex);
query.setMaxResults (pageSize);
第二种
使用mysql分页时,需要的几个关键信息:
offset:偏移量,也就是起始索引fromIndex = (currentPage-1)*pageSize
n:指定返回的最大记录数 n=pageSize
-- select * from tb limit offset,n;
-------------------------------
用来存放sql查询返回的数据的集合类 List<T>
用来存放分页数据的类设计如下
package com.imooc.page.model;
import java.io.Serializable;
import java.util.List;
public class Pager<T> implements Serializable{
private static final long serialVersionUID = 3044938529345238647L;
private int totalRecord;//[一共多少条记录]
private int currentPage;//[当前第几页数据]
private int totalPage;//[一共多少页记录]
private int pageSize;//[每页显示多少条记录]
private List<T> dataList;//[要显示的数据]
/*
* 构建 hibernate,HQL/MySQL分页 对象
*/
public Pager(int pageSize, int currentPage, int totalRecord, int totalPage,
List<T> dataList) {
super();
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalRecord = totalRecord;
this.totalPage = totalPage;
this.dataList = dataList;
}
public Pager() {
super();
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentPage() {
return currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
@Override
public String toString() {
return "Pager [pageSize=" + pageSize + ", currentPage=" + currentPage
+ ", totalRecord=" + totalRecord + ", totalPage=" + totalPage
+ ", dataList=" + dataList + "]";
}
}