PageHelper 实现原理

343 阅读1分钟

简述

  1. 分页信息存到怕个pagehelper

  2. 使用pagehelper封装的分页方法

  3. pagehelper将page信息存到ThreadLocal

  4. 执行sql前拦截实现Interceptor的接口(pageInterceptor实现了该接口)

  5. pageInterceptor从ThreadLocal中获取分页信息

  6. 进行count查询判断是否要进行分页

  7. 如果需要分页,重新组装sql,带上分页信息

  8. 返回pageinfo

PageHelper 实现原理

存储page信息到ThreadLocal

page内是分页信息,存到了Threadlocal中

切实现interceptor方法等的sql执行

切面中的这个方法执行分页

然后会在这里面去取本地的分页数据

附上MyBatis 插件实现原理(pagehelper之所以要实现interceptor)

既然插件需要在配置文件中进行配置,那么肯定就需要进行解析,我们看看插件式如何被解析的。我们进入XMLConfigBuilder类看看

解析出来之后会将插件存入InterceptorChain对象的 list 属性

看到InterceptorChain我们是不是可以联想到,MyBatis 的插件就是通过责任链模式实现的。