Mybatis paging plugin - PageHelper quick start

        In actual work, there are many scenarios for list query, and we often need to do two steps: 1. Query the data corresponding to the number of pages required; 2. Count the total number of qualified data; and this will lead us to at least To write 2 sql to operate. This invisibly increases our workload. In addition, when there is a need to change, we need to change the two SQL at the same time, otherwise it will inevitably lead to inconsistencies in the results.

        Therefore, we need a simple and easy-to-use paging tool to help us complete this work. The requirements are clear, and how to implement each has its own advantages. And the pageHelper we want to talk about is one of the better components. Let's take a look at how to use it to improve work efficiency!

① Dependency introduction of pageHelper

Introduce pageHelper dependency in pom.xml:

(1) If it is springboot, you can directly introduce pagehelper-spring-boot-starter, which will save us many unnecessary configurations.

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.12</version>
</dependency>

(2) If it is an ordinary springmvc project, just introduce pageHelper.

<!-- pageHelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.10</version>
</dependency>

② pagehelper plug-in configuration

(1) If it is springboot, you can directly configure several configuration items:

# mybatis 相关配置
mybatis:
  #... 其他配置信息
  configuration-properties:
       helperDialect: mysql
       offsetAsPageNum: true
       rowBoundsWithCount: true
       reasonable: true
  mapper-locations: mybatis/mapper/*.xml

(2) If it is a common springmvc project configuration: mybatis-config.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
  <plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
      <!-- 该参数指明要连接的是哪个数据库 -->
      <property name="helperDialect" 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"/>
      <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
      <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
      <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
      <!-- 不理解该含义的前提下,不要随便复制该配置 -->
      <property name="params" value="pageNum=start;pageSize=limit;"/>
      <!-- 支持通过Mapper接口参数来传递分页参数 -->
      <property name="supportMethodsArguments" value="true"/>
      <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
      <property name="returnPageInfo" value="check"/>
    </plugin>
  </plugins>
</configuration>

And when configuring the data source, point the mybatis configuration file to the above file.

③ Use of pagehelper 

        When using it, you only need to call startPage to set the paging information before querying the list, and then you can use the paging function. 

public Object getUsers(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    // 不带分页的查询
    List<UserEntity> list = userMapper.selectAllWithPage(null);
    // 1. 可以将结果转换为 Page , 然后获取 count 和其他结果值
    com.github.pagehelper.Page listWithPage = (com.github.pagehelper.Page) list;
    System.out.println("listCnt:" + listWithPage.getTotal());
    // 2. 也可使用 PageInfo 来接收
    PageInfo<UserEntity> pageinfo = new PageInfo(list);
    return list;
}

         That is, when using it, you only need to declare the information to be paginated in advance, and the result will have pagination information. If you don't want to count, just check the paging data, then call: PageHelper.startPage(pageNum, pageSize, false); That's it, avoiding unnecessary consumption of count.

This paging plugin can be used in conjunction with the BootStrap Table component. In this case, when the server returns data to the client, it needs to include the two parameters of total and rows (and the key of the parameter must be total and rows). The specific implementation is in the Js table component Artifacts are introduced.

Note: The above configuration is only for pagehelper4.x version. If you are using pagehelper5.x version, you need to configure as follows

(1) If  mybatis uses the PageHelper pagination plug-in alone , you need to add the following code in the xml configuration:

<!-- 
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

(2) If it is an ssm project, mybatis is managed by spring's IOC container , you need to add the following code in spring's xml configuration (add in the creation factory):

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 注意其他配置 -->
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          <!--使用下面的方式配置参数,一行配置一个 -->
          <value>
            params=value1
          </value>
        </property>
      </bean>
    </array>
  </property>
</bean>

Note: This article refers to the Mybatis paging plugin: the use of pageHelper and its principle analysis  

Guess you like

Origin blog.csdn.net/weixin_52850476/article/details/124802877