首先:导入c3po-config.xml和DataSourceUtils.java

1.新建index.jsp页面写入
<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=1">分页展示所有电站</a><br>
2.在domain中新建一个PageBean.java类,获取set .get方法,其中获取总页数是需要计算的,所以不需要用set方法,使用构造函数
private List

list; //当前页内容 为一个泛型,需要将类型参数E添加至pageBean
private int currPage; //当前页面
private int pageSize; //每页显示当地条数
private int totalCount; //总条数
private int totalPage; //总页数

public List

getList() {
return list;
}

public void setList(List

list) {
this.list = list;
}


public int getCurrPage() {
return currPage;
}

public void setCurrPage(int currPage) {
this.currPage = currPage;
}


public int getPageSize() {
return pageSize;
}




public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}


public int getTotalCount() {
return totalCount;
}


public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}



/**
* 获取的总页数
* @param totalCount
*/


public int getTotalPage() {
return (int) Math.ceil(totalCount*1.0/pageSize);
}

public PageBean(List

list, int currPage, int pageSize, int totalCount) {
super();
this.list = list;
this.currPage = currPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
}
2.1在domain中新建一个StationInfo类,写入
package com.itheima.domain;


public class StationInfo {
private String stationId;
private String stationName;
public String getStationId() {
return stationId;
}
public void setStationId(String stationId) {
this.stationId = stationId;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
public String getAdjustType() {
return adjustType;
}
public void setAdjustType(String adjustType) {
this.adjustType = adjustType;
}
public String getNormalZ() {
return normalZ;
}
public void setNormalZ(String normalZ) {
this.normalZ = normalZ;
}
public String getDeadZ() {
return deadZ;
}
public void setDeadZ(String deadZ) {
this.deadZ = deadZ;
}
public String getRiverId() {
return riverId;
}
public void setRiverId(String riverId) {
this.riverId = riverId;
}
private String adjustType;
private String normalZ;
private String deadZ;
private String riverId;
}



3.新建一个showStationByPageServlet的servlet,dopost()和doget()方法中写入
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//0.设置编码

//1.获取第一页
int currPage =Integer.parseInt(request.getParameter("currPage"));

//固定每页的条数
int pageSize =5;
//2.调用service完成分页,返回pagebean
PageBean

bean=null; try { bean = new StationService().showStationByPage(currPage,pageSize); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //3.将pagebean放入request域中,请求转发station_page.jsp request.setAttribute("Station", bean); request.getRequestDispatcher("/station_page.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } 4.service包中新建StationService类,写入showStationByPage方法 /** * 分页查询 * @param currPage * @param pageSize * @return * @throws SQLException */ public PageBean

showStationByPage(int currPage, int pageSize) throws SQLException { // TODO 自动生成的方法存根 //查询当前页数据 limit m,n StationDao dao=new StationDao(); List

list=dao.findStationByPage(currPage,pageSize); int totalCount=dao.getCount(); //插叙总条数 return new PageBean<>(list, currPage, pageSize, totalCount); } 5.在Dao包中新建StationDao类,写入findStationByPage和getCount方法 //查询第几页数据 public List

findStationByPage(int currPage, int pageSize) throws SQLException { // TODO 自动生成的方法存根 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from STATIONINFO limit ?,?"; return qr.query(sql, new BeanListHandler<>(StationInfo.class), (currPage-1)*pageSize,pageSize); } //获取总条数 public int getCount() throws SQLException { // TODO 自动生成的方法存根 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select count(*) from STATIONINFO"; return ((Long)qr.query(sql, new ScalarHandler())).intValue(); } private Object Long(Object query) { // TODO 自动生成的方法存根 return null; } 6.开始将数据传输送到station_page.jsp页面中,新建station_page.jsp页面,然后写入 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<base href="<%=basePath%>">
<title>My JSP 'station_page.jsp' starting page</title>
<meta>
<meta>
<meta>
<meta>
<meta>

<style type="text/css">
a{
padding:5px;
}
</style>


<table border="1" align="center" width="88%"><tbody><tr><th>stationId</th><th>stationName</th><th>adjustType</th><th>normalZ</th><th>deadZ</th><th>riverId</th></tr><tr><th width="8%">${p.stationId }</th><th width="8%">${p.stationName }</th><th width="8%">${p.adjustType }</th><th width="8%">${p.normalZ }</th><th width="8%">${p.deadZ }</th><th width="8%">${p.riverId }</th></tr></tbody></table>
<center>



<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=1">[首页]</a>
<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=$%7bStation.currPage-1%7d">【上一页】 </a>






${n }



<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=$%7bn%20%7d">${n }</a>









${n }



<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=$%7bn%20%7d">${n }</a>








${n }



<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=$%7bn%20%7d">${n }</a>





<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=$%7bStation.currPage+1%7d">【下一页】 </a>
<a href="$%7bpageContext.request.contextPath%20%7d/showStationByPage?currPage=$%7bStation.totalPage%7d">【尾页】</a>
第${Station.currPage }页/共${Station.totalPage }页
</center>