商品增删改查之分页查询

商品增删改查之分页显示

首先需要创建一个javaBean

public class PageBean {
    
    private Integer currentPage;//当前页数      
    
    private Integer pagesize;//每页显示的数目
    
    private Integer totalCount;//总条目数
    
    private Integer totalPage;//总页数
    
    private List list;//用来封装商品集合
    
    public List getList() {
        return list;
    }

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

    

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPagesize() {
        return pagesize;
    }

    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

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

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

}

然后需要请求servlet用来处理

public String findByCid(HttpServletRequest request,
            HttpServletResponse response) {
        try {
            // 接受参数
            String cid = request.getParameter("cid");
            Integer currentPage = Integer.parseInt(request
                    .getParameter("currentPage"));
            // 调用业务层
            ProductService productService = (ProductService) BeanFactory.getBean("productService");
            PageBean pageBean = productService.findByPageCid(cid,
                    currentPage);
            // 将数据放入域中
            request.setAttribute("pageBean", pageBean);
            return "/jsp/product_list.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }

然后调用service来处理封装数据

/*
     * 用于分页查询商品分类
     * 并且在业务层封装pageBean的信息
     */
    public PageBean findByPageCid(String cid, Integer currentPage) throws SQLException {
        PageBean pageBean = new PageBean();
        //设置当前页数
        pageBean.setCurrentPage(currentPage);
        //设置每页读取的商品数目
        Integer pagesize = 12;
        pageBean.setPagesize(pagesize);
        //设置总的记录数
        ProductDao productDao = (ProductDao) BeanFactory.getBean("productDao");
        Integer count = productDao.findCount(cid);
        pageBean.setTotalCount(count);
        //设置总页数
        double tc = count;
        Double totalPage = Math.ceil(tc/pagesize);
        pageBean.setTotalPage(totalPage.intValue());
        
        //设置每页显示数据的商品的集合        1 10   2 11-20 
        int begin = (currentPage-1)*pagesize;
        List list = productDao.findByCid(cid, begin, pagesize);
        pageBean.setList(list);
        
        return pageBean;
        
    }

调用DAO来查询数据库

    /*
     * 分页查询商品
     */
    public List findByCid(String cid, int begin, Integer pagesize) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        String sql = "select * from product where pflag = ? and cid = ? order by pdate desc limit ?,?";
        List list = queryRunner.query(sql, new BeanListHandler(Product.class),0,cid,begin,pagesize);
        return list;
    }

将封装的数据返回并在jsp中获取


    

你可能感兴趣的:(商品增删改查之分页查询)