标签:tps .com rac 接口 调用 utils getc image row
分页查询 分析图
分页javaBean 设计
public class PageBean<T> { private int currntPage = 1; // 当前页, 默认显示第一页 private int pageCount = 2; // 查询返回的行数(每页显示的行数),默认每页显示3行 private int totalCount; // 总记录数 private int totalPage; // 总页数 = 总记录数/每页显示的行数(+1) private List<T> pageData; // 分页查询的数据,运用泛型,可以重复利用 public int getTotalPage() { if (totalCount % pageCount == 0) { totalPage = totalCount / pageCount; } else { totalPage = totalCount / pageCount + 1; } return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrntPage() { return currntPage; } public void setCurrntPage(int currntPage) { this.currntPage = currntPage; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getPageData() { return pageData; } public void setPageData(List<T> pageData) { this.pageData = pageData; } |
分页 servlet
// 创建service实例 private StuService stuService = new StuService(); String uri; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 获取当前页参数,第一次访问为空 String currPage = request.getParameter("currenPage"); // 判断,如果为空,则设置为1 if (currPage == null || "".equals(currPage.trim())) { currPage = "1"; } // 转换 Integer currenPage = Integer.valueOf(currPage); // 创建pageBean对象 PageBean<Student> pageBean = new PageBean<Student>(); // 设置当前页参数 pageBean.setCurrntPage(currenPage); // 调用service,传入service方法参数 stuService.getAll(pageBean); // 保存pageBean对象到request中 request.setAttribute("pageBean", pageBean); // 跳转 uri = "/WEB-INF/list.jsp"; } catch (Exception e) { // 出现错误跳转错误页面 uri="/error.jsp"; } request.getRequestDispatcher(uri).forward(request, response); } |
数据访问层
/** * 数据访问层,实现接口 * */ public class StuDao implements IStuDao { /* * 分页查询 */ @Override public void getAll(PageBean<Student> pb) { // 查询总记录数 int totalCount = this.getTotalCount(); //设置到pb对象中 pb.setTotalCount(totalCount); //判断 if(pb.getCurrntPage()<=0){ pb.setCurrntPage(1); }else if(pb.getCurrntPage()>pb.getTotalPage()){ pb.setCurrntPage(pb.getTotalPage()); } // 获取当前页 int currentPage = pb.getCurrntPage(); //计算查询的起始行 int index = (currentPage - 1) * pb.getPageCount(); //返回的行数 int count = pb.getPageCount(); // 分页查询数据,把查询到的数据设置到pb对象中 String sql = "select * from student limit ?,?"; try { // 得到queryRunner对象 QueryRunner qr = JdbcUtils.getQueryRunner(); // 根据当前页查询当前页数据 List<Student> pageData = qr.query(sql, new BeanListHandler<Student>(Student.class), index, count); // 设置到pb对象中 pb.setPageData(pageData); } catch (SQLException e) { e.printStackTrace(); } } /* * 查询总记录数 */ @Override public int getTotalCount() { String sql = "select count(*) from student"; int coun = 0; try { QueryRunner qr = JdbcUtils.getQueryRunner(); Long count = qr.query(sql, new ScalarHandler<Long>()); coun = count.intValue(); } catch (SQLException e) { e.printStackTrace(); } return coun; } } |
标签:tps .com rac 接口 调用 utils getc image row
原文地址:http://www.cnblogs.com/lb1988/p/6146698.html