1.PageBean
/**
* 这是一个用于封装了分页的数据
* 包含:
* 该页的学生集合数据
* 总的记录数
* 总的页数
* 当前页
* 每页显示的记录数
* @author acer
*
*/
public class PageBean
private int currentPage;//当前页
private int totalPage;//总页数
private int pageSize;//每页的记录数
private int totalSize;//总的记录数
private List
public int getCurrentPage() {
return currentPage;
}
注意给出setter getter方法
2.Dao接口
List
/**
* 查询总的学生记录数
*/
int findCount() throws SQLException;
3.Dao实现
@Override
public List
String sql = "select * from stus limit ? offset ?";
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSouece());
return runner.query(sql, new BeanListHandler
}
@Override
public int findCount() throws SQLException {
String sql = "select count(*) from stus";
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSouece());
Long result = (Long)runner.query(sql, new ScalarHandler());
return result.intValue();
}
4.service接口
/**
* 查询当页数据
* @param currentPage
* @return
* @throws SQLException
*/
PageBean findStudentByPage(int currentPage) throws SQLException;
5.service实现
public PageBean findStudentByPage(int currentPage) throws SQLException {
// 封装分页的该页数据
PageBean
int pageSize = StudentDao.PAGE_SIZE;
pageBean.setCurrentPage(currentPage);//设置当前页
pageBean.setPageSize(pageSize);//设置每页显示多少记录
StudentDao dao = new StudentDaoImpl();
List
pageBean.setList(list);//设置这一页的学生数据
//总的记录数,总的页数
int count = dao.findCount();
pageBean.setTotalSize(count);//设置总的记录数
pageBean.setTotalPage(count % pageSize==0 ? count / pageSize : (count / pageSize) + 1);//总页数
return pageBean;
}
6.servlet控制层
/**
* 分页显示学生列表的servlet
*/
public class StuListPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.获取需要显示的页码数
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
//2.根据指定的页面,去获取该页面的数据
StudentService service = new StudentServiceImpl();
PageBean pageBean = service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3.跳转页面
request.getRequestDispatcher("list_page.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
7.前台显示:
${stu.sid }
${stu.sname }
${stu.gender }
${stu.phone }
${stu.birthday }
${stu.hobby }
${stu.info }
更新 href="#" οnclick="doDelete(${stu.sid })">删除