@SuppressWarnings("unchecked")
  private Page pageByMetaObject(MetaObject metaObject) {
    DefaultParameterHandler defaultParameterHandler =
        (DefaultParameterHandler) metaObject.getValue("delegate.parameterHandler");
    Object parameterObject = defaultParameterHandler.getParameterObject();
    if (parameterObject != null && parameterObject instanceof MapperParamMap) {
      MapperParamMap resultMap = (MapperParamMap) parameterObject;
      if (!resultMap.containsKey("page")) {
        return null;
      }

      Object pageEx = resultMap.get("page");
      if (pageEx != null) return (Page) pageEx;
    }
    return null;
  }
Esempio n. 2
0
  public Object intercept(Invocation ivk) throws Throwable {
    if (ivk.getTarget() instanceof RoutingStatementHandler) {
      RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
      BaseStatementHandler delegate =
          (BaseStatementHandler) Reflections.getFieldValue(statementHandler, "delegate");
      MappedStatement mappedStatement =
          (MappedStatement) Reflections.getFieldValue(delegate, "mappedStatement");

      if (mappedStatement.getId().matches(pageSqlId)) { // 拦截需要分页的SQL
        BoundSql boundSql = delegate.getBoundSql();
        Object parameterObject =
            boundSql
                .getParameterObject(); // 分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
        if (parameterObject == null) {
          throw new NullPointerException("parameterObject尚未实例化!");
        } else {
          // 生成查询总数语句,并获取总数
          Connection connection = (Connection) ivk.getArgs()[0];
          String sql = boundSql.getSql();
          StringBuilder countSql = new StringBuilder();
          countSql.append("select count(1) from (").append(sql).append(") tmp_count");
          PreparedStatement countStmt = connection.prepareStatement(countSql.toString());
          // BoundSql countBS = new
          // BoundSql(mappedStatement.getConfiguration(),countSql.toString(),boundSql.getParameterMappings(),parameterObject);

          // 使用DefaultParameterHandler来对语句进行预处理
          DefaultParameterHandler defaultParameterHandler =
              new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
          defaultParameterHandler.setParameters(countStmt);
          ResultSet rs = countStmt.executeQuery();
          long count = 0;
          if (rs.next()) {
            count = rs.getLong(1);
          }
          rs.close();
          countStmt.close();
          // System.out.println(count);
          RequestBean requestBean = null;
          if (parameterObject instanceof RequestBean) { // 参数就是Page实体
            requestBean = (RequestBean) parameterObject;
            requestBean.setCount(count);
          } else if (parameterObject instanceof Map) {
            requestBean = (RequestBean) ((Map) parameterObject).get("requestBean");
            requestBean.setCount(count);
          } else { // 参数为某个实体,该实体拥有Page属性
            Field pageField = Reflections.getAccessibleField(parameterObject, "requestBean");
            if (pageField != null) {
              requestBean = (RequestBean) Reflections.getFieldValue(parameterObject, "requestBean");
              if (requestBean == null) requestBean = new RequestBean();

              requestBean.setCount(count);
              Reflections.setFieldValue(
                  parameterObject, "requestBean", requestBean); // 通过反射,对实体对象设置分页对象
            } else {
              throw new NoSuchFieldException(
                  parameterObject.getClass().getName() + "不存在 requestBean 属性!");
            }
          }
          String pageSql =
              dialectObj.getLimitString(sql, requestBean.getOffset(), requestBean.getPageSize());
          Reflections.setFieldValue(boundSql, "sql", pageSql); // 将分页sql语句反射回BoundSql.
          if (logger.isDebugEnabled()) {
            logger.debug("生成分页SQL : " + boundSql.getSql());
          }
        }
      }
    }

    return ivk.proceed();
  }