SentralLiu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.1 概念

​ 分页是Web应用程序非常重要的一个技术。数据库中的数据可能是成千上万的,不可能把这么多的数据一次显示在浏览器上面。一般根据每行数据在页面上所占的空间设置每页显示若干行,比如一般20行是一个比较理想的显示状态。

1.2 分页实现思路

对于海量的数据查询,需要多少就取多少,显然是最佳的解决方法,假如某个表中有200万条记录,第一页取前20条,第二页取21~40条记录。

select  * from 表名 limit  0,20;//第一页
select  * from 表名 limit  20,20;//第二页
select  * from 表名 limit  40,20;//第三页

1.3 分页代码实现

步骤:

  1. 创建实体类Page 属性有:currentPage,pageSize,pageCount,totalCount,List
  2. 控制层获取Page对象,反馈到前端
  3. 业务层封装Page实体类属性值
  4. dao层完成获取总条数,分页查询SQL
  5. 前端分页展示
  • 分页需要的数据:
  • 1.当前页(前端传)
  • 2.页大小(4条)
  • 3.总条数(数据库求的)
  • 4.显示数据(通过SQL求)
  • 5.总页数(自己计算)
  • 总页数:总条数/页大小–判断 例如共12条–3页 13条—4页
  • long pageCount = (long)Math.ceil((double)totalCount/pageSize);

1.4分页实体类Page

public class Page <T> {
    private long currentPage;   //当前页
    private long pageSize;      //页大小
    private long pageCount;     //页数量-最好用long
    private long totalCount;    //总条数-最好用long
    private List<T> list;       //存内容
}

1.5分页控制层

@WebServlet("/show")
public class EmpController extends HttpServlet {
    private EmpService empService = new EmpServiceImpl();  //员工业务对象
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //==============分页处理=================
        String current = req.getParameter("currentPage");
        Page<Emp> page = empService.getPage(current);  //获取封装的page对象
              req.setAttribute("pg",page);
        req.getRequestDispatcher("/show2.jsp").forward(req,resp);
    }
}

1.6业务层分页封装

public class EmpServiceImpl implements EmpService {
    private EmpDao empDao = new EmpDaoImpl();
    @Override
    public Page<Emp> getPage(String current) {
        Page<Emp> page = new Page<>();  //实例化Page对象后,给所有属性赋值
        long currentPage = 1;  //当前页初始为1
        if(current!=null){  //判断当前页是否为null,不为null则赋值
            currentPage = Integer.parseInt(current);
        }
        page.setCurrentPage(currentPage);  //存当前页
        long pageSize = 4;
        page.setPageSize(pageSize);  //页大小,默认为每页4条
        long totalCount = empDao.getTotalCount();  //数据库中获取总条数
        page.setTotalCount(totalCount);  //存总条数
        long pageCount = (long)Math.ceil((double)totalCount/pageSize);  //求总页数
        page.setPageCount(pageCount);

        long startIndex = (currentPage-1)*pageSize;   //起始下标=(当前页-1)*页大小
             List<Emp> list = empDao.selectPage(startIndex,pageSize);  //limit 起始下标,页大小
             page.setList(list);

        return page;
    }
}

1.7分页DAO层处理

public class EmpDaoImpl implements EmpDao {
    private QueryRunner runner = new QueryRunner();
    //获取总条数
    @Override
    public long getTotalCount() {
        String sql = "select count(*) from emp";
        //new ScalarHandler<>(): 用于处理聚合函数的对象
        try {
            //返回Long-->long
            return runner.query(DBUtils.getConnection(),sql,new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public List<Emp> selectPage(long startIndex, long pageSize) {
        String sql= "select * from emp limit ?,?";
        try {
            return runner.query(DBUtils.getConnection(),sql,
                    new BeanListHandler<>(Emp.class),startIndex,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

}

1.8分页前端展示

<a href="${pageContext.request.contextPath}/emp/safe/show?currentPage=1">首页</a>
<c:if test="${pg.currentPage!=1}">
    <%-- 当当前页不为第一页时,才有上一页 --%>
    <a href="${pageContext.request.contextPath}/emp/safe/show?currentPage=${pg.currentPage-1}">上一页</a>
</c:if>
<c:if test="${pg.currentPage!=pg.pageCount}">
    <%-- 当当前页不为尾页时,才有下一页 --%>
    <a href="${pageContext.request.contextPath}/emp/safe/show?currentPage=${pg.currentPage+1}">下一页</a>
</c:if>
<%-- 尾页:就是页数量 --%>
<a href="${pageContext.request.contextPath}/emp/safe/show?currentPage=${pg.pageCount}">尾页</a>
共${pg.currentPage}/${pg.pageCount}页

总结:分页要搞明白到底需要什么数据

posted on 2021-12-11 09:25  SentralLiu  阅读(100)  评论(0)    收藏  举报