Search in sources :

Example 6 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithAutoKey.

public static MappedStatement prepareInsertAuthorMappedStatementWithAutoKey(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new StaticSqlSource(config, "INSERT INTO author (username,password,email,bio,favourite_section) values(?,?,?,?,?)"), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
            add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
        }
    }).build()).cache(authorCache).keyGenerator(Jdbc3KeyGenerator.INSTANCE).keyProperty("id").build();
    return ms;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) Section(org.apache.ibatis.domain.blog.Section)

Example 7 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.

the class MapperBuilderAssistant method getStatementParameterMap.

private ParameterMap getStatementParameterMap(String parameterMapName, Class<?> parameterTypeClass, String statementId) {
    parameterMapName = applyCurrentNamespace(parameterMapName, true);
    ParameterMap parameterMap = null;
    if (parameterMapName != null) {
        try {
            parameterMap = configuration.getParameterMap(parameterMapName);
        } catch (IllegalArgumentException e) {
            throw new IncompleteElementException("Could not find parameter map " + parameterMapName, e);
        }
    } else if (parameterTypeClass != null) {
        List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
        parameterMap = new ParameterMap.Builder(configuration, statementId + "-Inline", parameterTypeClass, parameterMappings).build();
    }
    return parameterMap;
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ParameterMap(org.apache.ibatis.mapping.ParameterMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping 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 9 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-3 by mybatis.

the class BaseExecutor method createCacheKey.

@Override
public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
    if (closed) {
        throw new ExecutorException("Executor was closed.");
    }
    CacheKey cacheKey = new CacheKey();
    cacheKey.update(ms.getId());
    cacheKey.update(rowBounds.getOffset());
    cacheKey.update(rowBounds.getLimit());
    cacheKey.update(boundSql.getSql());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
    // mimic DefaultParameterHandler logic
    for (ParameterMapping parameterMapping : parameterMappings) {
        if (parameterMapping.getMode() != ParameterMode.OUT) {
            Object value;
            String propertyName = parameterMapping.getProperty();
            if (boundSql.hasAdditionalParameter(propertyName)) {
                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);
            }
            cacheKey.update(value);
        }
    }
    if (configuration.getEnvironment() != null) {
        // issue #176
        cacheKey.update(configuration.getEnvironment().getId());
    }
    return cacheKey;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 10 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project mybatis-paginator by HuQingmiao.

the class Dialect method init.

protected void init() {
    boundSql = mappedStatement.getBoundSql(parameterObject);
    parameterMappings = new ArrayList(boundSql.getParameterMappings());
    if (parameterObject instanceof Map) {
        pageParameters.putAll((Map) parameterObject);
    } else {
        for (ParameterMapping parameterMapping : parameterMappings) {
            pageParameters.put(parameterMapping.getProperty(), parameterObject);
        }
    }
    StringBuffer bufferSql = new StringBuffer(boundSql.getSql().trim());
    if (bufferSql.lastIndexOf(";") == bufferSql.length() - 1) {
        bufferSql.deleteCharAt(bufferSql.length() - 1);
    }
    String sql = bufferSql.toString();
    //替换制表符
    pageSQL = sql.replaceAll("\t", " ").trim();
    //合并空格符
    pageSQL = pageSQL.replaceAll("\\s{1,}", " ");
    if (pageBounds.getOrders() != null && !pageBounds.getOrders().isEmpty()) {
        pageSQL = getSortString(sql, pageBounds.getOrders());
    }
    if (pageBounds.getOffset() != RowBounds.NO_ROW_OFFSET || pageBounds.getLimit() != RowBounds.NO_ROW_LIMIT) {
        pageSQL = getLimitString(pageSQL, pageBounds.getOffset(), pageBounds.getLimit());
    }
    if (pageBounds.isIfCount()) {
        countSQL = getCountString(sql);
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)17 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)9 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)8 MappedStatement (org.apache.ibatis.mapping.MappedStatement)8 ArrayList (java.util.ArrayList)6 MetaObject (org.apache.ibatis.reflection.MetaObject)5 Section (org.apache.ibatis.domain.blog.Section)3 JdbcType (org.apache.ibatis.type.JdbcType)3 TypeHandler (org.apache.ibatis.type.TypeHandler)3 ResultMap (org.apache.ibatis.mapping.ResultMap)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CacheKey (org.apache.ibatis.cache.CacheKey)1 Author (org.apache.ibatis.domain.blog.Author)1 ExecutorException (org.apache.ibatis.executor.ExecutorException)1 SelectKeyGenerator (org.apache.ibatis.executor.keygen.SelectKeyGenerator)1 ParameterMap (org.apache.ibatis.mapping.ParameterMap)1 ParameterMode (org.apache.ibatis.mapping.ParameterMode)1