Пример #1
0
  /**
   * 获取查询sql <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String updateSql(Object obj) {
    AssertUtils.notEmpty(obj, "update obj must not empty.");

    // 获取当前对象中有哪些属性
    MetaObject metaObject = MetaObjectUtils.forObject(obj);
    Set<String> keySet = new HashSet<String>();
    for (String getterName : metaObject.getGetterNames()) {
      keySet.add(getterName);
    }
    AssertUtils.isTrue(
        keySet.contains(this.pkName),
        "obj:{} must contains pk{}.",
        new Object[] {obj, this.pkName});

    // 构建query语句
    SqlBuilder.BEGIN();
    SqlBuilder.UPDATE(this.tableName);

    for (String propertyName : updateAblePropertyNames) {
      if (!keySet.contains(propertyName)) {
        continue;
      }
      // Object value = metaObject.getValue(propertyName);
      String columnName = getter2columnNameMapping.get(propertyName);
      SqlBuilder.SET(columnName + " = ?");
    }
    SqlBuilder.WHERE(this.getter2columnNameMapping.get(this.pkName) + " = ? ");
    String updateSql = SqlBuilder.SQL();
    SqlBuilder.RESET();
    return updateSql;
  }
Пример #2
0
  /**
   * 获取查询条件的Setter对象 <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getUpdateSetter(Object obj) {
    AssertUtils.notEmpty(obj, "update obj must not empty.");

    // 获取当前对象中有哪些属性
    final MetaObject metaObject = MetaObjectUtils.forObject(obj);
    final Set<String> keySet = new HashSet<String>();
    for (String getterName : metaObject.getGetterNames()) {
      keySet.add(getterName);
    }
    AssertUtils.isTrue(
        keySet.contains(this.pkName),
        "obj:{} must contains pk{}.",
        new Object[] {obj, this.pkName});
    final String finalPkName = this.pkName;
    PreparedStatementSetter res =
        new PreparedStatementSetter() {
          @Override
          public void setValues(PreparedStatement ps) throws SQLException {
            int i = 1;
            for (String propertyNameTemp : updateAblePropertyNames) {
              if (!keySet.contains(propertyNameTemp)) {
                continue;
              }
              Object valueObj = metaObject.getValue(propertyNameTemp);
              Class<?> setterType = getter2JavaTypeMapping.get(propertyNameTemp);
              JdbcUtils.setPreparedStatementValueForSimpleType(ps, i++, valueObj, setterType);
            }
            Class<?> setterType = getter2JavaTypeMapping.get(finalPkName);
            JdbcUtils.setPreparedStatementValueForSimpleType(
                ps, i, metaObject.getValue(finalPkName), setterType);
          }
        };
    return res;
  }
Пример #3
0
  /**
   * 获取查询sql <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String countSql(Object obj) {
    // 构建query语句
    SqlBuilder.BEGIN();
    SqlBuilder.SELECT("COUNT(1)");
    SqlBuilder.FROM(this.tableName);

    if (!ObjectUtils.isEmpty(obj)) {
      MetaObject metaObject = MetaObjectUtils.forObject(obj);
      for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
        String queryKeyName = entryTemp.getKey();
        Object valueObj = metaObject.getValue(queryKeyName);
        if (ObjectUtils.isEmpty(valueObj)) {
          continue;
        }
        SqlBuilder.WHERE(entryTemp.getValue());
      }
    }
    for (String conditionExpressionTemp : otherCondition) {
      SqlBuilder.WHERE(conditionExpressionTemp);
    }
    String querySql = SqlBuilder.SQL();
    SqlBuilder.RESET();

    return querySql;
  }
Пример #4
0
  /**
   * 获取查询条件的Setter对象 <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getQueryCondtionSetter(Object obj) {
    PreparedStatementSetter res = null;
    if (!ObjectUtils.isEmpty(obj)) {
      final MetaObject metaObject = MetaObjectUtils.forObject(obj);
      res =
          new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
              int i = 1;
              for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
                String queryKeyName = entryTemp.getKey();
                Object valueObj = metaObject.getValue(queryKeyName);
                if (ObjectUtils.isEmpty(valueObj)) {
                  continue;
                }
                JdbcType jdbcType = queryConditionKey2JdbcTypeMapping.get(queryKeyName);
                JdbcUtils.setPreparedStatementValueForSimpleType(ps, i, valueObj, jdbcType);
                i++;
              }
            }
          };
    } else {
      res =
          new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {}
          };
    }

    return res;
  }
Пример #5
0
 /**
  * 获取动态查询条件的参数映射关系<br>
  * <功能详细描述>
  *
  * @param obj
  * @return [参数说明]
  * @return LinkedHashMap<String,Object> [返回类型说明]
  * @exception throws [异常类型] [异常说明]
  * @see [类、类#方法、类#成员]
  */
 public LinkedHashMap<String, Object> getQueryCondtionParamMaps(Object obj) {
   final MetaObject metaObject = MetaObjectUtils.forObject(obj);
   final LinkedHashMap<String, Object> resMap = new LinkedHashMap<String, Object>();
   for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
     String queryKeyName = entryTemp.getKey();
     Object valueObj = metaObject.getValue(queryKeyName);
     if (ObjectUtils.isEmpty(valueObj)) {
       continue;
     }
     resMap.put(queryKeyName, valueObj);
   }
   return resMap;
 }
Пример #6
0
  /**
   * 获取查询sql <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String querySql(Object obj) {
    AssertUtils.notEmpty(this.pkName, "pkName is empty.");
    AssertUtils.isTrue(
        this.getter2columnNameMapping.containsKey(this.pkName),
        "property2columnNameMapping not contains pkName:{}.",
        this.pkName);

    // 构建query语句
    SqlBuilder.BEGIN();
    for (Entry<String, String> entryTemp : getter2columnNameMapping.entrySet()) {
      SqlBuilder.SELECT(entryTemp.getValue());
    }
    SqlBuilder.FROM(this.tableName);

    if (!ObjectUtils.isEmpty(obj)) {
      MetaObject metaObject = MetaObjectUtils.forObject(obj);
      for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
        String queryKeyName = entryTemp.getKey();
        Object valueObj = metaObject.getValue(queryKeyName);
        if (ObjectUtils.isEmpty(valueObj)) {
          continue;
        }
        SqlBuilder.WHERE(entryTemp.getValue());
      }
    }
    for (String conditionExpressionTemp : otherCondition) {
      SqlBuilder.WHERE(conditionExpressionTemp);
    }

    // 在不存在排序字段时默认使用主键对应字段作为排序字段<br/>
    if (CollectionUtils.isEmpty(orderList)) {
      SqlBuilder.ORDER_BY(this.getter2columnNameMapping.get(this.pkName));
    } else {
      for (String order : orderList) {
        SqlBuilder.ORDER_BY(order);
      }
    }

    String querySql = SqlBuilder.SQL();
    SqlBuilder.RESET();

    return querySql;
  }
Пример #7
0
 /**
  * 生成setter对象进行设值<br>
  * <功能详细描述>
  *
  * @param obj
  * @return [参数说明]
  * @return PreparedStatementSetter [返回类型说明]
  * @exception throws [异常类型] [异常说明]
  * @see [类、类#方法、类#成员]
  */
 public PreparedStatementSetter getInsertSetter(final Object obj) {
   // 如果插入语句还没有初始化,则需要先初始化插入语句
   final MetaObject metaObject = MetaObjectUtils.forObject(obj);
   PreparedStatementSetter res =
       new PreparedStatementSetter() {
         @Override
         public void setValues(PreparedStatement ps) throws SQLException {
           int i = 1;
           for (Entry<String, String> entryTemp : getter2columnNameMapping.entrySet()) {
             String propertyTemp = entryTemp.getKey();
             Class<?> getterType = getter2JavaTypeMapping.get(propertyTemp);
             JdbcUtils.setPreparedStatementValueForSimpleType(
                 ps, i, metaObject.getValue(entryTemp.getKey()), getterType);
             i++;
           }
         }
       };
   return res;
 }
Пример #8
0
  /**
   * 获取设置主键的setter对象<br>
   * <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getPKSetter(final Object obj) {
    AssertUtils.notEmpty(this.pkName, "pkName is empty.");
    AssertUtils.isTrue(
        this.getter2columnNameMapping.containsKey(this.pkName),
        "property2columnNameMapping not contains pkName:{}.",
        this.pkName);

    final String finalPkName = this.pkName;
    final MetaObject metaObject = MetaObjectUtils.forObject(obj);
    PreparedStatementSetter res =
        new PreparedStatementSetter() {
          @Override
          public void setValues(PreparedStatement ps) throws SQLException {
            Class<?> setterType = getter2JavaTypeMapping.get(finalPkName);
            JdbcUtils.setPreparedStatementValueForSimpleType(
                ps, 1, metaObject.getValue(finalPkName), setterType);
          }
        };
    return res;
  }
Пример #9
0
  /**
   * 获取一个设置的callbackHandler实现 <功能详细描述>
   *
   * @param newObjInstance
   * @return [参数说明]
   * @return RowCallbackHandler [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public RowCallbackHandler getSelectRowCallbackHandler(Object newObjInstance) {
    AssertUtils.notNull(this.classReflector, "classReflector is null");

    final MetaObject metaObject = MetaObjectUtils.forObject(newObjInstance);
    final ClassReflector<T> finalClassReflector = this.classReflector;
    RowCallbackHandler res =
        new RowCallbackHandler() {
          @Override
          public void processRow(ResultSet rs) throws SQLException {
            for (Entry<String, String> entryTemp : getter2columnNameMapping.entrySet()) {

              String propertyName = entryTemp.getKey();
              String columnName = entryTemp.getValue();

              if (finalClassReflector.getSetterNames().contains(propertyName)) {
                Class<?> type = getter2JavaTypeMapping.get(propertyName);
                Object value = JdbcUtils.getResultSetValueForSimpleType(rs, columnName, type);
                metaObject.setValue(propertyName, value);
              }
            }
          }
        };
    return res;
  }
Пример #10
0
  /**
   * 获取查询条件的Setter对象 <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getPagedQueryCondtionSetter(Object obj, int offset, int limit) {
    final MetaObject metaObject = MetaObjectUtils.forObject(obj);

    // 如果支持isSupportsLimitOffset并且当前需要偏移值
    // final boolean isSupportsVariableLimit = dialect.supportsVariableLimit();//是否支持物理分页
    final boolean isBindOnFirst = dialect.bindLimitParametersFirst(); // 是否绑定在前
    final boolean isSupportsLimit = dialect.supportsLimit(); // 是否支持limit
    final boolean isSupportsLimitOffset = dialect.supportsLimitOffset(); // 是否支持offset
    final boolean bindLimitParametersInReverseOrder = dialect.bindLimitParametersInReverseOrder();
    final boolean isNeedSetOffset =
        isSupportsLimitOffset && (offset > 0 || dialect.forceLimitUsage());
    final boolean isUseMaxForLimit = dialect.useMaxForLimit();
    final int trueOffset = offset;
    final int trueLimit = isUseMaxForLimit ? limit : limit - offset;

    PreparedStatementSetter res =
        new PreparedStatementSetter() {
          @Override
          public void setValues(PreparedStatement ps) throws SQLException {
            int i = 1;
            if (isBindOnFirst) {
              if (bindLimitParametersInReverseOrder) {
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
              } else {
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
              }
            }
            for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
              String queryKeyName = entryTemp.getKey();
              Object valueObj = metaObject.getValue(queryKeyName);
              if (ObjectUtils.isEmpty(valueObj)) {
                continue;
              }
              JdbcType jdbcType = queryConditionKey2JdbcTypeMapping.get(queryKeyName);
              JdbcUtils.setPreparedStatementValueForSimpleType(ps, i++, valueObj, jdbcType);
            }
            if (!isBindOnFirst) {
              if (bindLimitParametersInReverseOrder) {
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
              } else {
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
              }
            }
          }
        };
    return res;
  }
Пример #11
0
 /**
  * 获取对象的主键值
  *
  * @param 对pkName进行赋值
  */
 public Object getValue(Object obj) {
   AssertUtils.notNull(obj, "obj is null.");
   MetaObject metaObject = MetaObjectUtils.forObject(obj);
   return metaObject.getValue(this.pkName);
 }