This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
In the previous article, “How to Learn from System Paging Tools -PageHelper Part 2,” we explained that PageHelper uses ThreadLocal to store paging parameters in the current thread. So why can’t we find code to add interceptors later in the project?
PageHelper-Spring-Boot-Starter
Zoe is based on SpringBoot. PageHelper has a spring-boot version of pagehelper-spring-boot-starter.
Let’s download the source code and have a look.
The project is one of the most important class PageHelperAutoConfiguration, some of its source code is as follows:
/** * Custom injection paging plug-in **@author liuzh
*/
@Configuration
@ConditionalOnBean(SqlSessionFactory.class)
@EnableConfigurationProperties(PageHelperProperties.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
@Lazy(false)
public class PageHelperAutoConfiguration implements InitializingBean {
// Other code
}
Copy the code
This injects the PageInterceptor into the project via annotations. Part of the code for the PageInterceptor is as follows:
public class PageInterceptor implements org.apache.ibatis.plugin.Interceptor {
// Other code in the class...
}
Copy the code
The plug-in implements the interface org. Apache. Ibatis. Plugin. The Interceptor, the interface is why?
Mybatis interceptor interface
The Interceptor PageHelper PageInterceptor implements an interface org. Apache. Ibatis. Plugin. The Interceptor, the latter actually for Mybatis Interceptor interface.
Interceptors that implement this interface can intercept Mybatis execution requests to modify the default behavior of Mybatis, such as overwriting Sql.
Wow, isn’t that the logic we’ve been looking for?
Mybatis: PageInterceptor Mybatis: PageInterceptor Mybatis: PageInterceptor
/ * * * Mybatis - general paging interceptor * < p > * lot: https://github.com/pagehelper/Mybatis-PageHelper * < p > * Gitee: https://gitee.com/free/Mybatis_PageHelper * *@author liuzh/abel533/isea533
* @version5.0.0 * /
@SuppressWarnings({"rawtypes", "unchecked"})
@Intercepts( { @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}), } )
public class PageInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// Interceptor logic code...}}Copy the code
There is an interesting annotation: @intercepts.
Class, statementhandler. class, pameterHandler. class, resultsethandler. class.
We won’t go into the implementation of the interface here, so you can get a basic impression of the implementation, especially this annotation.
Now, we opened, is PageHelper plug-in for PageInterceptor by implementing an interface org. Apache. Ibatis. Plugin. The Interceptor, so as to achieve the goal of the new SQL.
Going back, let’s take a look at how resql is implemented specifically in the interceptor for paging purposes.