pagehelper插件使用

       前言

        pagehelper分页插件,用于对集合数据进行分页处理。


pagehelper简介

官方网址

MyBatis 分页插件 PageHelper。

  • 物理分页

    支持12种数据库Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等

  • 支持多种分页方式

    支持常见的RowBounds(PageRowBounds),PageHelper.startPage 方法调用,Mapper 接口参数调用

  • QueryInterceptor 规范

    使用 QueryInterceptor 规范,开发插件更轻松。


pagehelper插件使用
  • 引入分页插件

    版本可以在Maven仓库中进行下载或在官方来查看最新版本。如果引入版本与现在使用的框架版本相差太大会产生错误信息。导致项目运行问题。

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>最新版本</version>
    </dependency>
    ----------------------------------
    //在SpringBoot中使用可以引入以下依赖,其中包括了上面的依赖,而上面的依赖不需要再引用
    <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper-spring-boot-starter</artifactId>
          <version>1.2.12</version>
      </dependency>
  • 配置拦截器插件

    特别注意,新版拦截器是 com.github.pagehelper.PageInterceptor。 com.github.pagehelper.PageHelper 现在是一个特殊的 dialect 实现类,是分页插件的默认实现类,提供了和以前相同的用法。

    upload successful

    Spring中配置文件

          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="mapperLocations">
        <array>
          <value>classpath:mapper/*.xml</value>
        </array>
      </property>
      <property name="typeAliasesPackage" value="com.isea533.ssm.model"/>
      <property name="plugins">
        <array>
          <bean class="com.github.pagehelper.PageHelper">
            <property name="properties">
              <value>
                dialect=hsqldb
                reasonable=true
              </value>
            </property>
          </bean>
        </array>
      </property>
    </bean>

    SpringBoot中配置

    upload successful

    常用的基本参数设置

    <plugins>
     <!-- com.github.pagehelper为PageHelper类所在包名 -->
     <plugin interceptor="com.github.pagehelper.PageHelper">
         <property name="dialect" value="mysql"/>
         <!-- 该参数默认为false -->
         <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
         <!-- 和startPage中的pageNum效果一样-->
         <property name="offsetAsPageNum" value="true"/>
         <!-- 该参数默认为false -->
         <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
         <property name="rowBoundsWithCount" value="true"/>
         <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
         <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
         <property name="pageSizeZero" value="true"/>
         <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
         <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
         <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
         <property name="reasonable" value="true"/>
     </plugin>
     </plugins>
    • 增加dialect属性,使用时必须指定该属性,可选值为oracle,mysql,mariadb,sqlite,hsqldb,postgresql,没有默认值,必须指定该属性。
    • 增加offsetAsPageNum属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。当该参数设置为true时,使用RowBounds分页时,会将offset参数当成pageNum使用,可以用页码和页面大小两个参数进行分页。
    • 增加rowBoundsWithCount属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。当该参数设置为true时,使用RowBounds分页会进行count查询。
    • 增加pageSizeZero属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。当该参数设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是Page类型)。
    • 增加reasonable属性,默认值为false,使用默认值时不需要增加该配置,需要设为true时,需要配置该参数。具体作用请看上面配置文件中的注释内容。
  • 如何在代码中使用

    配置完成后再调用类中配置分页属性:

    //查询所需数据条件
    List<TbItem> list = ibItemMapper.selectByExample(new TbItemExample());
    //设置分页信息(第几页,每页数量)
    PageHelper.startPage(pageNum, pageSize);
    //取记录总条数
    PageInfo<TbItem> pageInfo = new PageInfo<>(list); 
    long sum = pageInfo.getTotal();
其他几种查询方法:

   //第一种,RowBounds方式的调用
  List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10));

  //第二种,Mapper接口方式的调用,推荐这种使用方式。
  PageHelper.startPage(1, 10);
  List<Country> list = countryMapper.selectIf(1);

  //第三种,Mapper接口方式的调用,推荐这种使用方式。
  PageHelper.offsetPage(1, 10);
  List<Country> list = countryMapper.selectIf(1);

  //第四种,参数方法调用
  //存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
  public interface CountryMapper {
      List<Country> selectByPageNumSize(
              @Param("user") User user,
              @Param("pageNum") int pageNum,
              @Param("pageSize") int pageSize);
  }
  //配置supportMethodsArguments=true
  //在代码中直接调用:
  List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);

  //第五种,参数对象
  //如果 pageNum 和 pageSize 存在于 User 对象中,只要参数有值,也会被分页
  //有如下 User 对象
  public class User {
      //其他fields
      //下面两个参数名和 params 配置的名字一致
      private Integer pageNum;
      private Integer pageSize;
  }
  //存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
  public interface CountryMapper {
      List<Country> selectByPageNumSize(User user);
  }
  //当 user 中的 pageNum!= null && pageSize!= null 时,会自动分页
  List<Country> list = countryMapper.selectByPageNumSize(user);

  //第六种,ISelect 接口方式
  //jdk6,7用法,创建接口
  Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() {
      @Override
      public void doSelect() {
          countryMapper.selectGroupBy();
      }
  });
  //jdk8 lambda用法
  Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(()-> countryMapper.selectGroupBy());

  //也可以直接返回PageInfo,注意doSelectPageInfo方法和doSelectPage
  pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
      @Override
      public void doSelect() {
          countryMapper.selectGroupBy();
      }
  });
  //对应的lambda用法
  pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(() -> countryMapper.selectGroupBy());

  //count查询,返回一个查询语句的count数
  long total = PageHelper.count(new ISelect() {
      @Override
      public void doSelect() {
          countryMapper.selectLike(country);
      }
  });
  //lambda
  total = PageHelper.count(()->countryMapper.selectLike(country));