闲来无事, 自己写了一个分页查询, 可以提供初学者作为参考
1.首先封装一个分页对象, 通常情况下, 分页需要封装以下几个参数:
package com.crud.utils;
import java.util.List;
public class PageBean {
private int currPage; //当前页
private int totalPage; //总页数
private int count; //总记录数
private int pageSize; //每页显示数量
private List list; //用于存储每页显示的记录Customer对象的个数
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
2 , 通过查询数据库, 我们可以把需要的数据封装到PageBean的对象中
public PageBean findCustomersByPage(int currPage) throws Exception {
//当前页 currPage
//自己指定每页显示多少数量的数据
int pageSize = 15;
//数据库查询出总记录数
int count = cd.findCustomersCount();
//转成double类型, 方便调用Math函数处理总页数,仅此而已
double totalCount = count;
//总页数
int totalPage = (int) Math.ceil(totalCount / pageSize);
//mysql中 使用limit查询的参数第一个为 起始记录数; 第二个为 每页的数量
int begin = (currPage - 1) * pageSize;
List list = cd.findCustomerByPage(begin , pageSize);
//封装
PageBean pg = new PageBean();
pg.setCurrPage(currPage);
pg.setPageSize(pageSize);
pg.setCount(count);
pg.setTotalPage(totalPage);
pg.setList(list);
return pg;
}
3, 前端代码
第${pageBean.currPage }/${pageBean.totalPage }页
每页显示${pageBean.pageSize }条
总记录数${pageBean.count }条
首页
前一页
${i }
${i }
${i }
${i }
...
...
${i }
${i }
...
...
${i }
${i }
后一页
末页
至此,分页查询功能全部完成 ,下面我们对分页进行抽取, 提取出更方便的分页工具类 ,读者可以直接调用此分页工具类, 通过设置路径即可完成分页功能
1, 工具类
package com.crud.utils;
import java.util.List;
public class PageBean {
//注意:1,在Action层设置url 即action层的访问路径 2,前端页面的取值为 ${pageBean.links }
private int currPage; //当前页
private int totalPage; //总页数
private int count; //总记录数
private int pageSize; //每页显示数量
private List list; //用于存储每页显示的记录
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String links;
public String getLinks(){
int currentPage = this.currPage;
StringBuffer sb = new StringBuffer();
sb.append("第"+this.getCurrPage()+"/"+this.getTotalPage()+"页 ");
sb.append("每页显示"+this.getPageSize()+"条 ");
sb.append("总记录数"+this.getCount()+"条 ");
if (this.getCurrPage() <= 1) {
currentPage = 1 ;
} else {
currentPage = this.currPage - 1;
}
if (this.getCurrPage() != 1) {
sb.append("首页 ");
sb.append("前一页 ");
}
if (this.getTotalPage() <= 9) {
for (int i = 1; i <= this.getTotalPage(); i++) {
if (this.getTotalPage()==i) {
sb.append(""+i+" ");
} else {
sb.append(""+i+" ");
}
}
}
if (this.getTotalPage() > 9) {
if (this.getCurrPage() <= 5) {
for (int i = 1; i <= 9; i++) {
if (this.getCurrPage() == i) {
sb.append(""+ i +" ");
} else {
sb.append(""+ i +" ");
}
}
sb.append("...");
} else {
if (this.getCurrPage() + 4 < this.getTotalPage()) {
sb.append("...");
for (int i = this.getCurrPage() - 4; i <= this.getCurrPage() + 4; i++) {
if (this.getCurrPage()==i) {
sb.append(""+ i +" ");
} else {
sb.append(""+ i +" ");
}
}
sb.append("...");
} else {
sb.append("...");
for (int i = this.getTotalPage() - 8; i <= this.getTotalPage(); i++) {
if (this.getCurrPage()==i) {
sb.append(""+ i +" ");
} else {
sb.append(""+ i +" ");
}
}
}
}
}
if (this.getCurrPage() >= this.getTotalPage() ) {
currentPage = this.getTotalPage();
} else {
currentPage = this.getCurrPage() + 1;
}
if (this.getCurrPage() != this.getTotalPage()) {
sb.append(" 后一页 ");
sb.append("末页");
}
links = sb.toString();
return links;
}
}
2, 业务层查询数据库, 进行封装
public PageBean findCustomerByPage(int currPage) throws Exception {
//当前页 currPage
//指定每页显示多少数量的数据
int pageSize = 15;
//数据库查询总记录数
int count = cd.findCustomerCount();
//总页数
double totalCount = count;
//总页数
int totalPage = (int) Math.ceil(totalCount / pageSize);
int begin = (currPage - 1) * pageSize;
List list = cd.findCustomerByPage(begin , pageSize);
PageBean pg = new PageBean();
pg.setCurrPage(currPage);
pg.setPageSize(pageSize);
pg.setCount(count);
pg.setTotalPage(totalPage);
pg.setList(list);
return pg;
}
3, action层只需对工具类进行地址设置
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
int currPage = Integer.parseInt(request.getParameter("currPage"));
CustomerService cs = new CustomerServiceImpl();
PageBean pb = null;
pb = cs.findCustomerByPage(currPage);
pb.setUrl("/test_geode/FindCustomerByPage"); //设置uri为你的项目访问相关action的地址即可
request.setAttribute("pageBean", pb);
request.getRequestDispatcher("/crud/showCustomer.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
4, 前端页面调用方式 : 在你想要调用的位置,书写下面一行代码即可实现
${pageBean.links }