public StringBuilder buildPageSqlForMysql(String sql, PageParameter page) {
    StringBuilder pageSql = new StringBuilder(100);

    pageSql.append(sql);
    pageSql.append(" limit " + page.getOffset() + "," + page.getLimit());
    return pageSql;
  }
 public StringBuilder buildPageSqlForOracle(String sql, PageParameter page) {
   StringBuilder pageSql = new StringBuilder(100);
   String beginrow = String.valueOf((page.getCurrentPage() - 1) * page.getPageSize());
   String endrow = String.valueOf(page.getCurrentPage() * page.getPageSize());
   pageSql.append("select * from ( select temp.*, rownum row_id from ( ");
   pageSql.append(sql);
   pageSql.append(" ) temp where rownum <= ").append(endrow);
   pageSql.append(") where row_id > ").append(beginrow);
   return pageSql;
 }
 /**
  * 从数据库里查询总的记录数并计算总页数,回写进分页参数<code>PageParameter</code>,这样调用 者就可用通过 分页参数 <code>PageParameter
  * </code>获得相关信息。
  *
  * @param sql
  * @param connection
  * @param mappedStatement
  * @param boundSql
  * @param page
  */
 private void setPageParameter(
     String sql,
     Connection connection,
     MappedStatement mappedStatement,
     BoundSql boundSql,
     PageParameter page) {
   // 记录总记录数
   String countSql = "select count(0) as total from (" + sql + ")  tt";
   PreparedStatement countStmt = null;
   ResultSet rs = null;
   try {
     countStmt = connection.prepareStatement(countSql);
     setParameters(countStmt, mappedStatement, boundSql);
     rs = countStmt.executeQuery();
     int totalCount = 0;
     if (rs.next()) {
       totalCount = rs.getInt(1);
     }
     page.setTotalElements(totalCount);
     //			int totalPage = totalCount / page.getPageSize() + ((totalCount % page.getPageSize() == 0)
     // ? 0 : 1);
     //			page.setTotalPage(totalPage);
   } catch (SQLException e) {
     logger.error("Ignore this exception", e);
   } finally {
     try {
       rs.close();
     } catch (SQLException e) {
       logger.error("Ignore this exception", e);
     }
     try {
       countStmt.close();
     } catch (SQLException e) {
       logger.error("Ignore this exception", e);
     }
   }
 }
 @Override
 public Object intercept(Invocation invocation) throws Throwable {
   StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
   MetaObject metaStatementHandler =
       MetaObject.forObject(
           statementHandler,
           DEFAULT_OBJECT_FACTORY,
           DEFAULT_OBJECT_WRAPPER_FACTORY,
           DEFAULT_REFLECTOR_FACTORY);
   // 分离代理对象链(由于目标类可能被多个拦截器拦截,从而形成多次代理,通过下面的两次循环
   // 可以分离出最原始的的目标类)
   while (metaStatementHandler.hasGetter("h")) {
     Object object = metaStatementHandler.getValue("h");
     metaStatementHandler =
         MetaObject.forObject(
             object,
             DEFAULT_OBJECT_FACTORY,
             DEFAULT_OBJECT_WRAPPER_FACTORY,
             DEFAULT_REFLECTOR_FACTORY);
   }
   // 分离最后一个代理对象的目标类
   while (metaStatementHandler.hasGetter("target")) {
     Object object = metaStatementHandler.getValue("target");
     metaStatementHandler =
         MetaObject.forObject(
             object,
             DEFAULT_OBJECT_FACTORY,
             DEFAULT_OBJECT_WRAPPER_FACTORY,
             DEFAULT_REFLECTOR_FACTORY);
   }
   if (null == dialect || "".equals(dialect)) {
     logger.warn("Property dialect is not setted,use default 'mysql' ");
     dialect = defaultDialect;
   }
   if (null == pageSqlId || "".equals(pageSqlId)) {
     logger.warn("Property pageSqlId is not setted,use default '.*Page$' ");
     pageSqlId = defaultPageSqlId;
   }
   MappedStatement mappedStatement =
       (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
   PageParameter page = null;
   Object type = metaStatementHandler.getValue("delegate.boundSql.parameterObject");
   if (PageParameter.class.isInstance(type)) {
     page = (PageParameter) type;
   }
   // 只重写需要分页的sql语句。通过MappedStatement的ID匹配,默认重写以Page结尾的
   // MappedStatement的sql
   if (mappedStatement.getId().matches(pageSqlId) && page != null && page.getLimit() > 0) {
     BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
     Object parameterObject = boundSql.getParameterObject();
     if (parameterObject == null) {
       throw new NullPointerException("parameterObject is null!");
     } else {
       // 分页参数作为参数对象parameterObject的一个属性
       String sql = boundSql.getSql();
       // 重写sql
       String pageSql = buildPageSql(sql, page, dialect);
       metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
       // 采用物理分页后,就不需要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];
       // 重设分页参数里的总页数等
       setPageParameter(sql, connection, mappedStatement, boundSql, page);
     }
   }
   // 将执行权交给下一个拦截器
   return invocation.proceed();
 }