分页查询

将所需数据建立一个实体类PageBean

需要获取当前页的值,并转为int类型

String currentPageStr=request.getParameter("currentPage");
Integer currentPage=Integer.parseInt(currentPageStr);

获取每页显示的条数,或手动设置

Integer currentCount=12;
//Integer currentCount=request.getParameter("");

调用service层方法

PageBean<Product> page=productService.getPage(currentPage,currentCount);

Service:

PageBean<Product> page=new PageBean<Product>();
        //目的:封装一个PageBean返回给客户端
        //封装当前页
        page.setCurrentPage(currentPage);
        //每页条数
        page.setCurrentCount(currentCount);
        //封装总条数,count(*)
        int totalCount=0;
        try {
            totalCount=productDao.getTotalCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        page.setTotalCount(totalCount);
        //总页数: 总条数/每页显示条数, *1.0向上取整
        int totalPage=(int) Math.ceil(totalCount*1.0/currentCount);
        page.setTotalPage(totalPage);
        //每页显示的条数
        // 起始页=(当前页-1)*每页显示条数
        // 1,0,12        2,12,12          3,24,12       S=(n-1)*a
        //计算起始页的索引
        int index=(currentPage-1)*currentCount;
        List<Product> list=null;
        //根据索引取值
        try {
            list=productDao.getProductByPage(index, currentCount);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        page.setList(list);
        return page;
        }

Dao层: select * from product limit ?,?

 

上一篇:LeetCode 1502. Can Make Arithmetic Progression From Sequence (判断能否形成等差数列)


下一篇:Day74 综合案例