Learning Spring Boot: (8) Mybatis uses the paging plugin PageHelper

First of all, Mybqtis can implement paging through SQL. It is very simple, just add limit #{currIndex} , #{pageSize}it after the query SQL.

This article mainly introduces the use of interceptors to implement paging.

Implementation principle

The interceptor implements the method of intercepting all queries that require paging, and uses the obtained paging related parameters to uniformly add limit paging related statements after the SQL statement, so as to achieve the purpose of SQL paging. It is rarely written, and it has less intrusion to SQL, so it is recommended to use it.

step

add dependencies

<pagehelper.version>1.2.3</pagehelper.version>

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>${pagehelper.version}</version>
</dependency>

configuration file

Add the configuration information of pagehelper to the system configuration file:

pagehelper:
  helper-dialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

For pagehelperthe configuration parameters, query:
1. helperDialect: The paging plug-in will automatically detect the current database link and automatically select the appropriate paging method.
You can configure helperDialectproperties to specify which dialect the pagination plugin uses. When configuring, you can use the following abbreviated values:
oracle, mysql, mariadb, sqlite, hsqldb, postgresql, db2, sqlserver, informix, h2, sqlserver2012, derby
Special attention: when using the SqlServer2012 database, you need to manually specify it as sqlserver2012, otherwise, the SqlServer2005 method will be used for paging.
You can also implement AbstractHelperDialect, and then configure this property as the fully qualified name of the implementation class to use a custom implementation method.

  1. offsetAsPageNum: The default value false, this parameter is valid when used RowBoundsas a paging parameter.
    When this parameter is set trueto , RowBoundsthe offsetparameter in will pageNumbe used as , and the two parameters of page number and page size can be used for paging.

  2. rowBoundsWithCount: The default value false, this parameter is valid when used RowBoundsas a paging parameter.
    When this parameter is trueset to , using RowBoundspagination will do a count query.

  3. pageSizeZero: The default value is false, when this parameter is set trueto , if pageSize=0or , all the results RowBounds.limit = 0will be queried (equivalent to not executing the paging query, but the returned results are still of Pagetype ).

  4. reasonable: Paging rationalization parameter, the default value is false. When this parameter is set trueto , pageNum<=0the first page will be queried,
    pageNum>pages(when the total number is exceeded), the last page will be queried. By default false, the query is performed directly according to the parameters.

  5. params: In order to support startPage(Object params)the method, this parameter is added to configure the parameter mapping, which is used to get the value from the object according to the attribute name. It
    can be configured pageNum,pageSize,count,pageSizeZero,reasonable. If the mapping is not configured, the default value is used . The
    default value is pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero.

  6. supportMethodsArguments: Supports passing paging parameters through Mapper interface parameters. By default false, the paging plug-in will automatically obtain values ​​from the fields paramsconfigured and will automatically paginate when a suitable value is found.
    For usage, please refer com.github.pagehelper.test.basicto ArgumentsMapTestand under the package in the test code ArgumentsObjTest.

  7. autoRuntimeDialect: The default value is false. When set to true, allowed to automatically identify the paging of the corresponding dialect according to multiple data sources at runtime
    (automatic selection is not supported sqlserver2012, only it can be used sqlserver). For usage and precautions, refer to scenario 5 below .

  8. closeConn: The default value is true. When using the runtime dynamic data source or without setting the helperDialectproperty to automatically obtain the database type, a database connection will be automatically obtained.
    This property is used to set whether to close the obtained connection. It is closed by default true. falseAfter , the obtained connection will not be closed. The setting of this parameter depends on the data source you choose.

important hint:

When offsetAsPageNum=falseis , because of the PageNumproblem, RowBoundswhen querying reasonableis forced to false. The usage PageHelper.startPagemethod is not affected.
Note: PageRowBounds If you want to query the total number, you also need to configure this property as true.

use

Add in business inquiries

PageHelper.startPage(pageIndex, pageSize);

E.g:

@Override
    public List<SysUserEntity> query(SysUserEntity user) {
        // 查询第一页的两条数据
        PageHelper.startPage(1, 2);
        return sysUserDao.query(user);
    }

Test the return result:

image

Let's take a look at the SQL it executes:

Preparing: SELECT count(0) FROM sys_user 
Parameters: 
     Total: 1
Preparing: SELECT * FROM sys_user LIMIT ? 
Parameters: 2(Integer)
     Total: 2

Replenish

Instructions

**When using, you need to read the author's article pagehelper/Mybatis-PageHelper carefully **

It is mainly to read the usage method, as well as the method used in each scenario to solve the problem, and the precautions.

paging sort

PageHelper.startPage(pageIndex, pageSize, orderBy);

Support returning PageInfo

    @Override
    public PageInfo queryByPageInfo(SysUserEntity user) {
        return PageHelper.startPage(1,2).doSelectPageInfo(() -> sysUserDao.query(user));
    }

Test return data with Swagger:

{
  "pageNum": 1,
  "pageSize": 2,
  "size": 2,
  "startRow": 1,
  "endRow": 2,
  "total": 4,
  "pages": 2,
  "list": [
    {
      "id": 1,
      "username": "def",
      "password": "123",
      "mobile": null,
      "email": null,
      "createUserId": null,
      "createDate": null
    },
    {
      "id": 7,
      "username": "wuwii",
      "password": "123",
      "mobile": null,
      "email": null,
      "createUserId": null,
      "createDate": null
    }
  ],
  "prePage": 0,
  "nextPage": 2,
  "isFirstPage": true,
  "isLastPage": false,
  "hasPreviousPage": false,
  "hasNextPage": true,
  "navigatePages": 8,
  "navigatepageNums": [
    1,
    2
  ],
  "navigateFirstPage": 1,
  "navigateLastPage": 2,
  "firstPage": 1,
  "lastPage": 2
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325632777&siteId=291194637