Ejemplo n.º 1
0
 /**
  * 对SQL参数(?)设值, 参考org.apache.ibatis.executor.parameter.DefaultParameterHandler。
  *
  * @param ps 表示预编译的 SQL 语句的对象。
  * @param mappedStatement MappedStatement
  * @param boundSql SQL
  * @param parameterObject 参数对象
  * @throws java.sql.SQLException 数据库异常
  */
 @SuppressWarnings("unchecked")
 public static void setParameters(
     PreparedStatement ps,
     MappedStatement mappedStatement,
     BoundSql boundSql,
     Object parameterObject)
     throws SQLException {
   ErrorContext.instance()
       .activity("setting parameters")
       .object(mappedStatement.getParameterMap().getId());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     Configuration configuration = mappedStatement.getConfiguration();
     TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
     MetaObject metaObject =
         parameterObject == null ? null : configuration.newMetaObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         PropertyTokenizer prop = new PropertyTokenizer(propertyName);
         if (parameterObject == null) {
           value = null;
         } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else if (boundSql.hasAdditionalParameter(propertyName)) {
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
             && boundSql.hasAdditionalParameter(prop.getName())) {
           value = boundSql.getAdditionalParameter(prop.getName());
           if (value != null) {
             value =
                 configuration
                     .newMetaObject(value)
                     .getValue(propertyName.substring(prop.getName().length()));
           }
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         @SuppressWarnings("rawtypes")
         TypeHandler typeHandler = parameterMapping.getTypeHandler();
         if (typeHandler == null) {
           throw new ExecutorException(
               "There was no TypeHandler found for parameter "
                   + propertyName
                   + " of statement "
                   + mappedStatement.getId());
         }
         typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
       }
     }
   }
 }
Ejemplo n.º 2
0
 public Object getValue(String name) {
   PropertyTokenizer prop = new PropertyTokenizer(name);
   if (prop.hasNext()) {
     org.apache.ibatis.reflection.MetaObject metaValue =
         metaObjectForProperty(prop.getIndexedName());
     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
       return null;
     } else {
       return metaValue.getValue(prop.getChildren());
     }
   } else {
     return objectWrapper.get(prop);
   }
 }
Ejemplo n.º 3
0
 public void setValue(String name, Object value) {
   PropertyTokenizer prop = new PropertyTokenizer(name);
   if (prop.hasNext()) {
     org.apache.ibatis.reflection.MetaObject metaValue =
         metaObjectForProperty(prop.getIndexedName());
     if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
       if (value == null && prop.getChildren() != null) {
         return; // don't instantiate child path if value is null
       } else {
         metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
       }
     }
     metaValue.setValue(prop.getChildren(), value);
   } else {
     objectWrapper.set(prop, value);
   }
 }
Ejemplo n.º 4
0
 /**
  * 获取一个Mybatis运行时调用的SQL和参数
  *
  * @param ms
  * @param parameterObject
  * @return
  */
 public static MyBatisSql getMyBatisSql(MappedStatement ms, Object parameterObject) {
   MyBatisSql ibatisSql = new MyBatisSql();
   BoundSql boundSql = ms.getBoundSql(parameterObject);
   ibatisSql.setSql(boundSql.getSql());
   List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
   if (parameterMappings != null) {
     Object[] parameterArray = new Object[parameterMappings.size()];
     MetaObject metaObject =
         parameterObject == null ? null : MetaObject.forObject(parameterObject);
     for (int i = 0; i < parameterMappings.size(); i++) {
       ParameterMapping parameterMapping = parameterMappings.get(i);
       if (parameterMapping.getMode() != ParameterMode.OUT) {
         Object value;
         String propertyName = parameterMapping.getProperty();
         PropertyTokenizer prop = new PropertyTokenizer(propertyName);
         if (parameterObject == null) {
           value = null;
         } else if (ms.getConfiguration()
             .getTypeHandlerRegistry()
             .hasTypeHandler(parameterObject.getClass())) {
           value = parameterObject;
         } else if (boundSql.hasAdditionalParameter(propertyName)) {
           value = boundSql.getAdditionalParameter(propertyName);
         } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
             && boundSql.hasAdditionalParameter(prop.getName())) {
           value = boundSql.getAdditionalParameter(prop.getName());
           if (value != null) {
             value =
                 MetaObject.forObject(value)
                     .getValue(propertyName.substring(prop.getName().length()));
           }
         } else {
           value = metaObject == null ? null : metaObject.getValue(propertyName);
         }
         parameterArray[i] = value;
       }
     }
     ibatisSql.setParameters(parameterArray);
   }
   return ibatisSql;
 }