java分页基础.docx
《java分页基础.docx》由会员分享,可在线阅读,更多相关《java分页基础.docx(11页珍藏版)》请在冰豆网上搜索。
![java分页基础.docx](https://file1.bdocx.com/fileroot1/2022-12/7/dfc78bd9-a160-4e17-acfd-c08ab00cfdd8/dfc78bd9-a160-4e17-acfd-c08ab00cfdd81.gif)
java分页基础
Dao层
IEmployeeDao.java
packagecn.itcast.dao;
importcn.itcast.entity.Employee;
importcn.itcast.utils.PageBean;
/**
*2.数据访问层,接口设计
*@authorJie.Yuan
*
*/
publicinterfaceIEmployeeDao{
/**
*分页查询数据
*/
publicvoidgetAll(PageBeanpb);
/**
*查询总记录数
*/
publicintgetTotalCount();
}
EmployeeDao.java
packagecn.itcast.dao.impl;
importjava.sql.SQLException;
importjava.util.List;
importmons.dbutils.QueryRunner;
importmons.dbutils.handlers.BeanListHandler;
importmons.dbutils.handlers.ScalarHandler;
importcn.itcast.dao.IEmployeeDao;
importcn.itcast.entity.Employee;
importcn.itcast.utils.JdbcUtils;
importcn.itcast.utils.PageBean;
/**
*2.数据访问层实现
*@authorJie.Yuan
*
*/
publicclassEmployeeDaoimplementsIEmployeeDao{
@Override
publicvoidgetAll(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);
}
}
@Override
publicintgetTotalCount(){
Stringsql="selectcount(*)fromemployee";
try{
//创建QueryRunner对象
QueryRunnerqr=JdbcUtils.getQueryRuner();
//执行查询,返回结果的第一行的第一列
Longcount=qr.query(sql,newScalarHandler());
returncount.intValue();
}catch(Exceptione){
thrownewRuntimeException(e);
}
}
}
Entity层
Employee.java
packagecn.itcast.entity;
/**
*1.实体类设计(因为用了DbUtils组件,属性要与数据库中字段一致)
*@authorJie.Yuan
*
*/
publicclassEmployee{
privateintempId;//员工id
privateStringempName;//员工名称
privateintdept_id;//部门id
publicintgetEmpId(){
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.java
packagecn.itcast.service;
importcn.itcast.entity.Employee;
importcn.itcast.utils.PageBean;
/**
*3.业务逻辑层接口设计
*@authorJie.Yuan
*
*/
publicinterfaceIEmployeeService{
/**
*分页查询数据
*/
publicvoidgetAll(PageBeanpb);
}
EmployeeService.java
packagecn.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();
@Override
publicvoidgetAll(PageBeanpb){
try{
employeeDao.getAll(pb);
}catch(Exceptione){
thrownewRuntimeException(e);
}
}
}
Servlet层
IndexServlet.java
packagecn.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.java
packagecn.itcast.utils;
importjavax.sql.DataSource;
importmons.dbutils.QueryRunner;
importcom.mchange.v2.c3p0.ComboPooledDataSource;
/**
*工具类
*1.初始化C3P0连接池
*2.创建DbUtils核心工具类对象
*@authorJie.Yuan
*
*/
publicclassJdbcUtils{
/**
*1.初始化C3P0连接池
*/
privatestaticDataSourcedataSource;
static{
dataSource=newComboPooledDataSource();
}
/**
*2.创建DbUtils核心工具类对象
*/
publicstaticQueryRunnergetQueryRuner(){
//创建QueryRunner对象,传入连接池对象
//在创建QueryRunner对象的时候,如果传入了数据源对象;
//那么在使用QueryRunner对象方法的时候,就不需要传入连接对象;
//会自动从数据源中获取连接(不用关闭连接)
returnnewQueryRunner(dataSource);
}
}
PageBean.java
packagecn.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/pageCount+1;
}
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;
}
}