zoukankan      html  css  js  c++  java
  • MyBatis(十一)扩展:分页插件PageHelper

    一、PageHelper 插件介绍

      PageHelper是MyBatis中非常方便的第三方分页插件。

      官方文档:https://github.com/pagehelper/MybatisPageHelper/blob/master/README_zh.md

      我们可以对照官方文档的说明,快速的使用插件


    二、使用步骤

      1、导入相关 jar 包

        导入相关包pagehelper-x.x.x.jar jsqlparser-0.9.5.jar

        添加 Maven 依赖:

            <!--MyBatis-分页插件-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.0.0</version>
            </dependency>

      2、配置插件信息

        在 MyBatis 的全局配置文件中配置分页插件

        

        <!--
            注册插件
        -->
        <plugins>
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
                <property name="param1" value="value"/>
            </plugin>
        </plugins>

      3、进行分页查询

        ① 使用PageHelper 提供的方法进行分页

        ② 可以使用更强大的 PageInfo 封装返回结果

         @Test
         public void testPageHelper() throws IOException {
              //1、获取 sqlSessionFactory
              SqlSessionFactory sqlSessionFactory = getsqlSessionFactory();
    
              //2、获取 sqlSession 实例,能直接执行已经映射的 SQL 语句
              SqlSession sqlSession = sqlSessionFactory.openSession();
    
              try {
                   //3、获取接口的实现类对象
                   EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
    
                   //设置分页
                   Page<Object> page = PageHelper.startPage(2, 2);
    
                   List<Employee> emps = employeeMapper.getEmps();
                   emps.forEach(System.out::println);
    
                   System.out.println("当前页码:" + page.getPageNum());
                   System.out.println("总记录数:" + page.getTotal());
                   System.out.println("每页的记录数:" + page.getPageSize());
                   System.out.println("总页码:" + page.getPages());
                   System.out.println(page);
    
                   //使用pageInfo包装数据
                   PageInfo<Employee> pageInfo = new PageInfo<>(emps);
    
                   System.out.println("当前页码:" + pageInfo.getPageNum());
                   System.out.println("是否第一页:" + pageInfo.isIsFirstPage());
                   System.out.println("是否最后页:" + pageInfo.isIsLastPage());
    
                   // 连传入要连续显示几页
                   PageInfo<Employee> pageInfo2 = new PageInfo<>(emps, 5);
                   int[] nums = pageInfo2.getNavigatepageNums();
                   System.out.println("连续显示的页码:" + nums);
                   System.out.println(Arrays.toString(nums));
              } finally {
                   sqlSession.close();
              }
         }

        运行结果:

     

  • 相关阅读:
    faster with MyISAM tables than with InnoDB or NDB tables
    w-BIG TABLE 1-toSMALLtable @-toMEMORY
    Indexing and Hashing
    MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
    controlling the variance of request response times and not just worrying about maximizing queries per second
    Variance
    Population Mean
    12.162s 1805.867s
    situations where MyISAM will be faster than InnoDB
    1920.154s 0.309s 30817
  • 原文地址:https://www.cnblogs.com/niujifei/p/15312245.html
Copyright © 2011-2022 走看看