web层servlet代码:
package com.zys.web;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zys.domain.Product;
import com.zys.service.ProductListService;
import com.zys.vo.PageBean;
public class ProductListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//将请求传递到dao层,从数据库获取所有的商品信息
ProductListService service = new ProductListService();
//模拟当前是第一页
int currentPage = 1;
//认为每页显示12条
int currentCount = 12;
PageBean<Product> pageBean = null;
try {
pageBean = service.findPageBean(currentPage,currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
//将获取到的数据保存到request域
request.setAttribute("pageBean", pageBean);
//转发到对应的jsp页面
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
service层代码:
package com.zys.service;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zys.dao.ProductListDao;
import com.zys.domain.Product;
import com.zys.vo.PageBean;
public class ProductListService {
//分页操作
public PageBean findPageBean(int currentPage, int currentCount) throws SQLException {
ProductListDao dao = new ProductListDao();
// 目的:想办法封装一个PageBean 并返回
PageBean pageBean = new PageBean();
//1、当前页private int currentPage;
pageBean.setCurrentPage(currentPage);
//2、当前页显示的条数private int currentCount;
pageBean.setCurrentCount(currentCount);
//3、总条数private int totalCount;
int totalCount = dao.getTotalCount();
pageBean.setTotalCount(totalCount);
//4、总页数private int totalPage;
/*
* 总条数 当前页显示的条数 总页数
* 10 4 3
* 11 4 3
* 12 4 3
* 13 4 4
*
* 公式:总页数=Math.ceil(总条数/当前显示的条数)
*
*/
//ceil向上取整 floor向下取整
//1.0*是因为两个整型相除,得到的还是整型,如果此时向上取整得不到正确的数,所以在这里先*1.0变成double型
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、每页显示的数据private List<T> productList = new ArrayList<T>();
/*
*页数与limit起始索引的关系
*例如:每页显示4条
*页数 起始索引 每页显示条数
* 1 0 4
* 2 4 4
* 3 8 4
* 4 12 4
* 1页 limit 0,4
* 2页 limit 4,4
* 3页 limit 8,4
*
* 索引index = (当前页数-1)*每页显示的条数
*
*/
int index = (currentPage-1)*currentCount;
List<Product> productList = dao.findProductListForPageBean(index,currentCount);
//封装数据到pageBean
pageBean.setProductList(productList);
return pageBean;
}
}
dao层代码:
package com.zys.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.zys.domain.Product;
import com.zys.utils.DataSourceUtils;
public class ProductListDao {
//获得全部的商品条数
public int getTotalCount() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from product";
Long query = (Long) runner.query(sql, new ScalarHandler());
return query.intValue();
}
//获得分页的商品数据
public List<Product> findProductListForPageBean(int index, int currentCount) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product limit ?,?";
return runner.query(sql, new BeanListHandler<Product>(Product.class), index,currentCount);
}
}
对于商品显示页面一般要定义的实体类变量:

为了PageBean实体类达到通用,所以在集合后面加<T>泛型
为了方便调用时使用,在实体类内提前初始化

分页:limit(起始索引,显示条数);

网友评论