Search in sources :

Example 1 with PropertyTokenizer

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

the class MetaObject method setValue.

public void setValue(String name, Object value) {
    PropertyTokenizer prop = new PropertyTokenizer(name);
    if (prop.hasNext()) {
        MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());
        if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
            if (value == null && prop.getChildren() != null) {
                // don't instantiate child path if value is null
                return;
            } else {
                metaValue = objectWrapper.instantiatePropertyValue(name, prop, objectFactory);
            }
        }
        metaValue.setValue(prop.getChildren(), value);
    } else {
        objectWrapper.set(prop, value);
    }
}
Also used : PropertyTokenizer(org.apache.ibatis.reflection.property.PropertyTokenizer)

Example 2 with PropertyTokenizer

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

the class MetaClass method buildProperty.

private StringBuilder buildProperty(String name, StringBuilder builder) {
    PropertyTokenizer prop = new PropertyTokenizer(name);
    if (prop.hasNext()) {
        String propertyName = reflector.findPropertyName(prop.getName());
        if (propertyName != null) {
            builder.append(propertyName);
            builder.append(".");
            MetaClass metaProp = metaClassForProperty(propertyName);
            metaProp.buildProperty(prop.getChildren(), builder);
        }
    } else {
        String propertyName = reflector.findPropertyName(name);
        if (propertyName != null) {
            builder.append(propertyName);
        }
    }
    return builder;
}
Also used : PropertyTokenizer(org.apache.ibatis.reflection.property.PropertyTokenizer)

Example 3 with PropertyTokenizer

use of org.apache.ibatis.reflection.property.PropertyTokenizer in project mybatis.flying by limeng32.

the class AutoMapperInterceptor method setParameters.

@SuppressWarnings("unchecked")
private 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);
                }
                TypeHandler<Object> typeHandler = (TypeHandler<Object>) parameterMapping.getTypeHandler();
                if (typeHandler == null) {
                    throw new AutoMapperException(new StringBuffer(AutoMapperExceptionEnum.noTypeHandlerSuitable.toString()).append(propertyName).append(" of statement ").append(mappedStatement.getId()).toString());
                }
                typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
            }
        }
    }
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) PropertyTokenizer(org.apache.ibatis.reflection.property.PropertyTokenizer) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) AutoMapperException(indi.mybatis.flying.exception.AutoMapperException) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Aggregations

PropertyTokenizer (org.apache.ibatis.reflection.property.PropertyTokenizer)3 AutoMapperException (indi.mybatis.flying.exception.AutoMapperException)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 Configuration (org.apache.ibatis.session.Configuration)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1