IPage内部原理也是基于拦截器,但是这个拦截的是方法以及方法中的参数,这个也会判断是否是查询操作。 如果是查询操作,才会进入分页的处理逻辑。 进入分页逻辑处理后,拦截器会通过反射获取该方法的参数 进行判断是否存在IPage对象的实现类。如果不存在则不进行分页,存在则将该参数赋值给IPage对象。 然后进行拼接sql的处理完成分页操作。 但是使用IPage需要注入一个bean拦截器交给spring进行管理。如下。否则不会进行拦截

  1. 注入paginationInterceptor 分页bean
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
  1. 拦截源码
public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
     MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
     // SQL 解析
     this.sqlParser(metaObject);
     // 先判断是不是SELECT操作  (2019-04-10 00:37:31 跳过存储过程)
     MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
     if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType()
            || StatementType.CALLABLE == mappedStatement.getStatementType()) {
            return invocation.proceed();
     }
        // 针对定义了rowBounds,做为mapper接口方法的参数
     BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
     Object paramObj = boundSql.getParameterObject();
     // 判断参数里是否有page对象
     IPage<?> page = null;
     if (paramObj instanceof IPage) {
            page = (IPage<?>) paramObj;
     } else if (paramObj instanceof Map) {
            for (Object arg : ((Map<?, ?>) paramObj).values()) {
                if (arg instanceof IPage) {
                    page = (IPage<?>) arg;
     break; }
            }
        }
        /*
     * 不需要分页的场合,如果 size 小于 0 返回结果集 */ if (null == page || page.getSize() < 0) {
            return invocation.proceed();
     }
        if (this.limit > 0 && this.limit <= page.getSize()) {
            //处理单页条数限制
     handlerLimit(page);
     }
        String originalSql = boundSql.getSql();
     Connection connection = (Connection) invocation.getArgs()[0];
     if (page.isSearchCount() && !page.isHitCount()) {
            SqlInfo sqlInfo = SqlParserUtils.getOptimizeCountSql(page.optimizeCountSql(), countSqlParser, originalSql);
     this.queryTotal(sqlInfo.getSql(), mappedStatement, boundSql, page, connection);
     if (page.getTotal() <= 0) {
                return null;
     }
        }
        DbType dbType = Optional.ofNullable(this.dbType).orElse(JdbcUtils.getDbType(connection.getMetaData().getURL()));
     IDialect dialect = Optional.ofNullable(this.dialect).orElse(DialectFactory.getDialect(dbType));
     String buildSql = concatOrderBy(originalSql, page);
    //执行物理分页
    DialectModel model = dialect.buildPaginationSql(buildSql, page.offset(), page.getSize());
     Configuration configuration = mappedStatement.getConfiguration();
     List<ParameterMapping> mappings = new ArrayList<>(boundSql.getParameterMappings());
     Map<String, Object> additionalParameters = (Map<String, Object>) metaObject.getValue("delegate.boundSql.additionalParameters");
     model.consumers(mappings, configuration, additionalParameters);
     metaObject.setValue("delegate.boundSql.sql", model.getDialectSql());
     metaObject.setValue("delegate.boundSql.parameterMappings", mappings);
     return invocation.proceed();
    }

参考博客
`https://blog.csdn.net/xiaolegeaizy/article/details/108461284


Redorblack
39 声望0 粉丝

后端菜鸟记录美好生活


引用和评论

0 条评论