以前刚看到PageHelper的时候,觉得有点意思,它没有通过sql的硬编码来实现分页,而是通过拦截器配置的方式拦截sql,实现sql的自动分页。
本篇将会通过使用PageHelper做一次sql查询测试,来解析PageHelper底层是如何实现自动分页的。
我们先看下mybatis-config.xml 的配置如下:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
PageHelper底层是使用的mybatis的拦截器,不熟悉的同学,可以看看先这个源码解析mybatis拦截器的运行原理。
测试代码如下:
public class TestPageHelper {
public static void main(String[] args) throws Exception {
Map<String, Object> map=new HashMap<>();
Page page= PageHelper.startPage(1, 5);
DBoperate.testPageHelper(map);
System.out.println(page);
}
}
public static List<Map<String, Object>> testPageHelper(Map<String, Object> map) throws Exception {