private void pagination(Invocation invocation, StatementHandler target) throws SQLException {
   final MetaObject metaStatementHandler = getMetaObject(target);
   final BoundSql boundSql = target.getBoundSql();
   Page<?> page = PAGE_THREAD_LOCAL.get();
   if (page == null) {
     page = findPageParameter(boundSql.getParameterObject());
   }
   // 如果传入的参数中有分页对象且sql语句中有select,才做分页处理
   String sql = boundSql.getSql().toLowerCase();
   if (sql.startsWith("select") && page != null) {
     // 采用物理分页后,就不需要mybatis的内存分页了,所以重置下面的两个参数
     metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
     metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
     // 设置分页对象里的总记录数和总页数
     Connection connection = (Connection) invocation.getArgs()[0];
     MappedStatement mappedStatement =
         (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
     if (page.isCountTotal()) {
       int recordsTotal = getTotalCount(sql, connection, mappedStatement, boundSql);
       page.setTotalNum(recordsTotal);
     }
     // 最后重写sql
     String pageSql = buildPageSql(sql, page);
     metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
   }
 }
  @SuppressWarnings({"rawtypes", "unchecked"})
  private void autoMap(Invocation invocation, String name) throws ClassNotFoundException {
    final Object[] queryArgs = invocation.getArgs();
    final MappedStatement ms = (MappedStatement) queryArgs[0];
    final Object parameter = queryArgs[1];
    String statementId = ms.getId();
    MapperMeta meta = getMapperMeta(ms.getConfiguration(), statementId);
    if (meta.isFillEntity()) {
      // 将泛型类加入到参数中供CrudTemplate使用
      if (parameter != null) {
        Map map;
        if (parameter instanceof Map) {
          map = (HashMap) parameter;
          map.put(CrudProvider.CLASS_KEY, meta.getEntity());
        } else {
          map = new HashMap();
          map.put(CrudProvider.PARA_KEY, parameter);
          map.put(CrudProvider.CLASS_KEY, meta.getEntity());
        }
        queryArgs[1] = map;
      } else {
        queryArgs[1] = meta.getEntity();
      }
    }

    if (meta.isFillResultMap()) {
      MetaObject metaMappedStatement = getMetaObject(ms);
      metaMappedStatement.setValue("resultMaps", meta.getResultMaps());
    }

    if (name.equals("query")) {
      final RowBounds rowBounds = (RowBounds) queryArgs[2];
      if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        Page p = findPageParameter(queryArgs[1]);
        if (p != null) {
          queryArgs[2] = new RowBounds(p.getOffset(), p.getLimit());
        }
      }
    }
  }
  public Object doPage(Invocation invocation) throws Throwable {
    log.debug("进入Executor");
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    PageParam pageParam = null; // PageContextHolder.get();
    if (args[1] instanceof MapperMethod.ParamMap<?>) {
      MapperMethod.ParamMap<?> map = (MapperMethod.ParamMap<?>) args[1];
      for (String key : map.keySet()) {
        if (key.startsWith("param")) {
          Object obj = map.get(key);
          if (obj instanceof PageParam) {
            pageParam = (PageParam) obj;
          } else if (args[1] instanceof MapperMethod.ParamMap<?>) {
            args[1] = obj;
          }
        }
      }
    }
    // 无分页参数
    if (pageParam == null) {
      log.debug("默认查询");
      return invocation.proceed();
    }
    log.debug("分页查询");
    PageWrapper page = new PageWrapper();
    MappedStatement ms = (MappedStatement) args[0];
    args[2] = RowBounds.DEFAULT;
    // 无需分页
    if (pageParam.getPageSize() == 0) {
      log.debug("全量查询");
      Object result = invocation.proceed();
      page.addAll((List) result);
      page.setPageNo(1);
      page.setPageSize(page.size());
      page.setTotalPage(1);
      page.setTotalCount(page.size());
      return page;
    }
    SqlSource sqlSource = ((MappedStatement) args[0]).getSqlSource();
    dialect = DialectFactory.buildDialect(ms.getConfiguration());

    // 需总数
    args[0] = getMappedStatement(ms, sqlSource, args[1], Const.CACHE_KEY_COUNT, MappingEnum.INT);
    Object resultCount = invocation.proceed();
    int totalCount = ((Integer) ((List) resultCount).get(0));
    log.debug("总条数查询为 {}", totalCount);
    page.setTotalCount(totalCount);
    page.setPageNo(pageParam.getPageNo());
    page.setPageSize(pageParam.getPageSize());
    // 提升效率:count为0不再分页查
    if (page.getTotalCount() == 0) {
      return page;
    }

    // 需分页
    // 便捷查询:pageSize<=0 仅查询count
    if (pageParam.getPageSize() > 0) {
      args[0] =
          getMappedStatement(ms, sqlSource, args[1], Const.CACHE_KEY_PAGE, MappingEnum.DEFAULT);
      args[1] = setParameter((MappedStatement) args[0], args[1], pageParam, dialect);
      Object result = invocation.proceed();
      page.addAll((List) result);
      log.debug("分页查询结束 {}", pageParam);
    }
    return page;
  }