Search in sources :

Example 31 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project Mybatis-PageHelper by pagehelper.

the class MySqlDialect method processPageParameter.

@Override
public Object processPageParameter(MappedStatement ms, Map<String, Object> paramMap, Page page, BoundSql boundSql, CacheKey pageKey) {
    paramMap.put(PAGEPARAMETER_FIRST, page.getStartRow());
    paramMap.put(PAGEPARAMETER_SECOND, page.getPageSize());
    // 处理pageKey
    pageKey.update(page.getStartRow());
    pageKey.update(page.getPageSize());
    // 处理参数配置
    if (boundSql.getParameterMappings() != null) {
        List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>(boundSql.getParameterMappings());
        if (page.getStartRow() == 0) {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build());
        } else {
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_FIRST, long.class).build());
            newParameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), PAGEPARAMETER_SECOND, int.class).build());
        }
        MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
        metaObject.setValue("parameterMappings", newParameterMappings);
    }
    return paramMap;
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) ArrayList(java.util.ArrayList)

Example 32 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping 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 {
    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 StringBuilder(AutoMapperExceptionEnum.NO_TYPE_HANDLER_SUITABLE.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)

Example 33 with ParameterMapping

use of org.apache.ibatis.mapping.ParameterMapping in project paascloud-master by paascloud.

the class SqlLogInterceptor method getParamList.

/**
 * 获取sql参数集合。
 *
 * @param configuration the configuration
 * @param boundSql      the bound sql
 *
 * @return the param list
 */
private List<String> getParamList(Configuration configuration, BoundSql boundSql) {
    Object parameterObject = boundSql.getParameterObject();
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    List<String> params = new ArrayList<>();
    if (parameterMappings.size() > 0 && parameterObject != null) {
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
            params.add(getParameterValue(parameterObject));
        } else {
            MetaObject metaObject = configuration.newMetaObject(parameterObject);
            for (ParameterMapping parameterMapping : parameterMappings) {
                String propertyName = parameterMapping.getProperty();
                if (metaObject.hasGetter(propertyName)) {
                    Object obj = metaObject.getValue(propertyName);
                    params.add(getParameterValue(obj));
                } else if (boundSql.hasAdditionalParameter(propertyName)) {
                    Object obj = boundSql.getAdditionalParameter(propertyName);
                    params.add(getParameterValue(obj));
                }
            }
        }
    }
    return params;
}
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)

Example 34 with ParameterMapping

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

the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithBeforeAutoKey.

static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<>()).build();
    MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config, "SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT).keyProperty("id").resultMaps(new ArrayList<ResultMap>() {

        {
            add(rm);
        }
    }).build();
    config.addMappedStatement(kms);
    return new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.class)).build());
            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(new SelectKeyGenerator(kms, true)).keyProperty("id").build();
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) ArrayList(java.util.ArrayList) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) Section(org.apache.ibatis.domain.blog.Section) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 35 with ParameterMapping

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

the class DefaultResultSetHandler method handleOutputParameters.

// 
// HANDLE OUTPUT PARAMETER
// 
@Override
public void handleOutputParameters(CallableStatement cs) throws SQLException {
    final Object parameterObject = parameterHandler.getParameterObject();
    final MetaObject metaParam = configuration.newMetaObject(parameterObject);
    final List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    for (int i = 0; i < parameterMappings.size(); i++) {
        final ParameterMapping parameterMapping = parameterMappings.get(i);
        if (parameterMapping.getMode() == ParameterMode.OUT || parameterMapping.getMode() == ParameterMode.INOUT) {
            if (ResultSet.class.equals(parameterMapping.getJavaType())) {
                handleRefCursorOutputParameter((ResultSet) cs.getObject(i + 1), parameterMapping, metaParam);
            } else {
                final TypeHandler<?> typeHandler = parameterMapping.getTypeHandler();
                metaParam.setValue(parameterMapping.getProperty(), typeHandler.getResult(cs, i + 1));
            }
        }
    }
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject)

Aggregations

ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)40 MetaObject (org.apache.ibatis.reflection.MetaObject)18 ArrayList (java.util.ArrayList)17 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)15 MappedStatement (org.apache.ibatis.mapping.MappedStatement)9 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)8 Configuration (org.apache.ibatis.session.Configuration)5 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)4 StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)4 JdbcType (org.apache.ibatis.type.JdbcType)4 TypeHandler (org.apache.ibatis.type.TypeHandler)4 StringJoiner (java.util.StringJoiner)3 Section (org.apache.ibatis.domain.blog.Section)3 SQL (org.apache.ibatis.jdbc.SQL)3 BoundSql (org.apache.ibatis.mapping.BoundSql)3 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)2 Conditionable (indi.mybatis.flying.models.Conditionable)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 List (java.util.List)2