How does Pagination work in pageHelper?
Download pageHelper source code, using pageHelper official demo Debug test.
Source code address:
[source code] gitee.com/free/Mybati…
This article uses the branch: 4.2
PageHelper.startPage(2, 10, "id desc");
Copy the code
Hit the first break point
PageHelper.startPage
: Element, often denoting List
, uses generics in such a way that the code can be reused to allow different lists to be put in.
SqlUtil.getLocalPage
:
SqlUtil is a subclass of BaseSqlUtil, which defines a LOCAL_PAGE of type TreadLocal.
protected static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();
Copy the code
TreadLocal is a thread-internal storage class that stores copies of data in a specified thread. After data is stored, only a specific thread can obtain the stored data.
ThreadLocal defines a static member class, ThreadLocalMap, that exists to provide data storage for peripheral classes, so a ThreadLocal is like a map(), where the key is the current thread and the value is the object that needs to be stored. You can get the data (value) stored by the current thread through the get/set method.
ThreadLoacl.get()
Gets the value in the current thread copy, which in pageHelper mainly holds paging parameters.
ThreadLocal.set()
Assigns value to the current thread copy
Pagehelper. startPage(2,10) is now complete, so start MyBatis query statement.
List<Country> list = countryMapper.selectAll();
Copy the code
MyBatis interceptor
Before MyBatis converts Java code to SQL, the LOCAL_PAGE variable in BaseSqlUtil is used to fetch paging data in the thread.
It then goes to SqlUtil’s doIntercept.
SqlUtil’s interception seems to have nothing to do with MyBatis, but in fact, when it comes to interception, it is MyBatis’ interception method.
The reason:
PageHelper class from BasePageHelper implementation of MyBatis Interceptor interface using MyBatis Intercepts annotation, PageHelper implementation of MyBatis Interceptor interface, Complete dynamic paging of SQL. So when you intercept, you go to pageHelper.
The interceptor implementation is in SqlUtil.
BoundSql is the object that compiles the SQL statement to be executed based on the parameters.
Compare calls to SQL in XML:
Not exactly the same, but exactly the same.
Interceptor translation
In this step, the written XML file is compiled into SQL statements and executed in the database.
RuntimeDialect calls getCountSql to count the query statement first, and then execute the select statement according to the count value. Of course, you can also set no count in your code when querying. If there is no paging parameter in ThreadLoacl, the query results are returned directly.
If the count is greater than zero, the SQL statement is executed with paging parameters, the results are stored in LOCAL_PAGE, and the query results are returned.
After the result is returned, the sqlSession is closed.
Tips from the official use:
Only the first Mybatis query (Select) method immediately after the PageHelper.startPage method is paginated.