MyBatis分页插件的原理?

180 阅读1分钟

image.png

首先分页参数放到ThreadLocal中,拦截执行的sql,根据数据库类型添加对应的分页语句重写sql,例如select * from table转换为select count(*) from tableselect * from table limit a,b,计算出了total总条数、pageNum当前第几页、pageSize每页大小和当前页的数据,是否为首页、是否为尾页、总条数等。

@Configuration 
@MapperScan("xx.xx.xx.mapper") //mapper的路径
public class MybatisPlusConfig { 
    @Bean 
    public MybatisPlusInterceptor addPaginationInnerInterceptor(){ 
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); 
        //向Mybatis过滤器链中添加分页拦截器 
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); 
        return interceptor; 
    } 
}