简述
-
分页信息存到怕个pagehelper
-
使用pagehelper封装的分页方法
-
pagehelper将page信息存到ThreadLocal
-
执行sql前拦截实现Interceptor的接口(pageInterceptor实现了该接口)
-
pageInterceptor从ThreadLocal中获取分页信息
-
进行count查询判断是否要进行分页
-
如果需要分页,重新组装sql,带上分页信息
-
返回pageinfo
PageHelper 实现原理
存储page信息到ThreadLocal
page内是分页信息,存到了Threadlocal中
切实现interceptor方法等的sql执行
切面中的这个方法执行分页
然后会在这里面去取本地的分页数据
附上MyBatis 插件实现原理(pagehelper之所以要实现interceptor)
既然插件需要在配置文件中进行配置,那么肯定就需要进行解析,我们看看插件式如何被解析的。我们进入XMLConfigBuilder类看看
解析出来之后会将插件存入InterceptorChain对象的 list 属性
看到InterceptorChain我们是不是可以联想到,MyBatis 的插件就是通过责任链模式实现的。