Search in sources :

Example 1 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class MapperMethod method convertToDeclaredCollection.

private <E> Object convertToDeclaredCollection(Configuration config, List<E> list) {
    Object collection = config.getObjectFactory().create(method.getReturnType());
    MetaObject metaObject = config.newMetaObject(collection);
    metaObject.addAll(list);
    return collection;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 2 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class BeanWrapper method instantiatePropertyValue.

@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
    MetaObject metaValue;
    Class<?> type = getSetterType(prop.getName());
    try {
        Object newObject = objectFactory.create(type);
        metaValue = MetaObject.forObject(newObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
        set(prop, newObject);
    } catch (Exception e) {
        throw new ReflectionException("Cannot set value of property '" + name + "' because '" + name + "' is null and cannot be instantiated on instance of " + type.getName() + ". Cause:" + e.toString(), e);
    }
    return metaValue;
}
Also used : ReflectionException(org.apache.ibatis.reflection.ReflectionException) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) ReflectionException(org.apache.ibatis.reflection.ReflectionException)

Example 3 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultParameterHandler method setParameters.

@Override
public void setParameters(PreparedStatement ps) {
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                if (boundSql.hasAdditionalParameter(propertyName)) {
                    // issue #448 ask first for additional params
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (parameterObject == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                    value = parameterObject;
                } else {
                    MetaObject metaObject = configuration.newMetaObject(parameterObject);
                    value = metaObject.getValue(propertyName);
                }
                TypeHandler typeHandler = parameterMapping.getTypeHandler();
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null) {
                    jdbcType = configuration.getJdbcTypeForNull();
                }
                try {
                    typeHandler.setParameter(ps, i + 1, value, jdbcType);
                } catch (TypeException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                } catch (SQLException e) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
                }
            }
        }
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) SQLException(java.sql.SQLException) JdbcType(org.apache.ibatis.type.JdbcType) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeException(org.apache.ibatis.type.TypeException) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 4 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultMapResultHandler method handleResult.

@Override
public void handleResult(ResultContext<? extends V> context) {
    final V value = context.getResultObject();
    final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory, reflectorFactory);
    // TODO is that assignment always true?
    final K key = (K) mo.getValue(mapKey);
    mappedResults.put(key, value);
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 5 with MetaObject

use of org.apache.ibatis.reflection.MetaObject in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method prepareCompositeKeyParameter.

private Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping resultMapping, Class<?> parameterType, String columnPrefix) throws SQLException {
    final Object parameterObject = instantiateParameterObject(parameterType);
    final MetaObject metaObject = configuration.newMetaObject(parameterObject);
    boolean foundValues = false;
    for (ResultMapping innerResultMapping : resultMapping.getComposites()) {
        final Class<?> propType = metaObject.getSetterType(innerResultMapping.getProperty());
        final TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(propType);
        final Object propValue = typeHandler.getResult(rs, prependPrefix(innerResultMapping.getColumn(), columnPrefix));
        // issue #353 & #560 do not execute nested query if key is null
        if (propValue != null) {
            metaObject.setValue(innerResultMapping.getProperty(), propValue);
            foundValues = true;
        }
    }
    return foundValues ? parameterObject : null;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) ResultMapping(org.apache.ibatis.mapping.ResultMapping) MetaObject(org.apache.ibatis.reflection.MetaObject)

Aggregations

MetaObject (org.apache.ibatis.reflection.MetaObject)27 ExecutorException (org.apache.ibatis.executor.ExecutorException)6 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)5 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)5 SQLException (java.sql.SQLException)4 CacheKey (org.apache.ibatis.cache.CacheKey)3 ResultMapping (org.apache.ibatis.mapping.ResultMapping)3 TypeHandler (org.apache.ibatis.type.TypeHandler)3 CacheException (org.apache.ibatis.cache.CacheException)2 ResultLoaderMap (org.apache.ibatis.executor.loader.ResultLoaderMap)2 Configuration (org.apache.ibatis.session.Configuration)2 JdbcType (org.apache.ibatis.type.JdbcType)2 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)2 Page (com.github.pagehelper.Page)1 PageException (com.github.pagehelper.PageException)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Map (java.util.Map)1 Properties (java.util.Properties)1 InitializingObject (org.apache.ibatis.builder.InitializingObject)1