首页 java分页基础

java分页基础

举报
开通vip

java分页基础Dao层IEmployeeDao.javapackagecn.itcast.dao;importcn.itcast.entity.Employee;importcn.itcast.utils.PageBean;/***2.数据访问层,接口设计*@authorJie.Yuan**/publicinterfaceIEmployeeDao{/***分页查询数据*/publicvoidgetAll(PageBeanpb);/***查询总记录数*/publicintgetTotalCount();}EmployeeDao.j...

java分页基础
Dao层IEmployeeDao.javapackagecn.itcast.dao;importcn.itcast.entity.Employee;importcn.itcast.utils.PageBean;/***2.数据访问层,接口设计*@authorJie.Yuan**/publicinterfaceIEmployeeDao{/***分页查询数据*/publicvoidgetAll(PageBeanpb);/***查询总 记录 混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载 数*/publicintgetTotalCount();}EmployeeDao.javapackagecn.itcast.dao.impl;importjava.sql.SQLException;importjava.util.List;importorg.apache.commons.dbutils.QueryRunner;importorg.apache.commons.dbutils.handlers.BeanListHandler;importorg.apache.commons.dbutils.handlers.ScalarHandler;importcn.itcast.dao.IEmployeeDao;importcn.itcast.entity.Employee;importcn.itcast.utils.JdbcUtils;importcn.itcast.utils.PageBean;/***2.数据访问层实现*@authorJie.Yuan**/publicclassEmployeeDaoimplementsIEmployeeDao{@OverridepublicvoidgetAll(PageBeanpb){//2.查询总记录数; 设置到pb对象中inttotalCount=this.getTotalCount();pb.setTotalCount(totalCount);/**问题:jsp页面,如果当前页为首页,再点击上一页报错!*       如果当前页为末页,再点下一页显示有问题!*解决:*   1.如果当前页<=0;   当前页设置当前页为1;*   2.如果当前页>最大页数; 当前页设置为最大页数*///判断if(pb.getCurrentPage()<=0){pb.setCurrentPage(1);            //把当前页设置为1}elseif(pb.getCurrentPage()>pb.getTotalPage()){pb.setCurrentPage(pb.getTotalPage());    //把当前页设置为最大页数}//1.获取当前页:计算查询的起始行、返回的行数intcurrentPage=pb.getCurrentPage();intindex=(currentPage-1)*pb.getPageCount();    //查询的起始行intcount=pb.getPageCount();              //查询返回的行数//3.分页查询数据; 把查询到的数据设置到pb对象中Stringsql="select*fromemployeelimit?,?";try{//得到Queryrunner对象QueryRunnerqr=JdbcUtils.getQueryRuner();//根据当前页,查询当前页数据(一页数据)ListpageData=qr.query(sql,newBeanListHandler(Employee.class),index,count);//设置到pb对象中pb.setPageData(pageData);}catch(Exceptione){thrownewRuntimeException(e);}}@OverridepublicintgetTotalCount(){Stringsql="selectcount(*)fromemployee";try{//创建QueryRunner对象QueryRunnerqr=JdbcUtils.getQueryRuner();//执行查询,返回结果的第一行的第一列Longcount=qr.query(sql,newScalarHandler());returncount.intValue();}catch(Exceptione){thrownewRuntimeException(e);}}}Entity层Employee.javapackagecn.itcast.entity;/***1.实体类设计(因为用了DbUtils组件,属性要与数据库中字段一致)*@authorJie.Yuan**/publicclassEmployee{privateintempId;      //员工idprivateStringempName;    //员工名称privateintdept_id;    //部门idpublicintgetEmpId(){returnempId;}publicvoidsetEmpId(intempId){this.empId=empId;}publicStringgetEmpName(){returnempName;}publicvoidsetEmpName(StringempName){this.empName=empName;}publicintgetDept_id(){returndept_id;}publicvoidsetDept_id(intdeptId){dept_id=deptId;}}Service层IEmployeeService.javapackagecn.itcast.service;importcn.itcast.entity.Employee;importcn.itcast.utils.PageBean;/***3.业务逻辑层接口设计*@authorJie.Yuan**/publicinterfaceIEmployeeService{/***分页查询数据*/publicvoidgetAll(PageBeanpb);}EmployeeService.javapackagecn.itcast.service.impl;importcn.itcast.dao.IEmployeeDao;importcn.itcast.dao.impl.EmployeeDao;importcn.itcast.entity.Employee;importcn.itcast.service.IEmployeeService;importcn.itcast.utils.PageBean;/***3.业务逻辑层,实现*@authorJie.Yuan**/publicclassEmployeeServiceimplementsIEmployeeService{//创建Dao实例privateIEmployeeDaoemployeeDao=newEmployeeDao();@OverridepublicvoidgetAll(PageBeanpb){try{employeeDao.getAll(pb);}catch(Exceptione){thrownewRuntimeException(e);}}}Servlet层IndexServlet.javapackagecn.itcast.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcn.itcast.entity.Employee;importcn.itcast.service.IEmployeeService;importcn.itcast.service.impl.EmployeeService;importcn.itcast.utils.PageBean;/***4.控制层开发*@authorJie.Yuan**/publicclassIndexServletextendsHttpServlet{//创建Service实例privateIEmployeeServiceemployeeService=newEmployeeService();//跳转资源privateStringuri;publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{try{//1.获取“当前页”参数; (第一次访问当前页为null)StringcurrPage=request.getParameter("currentPage");//判断if(currPage==null||"".equals(currPage.trim())){currPage="1";   //第一次访问,设置当前页为1;}//转换intcurrentPage=Integer.parseInt(currPage);//2.创建PageBean对象,设置当前页参数;传入service方法参数PageBeanpageBean=newPageBean();pageBean.setCurrentPage(currentPage);//3.调用service employeeService.getAll(pageBean);  //【pageBean已经被dao填充了数据】//4.保存pageBean对象,到request域中request.setAttribute("pageBean",pageBean);//5.跳转uri="/WEB-INF/list.jsp";}catch(Exceptione){e.printStackTrace(); //测试使用//出现错误,跳转到错误页面;给用户友好提示uri="/error/error.jsp";}request.getRequestDispatcher(uri).forward(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{this.doGet(request,response);}}Utils层JdbcUtils.javapackagecn.itcast.utils;importjavax.sql.DataSource;importorg.apache.commons.dbutils.QueryRunner;importcom.mchange.v2.c3p0.ComboPooledDataSource;/***工具类*1.初始化C3P0连接池*2.创建DbUtils核心工具类对象*@authorJie.Yuan**/publicclassJdbcUtils{/*** 1.初始化C3P0连接池*/privatestatic DataSourcedataSource;static{dataSource=newComboPooledDataSource();}/***2.创建DbUtils核心工具类对象*/publicstaticQueryRunnergetQueryRuner(){//创建QueryRunner对象,传入连接池对象//在创建QueryRunner对象的时候,如果传入了数据源对象;//那么在使用QueryRunner对象方法的时候,就不需要传入连接对象;//会自动从数据源中获取连接(不用关闭连接)returnnewQueryRunner(dataSource);}}PageBean.javapackagecn.itcast.utils;importjava.util.List;importcn.itcast.entity.Employee;/***封装分页的参数**@authorJie.Yuan**/publicclassPageBean{privateintcurrentPage=1;//当前页,默认显示第一页privateintpageCount=4; //每页显示的行数(查询返回的行数),默认每页显示4行privateinttotalCount;   //总记录数privateinttotalPage;   //总页数=总记录数/每页显示的行数 (1)privateListpageData;   //分页查询到的数据//返回总页数publicintgetTotalPage(){if(totalCount%pageCount==0){totalPage=totalCount/pageCount;}else{totalPage=totalCount/pageCount1;}returntotalPage;}publicvoidsetTotalPage(inttotalPage){this.totalPage=totalPage;}publicintgetCurrentPage(){returncurrentPage;}publicvoidsetCurrentPage(intcurrentPage){this.currentPage=currentPage;}publicintgetPageCount(){returnpageCount;}publicvoidsetPageCount(intpageCount){this.pageCount=pageCount;}publicintgetTotalCount(){returntotalCount;}publicvoidsetTotalCount(inttotalCount){this.totalCount=totalCount;}publicListgetPageData(){returnpageData;}publicvoidsetPageData(ListpageData){this.pageData=pageData;}}
本文档为【java分页基础】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_686908
暂无简介~
格式:doc
大小:35KB
软件:Word
页数:21
分类:
上传时间:2022-08-04
浏览量:0