1、查询信息的封装
public class QueryInfo {
private int currentPage = 1;
private int pageSize = 1;
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() {
startIndex = (this.currentPage - 1) * this.pageSize;
return startIndex;
}
}
2、查询结果的封装
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;
}
}
3、前端页面显示的封装
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;
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;
return this.pagebar;
}
}
4、分页查询的实现
public class StudentDao {
public QueryResult query(QueryInfo info) {
try {
QueryResult qr = new QueryResult();
String sql = "select * from student limit ?,?";
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
List<Student> lists = (List<Student>) runner.query(sql,
new Object[] { info.getStartIndex(), info.getPageSize() },
new BeanListHandler(Student.class));
qr.setList(lists);
System.out.println(lists.get(0).getId());
sql = "select count(*) from student";
Object[] data = (Object[]) runner.query(sql, new ArrayHandler() {
});
qr.setTotalrecord(((Long) data[0]).intValue());
return qr;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
5、Action的实现
public class PageServlet extends HttpServlet {
private StudentSerivce serivce = new StudentSerivce();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// 用于封装请求信息到QueryInfo
QueryInfo info = WebUtils.request2Bean(request, QueryInfo.class);
PageBean pageBean = serivce.query(info);
request.getSession().setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/FenYe.jsp").forward(request, response);
}
}
6、运行截图
7、源码下载