JavaWeb页面数据分页

2018-10-27  本文已影响0人  _拾年丶

POJO:

package com.shop.pojo;

import java.io.Serializable;
import java.util.List;

public class PageBean<T> implements Serializable {

    // 分页中的数据
    private List<T> list;
    // 当前页
    private Integer currPage;
    // 每页条数
    private Integer pageSize;
    // 总页数(不能直接设置,只能通过计算得出结果)
    private Integer totalPage;
    // 总条数
    private Integer totalCount;
    
    // 无参构造器(在调用的时候,可以直接传递值进来,然后封装数据)
    public PageBean(List<T> list, Integer currPage, Integer pageSize, Integer totalCount) {
        super();
        this.list = list;
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }

    public PageBean() {
        super();
    }




    public List<T> getList() {
        return list;
    }



    public void setList(List<T> list) {
        this.list = list;
    }

    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return (int) Math.ceil((totalCount * 1.0 / pageSize));
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

}

Controller层:

    public String findProByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1. 获取 cid 和 currPage 参数
        // 当获取到 cid 的时候,就可以知道是哪个分类
        // 获取分类下所有的商品
        // 商品要展示,先告诉页面每页要展示多少条数据(写死)
        String cid = request.getParameter("cid");
        int currPage = Integer.parseInt(request.getParameter("currPage"));
        // 设置每个分页,固定显示商品的条数
        int pageSize = 12;
        
        // 2. 调用 ProductService 执行获取分页数据,需要返回一个 PageBean
        PageBean<Product> pb = null;
        
        try {
            pb = ps.findProByPage(currPage, cid, pageSize);
        
            System.out.println("分页对象:" + pb);
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        // 3. 将结果放入到 request 域中
        request.setAttribute("pb", pb);
        
        // 4. 请求转发页面
        return "product_list";
    }

Service层:

public PageBean<Product> findProByPage(int currPage, String cid, int pageSize) throws Exception {
        
        // 获取分页数据 --- 所有相关的商品
        List<Product> proList = pdao.findProByPage(currPage, cid, pageSize);
        
        // 总数量
        int totalCount = pdao.getTotalCount(cid); 
        
        return new PageBean<Product>(proList, currPage, pageSize, totalCount);
    }

Dao层:

public class ProductDaoImpl implements ProductDao {
    
    private QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public List<Product> findProByPage(int currPage, String cid, int pageSize) throws Exception {
        
        String sql = "select * from product where cid = ? limit ?, ?";
        
        return qr.query(sql, new BeanListHandler<>(Product.class), cid, (currPage - 1) * pageSize , pageSize);
        
        // limit(m, n)
        // m 是指记录开始的 index,从 0 开始,表示第一条记录
        // n 是指从第 m + 1 条开始,取 n 条。每页固定的条数。
    }
    
    

    @Override
    public int getTotalCount(String cid) throws Exception {
        
        String sql = "select count(*) from product where cid = ?";
        
        return ((Long) qr.query(sql, new ScalarHandler(), cid)).intValue();
    }
}
上一篇下一篇

猜你喜欢

热点阅读