예제 #1
0
  public Object intercept(Invocation ivk) throws Throwable {
    // TODO Auto-generated method stub
    if (ivk.getTarget() instanceof RoutingStatementHandler) {
      RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
      BaseStatementHandler delegate =
          (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
      MappedStatement mappedStatement =
          (MappedStatement) ReflectHelper.getValueByFieldName(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();
          String countSql = "select count(0) from (" + sql + ") as tmp_count"; // 记录统计
          PreparedStatement countStmt = connection.prepareStatement(countSql);
          BoundSql countBS =
              new BoundSql(
                  mappedStatement.getConfiguration(),
                  countSql,
                  boundSql.getParameterMappings(),
                  parameterObject);
          setParameters(countStmt, mappedStatement, countBS, parameterObject);
          ResultSet rs = countStmt.executeQuery();
          int count = 0;
          if (rs.next()) {
            count = rs.getInt(1);
          }
          rs.close();
          countStmt.close();
          // System.out.println(count);
          Page page = null;
          if (parameterObject instanceof Page) { // 参数就是Page实体
            page = (Page) parameterObject;
            page.setEntityOrField(true); // 见com.ttsoft.entity.Page.entityOrField 注释
            page.setTotalResult(count);
          } else { // 参数为某个实体,该实体拥有Page属性
            Field pageField = ReflectHelper.getFieldByFieldName(parameterObject, "page");
            if (pageField != null) {
              page = (Page) ReflectHelper.getValueByFieldName(parameterObject, "page");
              if (page == null) page = new Page();
              page.setEntityOrField(false); // 见com.ttsoft.entity.Page.entityOrField 注释
              page.setTotalResult(count);
              ReflectHelper.setValueByFieldName(parameterObject, "page", page); // 通过反射,对实体对象设置分页对象
            } else {
              throw new NoSuchFieldException(parameterObject.getClass().getName() + "不存在 page 属性!");
            }
          }
          String pageSql = generatePageSql(sql, page);
          ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); // 将分页sql语句反射回BoundSql.
        }
      }
    }
    return ivk.proceed();
  }
예제 #2
0
  public Object intercept(Invocation ivk) throws Throwable {
    if (ivk.getTarget() instanceof RoutingStatementHandler) {
      RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
      BaseStatementHandler delegate =
          (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
      MappedStatement mappedStatement =
          (MappedStatement) ReflectHelper.getValueByFieldName(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 {
          long count = 0;
          String sql = "";
          try {
            Connection connection = (Connection) ivk.getArgs()[0];
            sql = boundSql.getSql();
            String countSql = "";
            if ("mysql".equals(dialect)) {
              countSql = "select count(1) from (" + sql + ") as tmp_count"; // 记录统计
            } else if ("oracle".equals(dialect)) {
              countSql = "select count(1) from (" + sql + ") tmp_count"; // 记录统计
            }

            PreparedStatement countStmt = connection.prepareStatement(countSql);
            BoundSql countBS =
                new BoundSql(
                    mappedStatement.getConfiguration(),
                    countSql,
                    boundSql.getParameterMappings(),
                    parameterObject);
            setParameters(countStmt, mappedStatement, countBS, parameterObject);
            ResultSet rs = countStmt.executeQuery();

            if (rs.next()) {
              count = rs.getLong(1);
            }
            rs.close();
            countStmt.close();
          } catch (Exception e) {
            throw new RuntimeException("分页查询无法获取总记录数", e);
          }

          Pagination page = null;
          if (parameterObject instanceof Pagination) { // 参数就是Pagination实体
            page = (Pagination) parameterObject;
            page.setTotalRows(count);
          } else { // 参数为某个实体,该实体拥有Page属性
            Field pageField = ReflectHelper.getFieldByFieldName(parameterObject, "pagination");
            if (pageField != null) {
              page = (Pagination) ReflectHelper.getValueByFieldName(parameterObject, "pagination");
              if (page == null) page = new Pagination();
              page.setTotalRows(count);
              ReflectHelper.setValueByFieldName(
                  parameterObject, "pagination", page); // 通过反射,对实体对象设置分页对象
            } else {
              throw new NoSuchFieldException(
                  parameterObject.getClass().getName() + "不存在 pagination 属性!");
            }
          }
          String pageSql = generatePageSql(sql, page);
          ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); // 将分页sql语句反射回BoundSql.
        }
      }
    }
    return ivk.proceed();
  }
예제 #3
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();
  }