zoukankan      html  css  js  c++  java
  • 分页--PageHelper的使用

    POM依赖

    <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.2.0</version>
     </dependency>

    Mybatis配置文件

    <plugins>
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
                <property name="helperDialect" value="mysql"/>
            </plugin>
        </plugins>

    Servlet代码示例

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String page = request.getParameter("page");
            String size = request.getParameter("size");
            StudentService studentService = new StudentServiceImpl();
            PageHelper.startPage(page == null ? 1:Integer.parseInt(page),size == null ? 4:Integer.parseInt(size));
            List<Student> list = studentService.findAllStudent();
            PageInfo pageInfo = PageInfo.of(list);
            response.getWriter().println(new ObjectMapper().writeValueAsString(pageInfo));
        }

    1、PageHelper的优点是,分页和Mapper.xml完全解耦。实现方式是以插件的形式,对Mybatis执行的流程进行了强化,添加了总数count和limit查询。属于物理分页。

    2、Page page = PageHelper.startPage(pageNum, pageSize, true); - true表示需要统计总数,这样会多进行一次请求select count(0); 省略掉true参数只返回分页数据。

    参考:

    https://www.cnblogs.com/kangoroo/p/7998433.html

  • 相关阅读:
    查询长事务和SQL执行等待间隔时间
    一条诡异的SQL
    查询表的使用空间和可用空间
    查询SQL Server存储过程的执行信息
    清理大批量数据例子
    sql server 清理日志存储过程
    BCP 实例
    SQL Server 查询Job中的存储过程
    下车扫描五次优化全过程
    empty、isset和is_null的比较
  • 原文地址:https://www.cnblogs.com/FivePointOne/p/13879807.html
Copyright © 2011-2022 走看看