コード例 #1
0
  /**
   * 生成特定数据库的分页语句
   *
   * @param sql
   * @param page
   * @return
   */
  private String buildPageSql(String sql, Page<?> page) {
    if (page == null || dialect == null || dialect.equals("")) {
      return sql;
    }
    StringBuilder sb = new StringBuilder();
    int startRow = page.getOffset();

    if ("mysql".equals(dialect)) {
      sb.append(sql);
      sb.append(" limit ").append(startRow).append(",").append(page.getLimit());
    } else if ("hsqldb".equals(dialect)) {
      sb.append("select limit ");
      sb.append(startRow);
      sb.append(" ");
      sb.append(page.getLimit());
      sb.append(" ");
      sb.append(sql.substring(6));
    } else if ("oracle".equals(dialect)) {
      sb.append("select * from (select tmp_tb.*,ROWNUM row_id from (");
      sb.append(sql);
      sb.append(")  tmp_tb where ROWNUM<=");
      sb.append(startRow + page.getLimit());
      sb.append(") where row_id>");
      sb.append(startRow);
    } else {
      throw new IllegalArgumentException("SelectInterceptor error:does not support " + dialect);
    }
    return sb.toString();
  }
コード例 #2
0
ファイル: PagePlugin.java プロジェクト: shixitong/ddj
  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();
  }