QueryInfo.java
package cn.itcast.domain;
public class QueryInfo {
private int currentpage = 1; //用户当前看的页
private int pagesize = 5; //记住用户想看的页面大小
private int startindex; //记住用户看的页的数据在数据库的起始位置
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 int getStartindex() {
if(this.currentpage-1<1) {
this.startindex = 0;
}else {
this.startindex = (this.currentpage-1)*this.pagesize;
}
return startindex;
}
}
QueryResult.java
package cn.itcast.domain;
import java.util.List;
public class QueryResult {
private List list; //当前页的页面数据
private int totalrecord; //总记录数
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}
}
PageBean.java
package cn.itcast.domain;
import java.util.List;
public class PageBean {
private List list;
private int totalrecord;
private int pagesize;
private int totalpage;
private int currentpage;
private int previouspage;
private int nextpage;
private int[] pagebar;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(int totalrecord) {
this.totalrecord = totalrecord;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getTotalpage() {
//100 5 20
//101 5 21
//99 5 20
if(this.totalrecord%this.pagesize==0){
this.totalpage = this.totalrecord/this.pagesize;
}else{
this.totalpage = this.totalrecord/this.pagesize+1;
}
return totalpage;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getPreviouspage() {
if(this.currentpage-1<1){
this.previouspage = 1;
}else{
this.previouspage = this.currentpage-1;
}
return previouspage;
}
public int getNextpage() {
if(this.currentpage+1>=this.totalpage){
this.nextpage = this.totalpage;
}else{
this.nextpage = this.currentpage +1;
}
return nextpage;
}
public int[] getPagebar() {
int startpage=0;
int endpage=0;
int pagebar[] = null;
if(this.totalpage <= 10) {
startpage = 1;
endpage = this.totalpage;
pagebar = new int[this.totalpage];
}else{
pagebar = new int[10];
if(this.currentpage+4 > this.totalpage) {
endpage = this.totalpage;
startpage = endpage-9;
}else if(startpage-5 < 0) {
startpage = 1;
endpage = this.totalpage;
}else {
startpage = this.currentpage-5;
endpage = this.currentpage+4;
}
}
int index = 0;
for(int i=startpage;i<=endpage;i++){
pagebar[index++] = i;
}
this.pagebar = pagebar;
return pagebar;
/* int startpage;
int endpage;
int pagebar[] = null;
if(this.totalpage<=10){
pagebar = new int[this.totalpage];
startpage = 1;
endpage = this.totalpage;
}else{
pagebar = new int[10];
startpage = this.currentpage - 4;
endpage = this.currentpage + 5;
//总页数=30 3 -1
//总页数=30 29 34 21 30
if(startpage<1){
startpage = 1;
endpage = 10;
}
if(endpage>this.totalpage){
endpage = this.totalpage;
startpage = this.totalpage - 9;
}
}
int index = 0;
for(int i=startpage;i<=endpage;i++){
pagebar[index++] = i;
}
this.pagebar = pagebar;*/
/*int pagebar[] = new int[this.totalpage];
for(int i=1;i<=this.totalpage;i++){
pagebar[i-1] = i;
}
this.pagebar = pagebar;
return pagebar;*/
}
}
DaoImpl.java
//获取到页面数据和页面大小
public QueryResult pageQuery(int startindex,int pagesize){
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
QueryResult qr = new QueryResult();
try{
conn = JdbcUtils.getConnection();
String sql = "select * from customer limit ?,?";
st = conn.prepareStatement(sql);
st.setInt(1, startindex);
st.setInt(2, pagesize);
rs = st.executeQuery();
List list = new ArrayList();
while(rs.next()){
Customer c = new Customer();
c.setBirthday(rs.getDate("birthday"));
c.setCellphone(rs.getString("cellphone"));
c.setDescription(rs.getString("description"));
c.setEmail(rs.getString("email"));
c.setGender(rs.getString("gender"));
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setPreference(rs.getString("preference"));
c.setType(rs.getString("type"));
list.add(c);
}
qr.setList(list);
sql = "select count(*) from customer";
st = conn.prepareStatement(sql);
rs = st.executeQuery();
if(rs.next()){
qr.setTotalrecord(rs.getInt(1));
}
return qr;
}catch (Exception e) {
throw new DaoException(e);
}finally{
JdbcUtils.release(conn, st, rs);
}
}
BusinessServiceImpl.java
public PageBean pageQuery(QueryInfo queryinfo) {
//1.调用dao获取页面数据
QueryResult qr = dao.pageQuery(queryinfo.getStartindex(), queryinfo.getPagesize());
//根据dao查询结果,生成页面需要pagebean
PageBean bean = new PageBean();
bean.setCurrentpage(queryinfo.getCurrentpage());
bean.setList(qr.getList());
bean.setPagesize(queryinfo.getPagesize());
bean.setTotalrecord(qr.getTotalrecord());
return bean;
}
接下来在jsp页面中使用${bean.***}即可实现分类