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 pagehelper
the 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 helperDialect
properties 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.
offsetAsPageNum
: The default valuefalse
, this parameter is valid when usedRowBounds
as a paging parameter.
When this parameter is settrue
to ,RowBounds
theoffset
parameter in willpageNum
be used as , and the two parameters of page number and page size can be used for paging.rowBoundsWithCount
: The default valuefalse
, this parameter is valid when usedRowBounds
as a paging parameter.
When this parameter istrue
set to , usingRowBounds
pagination will do a count query.pageSizeZero
: The default value isfalse
, when this parameter is settrue
to , ifpageSize=0
or , all the resultsRowBounds.limit = 0
will be queried (equivalent to not executing the paging query, but the returned results are still ofPage
type ).reasonable
: Paging rationalization parameter, the default value isfalse
. When this parameter is settrue
to ,pageNum<=0
the first page will be queried,
pageNum>pages
(when the total number is exceeded), the last page will be queried. By defaultfalse
, the query is performed directly according to the parameters.params
: In order to supportstartPage(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 configuredpageNum,pageSize,count,pageSizeZero,reasonable
. If the mapping is not configured, the default value is used . The
default value ispageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
.supportMethodsArguments
: Supports passing paging parameters through Mapper interface parameters. By defaultfalse
, the paging plug-in will automatically obtain values from the fieldsparams
configured and will automatically paginate when a suitable value is found.
For usage, please refercom.github.pagehelper.test.basic
toArgumentsMapTest
and under the package in the test codeArgumentsObjTest
.autoRuntimeDialect
: The default value isfalse
. When set totrue
, allowed to automatically identify the paging of the corresponding dialect according to multiple data sources at runtime
(automatic selection is not supportedsqlserver2012
, only it can be usedsqlserver
). For usage and precautions, refer to scenario 5 below .closeConn
: The default value istrue
. When using the runtime dynamic data source or without setting thehelperDialect
property 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 defaulttrue
.false
After , the obtained connection will not be closed. The setting of this parameter depends on the data source you choose.
important hint:
When offsetAsPageNum=false
is , because of the PageNum
problem, RowBounds
when querying reasonable
is forced to false
. The usage PageHelper.startPage
method 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:
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
}