Search in sources :

Example 11 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class XMLMapperBuilder method parameterMapElement.

private void parameterMapElement(List<XNode> list) throws Exception {
    for (XNode parameterMapNode : list) {
        String id = parameterMapNode.getStringAttribute("id");
        String type = parameterMapNode.getStringAttribute("type");
        Class<?> parameterClass = resolveClass(type);
        List<XNode> parameterNodes = parameterMapNode.evalNodes("parameter");
        List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
        for (XNode parameterNode : parameterNodes) {
            String property = parameterNode.getStringAttribute("property");
            String javaType = parameterNode.getStringAttribute("javaType");
            String jdbcType = parameterNode.getStringAttribute("jdbcType");
            String resultMap = parameterNode.getStringAttribute("resultMap");
            String mode = parameterNode.getStringAttribute("mode");
            String typeHandler = parameterNode.getStringAttribute("typeHandler");
            Integer numericScale = parameterNode.getIntAttribute("numericScale");
            ParameterMode modeEnum = resolveParameterMode(mode);
            Class<?> javaTypeClass = resolveClass(javaType);
            JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
            @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
            ParameterMapping parameterMapping = builderAssistant.buildParameterMapping(parameterClass, property, javaTypeClass, jdbcTypeEnum, resultMap, modeEnum, typeHandlerClass, numericScale);
            parameterMappings.add(parameterMapping);
        }
        builderAssistant.addParameterMap(id, parameterClass, parameterMappings);
    }
}
Also used : XNode(org.apache.ibatis.parsing.XNode) ArrayList(java.util.ArrayList) JdbcType(org.apache.ibatis.type.JdbcType) ParameterMode(org.apache.ibatis.mapping.ParameterMode) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 12 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createRowKeyForMappedProperties.

private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey, List<ResultMapping> resultMappings, String columnPrefix) throws SQLException {
    for (ResultMapping resultMapping : resultMappings) {
        if (resultMapping.getNestedResultMapId() != null && resultMapping.getResultSet() == null) {
            // Issue #392
            final ResultMap nestedResultMap = configuration.getResultMap(resultMapping.getNestedResultMapId());
            createRowKeyForMappedProperties(nestedResultMap, rsw, cacheKey, nestedResultMap.getConstructorResultMappings(), prependPrefix(resultMapping.getColumnPrefix(), columnPrefix));
        } else if (resultMapping.getNestedQueryId() == null) {
            final String column = prependPrefix(resultMapping.getColumn(), columnPrefix);
            final TypeHandler<?> th = resultMapping.getTypeHandler();
            List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
            // Issue #114
            if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) {
                final Object value = th.getResult(rsw.getResultSet(), column);
                if (value != null || configuration.isReturnInstanceForEmptyRow()) {
                    cacheKey.update(column);
                    cacheKey.update(value);
                }
            }
        }
    }
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) List(java.util.List) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 13 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-3 by mybatis.

the class DefaultResultSetHandlerTest method shouldThrowExceptionWithColumnName.

@Test
public void shouldThrowExceptionWithColumnName() throws Exception {
    final MappedStatement ms = getMappedStatement();
    final RowBounds rowBounds = new RowBounds(0, 100);
    final DefaultResultSetHandler defaultResultSetHandler = new DefaultResultSetHandler(null, /*executor*/
    ms, null, /*parameterHandler*/
    null, /*resultHandler*/
    null, /*boundSql*/
    rowBounds);
    final ResultSetWrapper rsw = mock(ResultSetWrapper.class);
    when(rsw.getResultSet()).thenReturn(mock(ResultSet.class));
    final ResultMapping resultMapping = mock(ResultMapping.class);
    final TypeHandler typeHandler = mock(TypeHandler.class);
    when(resultMapping.getColumn()).thenReturn("column");
    when(resultMapping.getTypeHandler()).thenReturn(typeHandler);
    when(typeHandler.getResult(any(ResultSet.class), any(String.class))).thenThrow(new SQLException("exception"));
    List<ResultMapping> constructorMappings = Collections.singletonList(resultMapping);
    try {
        defaultResultSetHandler.createParameterizedResultObject(rsw, null, /*resultType*/
        constructorMappings, null, /*constructorArgTypes*/
        null, /*constructorArgs*/
        null);
        Assert.fail("Should have thrown ExecutorException");
    } catch (Exception e) {
        Assert.assertTrue("Expected ExecutorException", e instanceof ExecutorException);
        Assert.assertTrue("", e.getMessage().contains("mapping: " + resultMapping.toString()));
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultSet(java.sql.ResultSet) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) TypeHandler(org.apache.ibatis.type.TypeHandler) SQLException(java.sql.SQLException) ExecutorException(org.apache.ibatis.executor.ExecutorException) Test(org.junit.Test)

Example 14 with TypeHandler

use of org.apache.ibatis.type.TypeHandler in project mybatis-paginator by HuQingmiao.

the class DefaultParameterHandler method setParameters.

public void setParameters(PreparedStatement ps) throws SQLException {
    ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    if (parameterMappings != null) {
        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();
                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 {
                    value = metaObject == null ? null : metaObject.getValue(propertyName);
                }
                TypeHandler typeHandler = parameterMapping.getTypeHandler();
                if (typeHandler == null) {
                    throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
                }
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null)
                    jdbcType = configuration.getJdbcTypeForNull();
                typeHandler.setParameter(ps, i + 1, value, jdbcType);
            }
        }
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MetaObject(org.apache.ibatis.reflection.MetaObject) JdbcType(org.apache.ibatis.type.JdbcType) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Aggregations

TypeHandler (org.apache.ibatis.type.TypeHandler)14 JdbcType (org.apache.ibatis.type.JdbcType)7 ResultMapping (org.apache.ibatis.mapping.ResultMapping)6 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 MetaObject (org.apache.ibatis.reflection.MetaObject)5 ExecutorException (org.apache.ibatis.executor.ExecutorException)4 UnknownTypeHandler (org.apache.ibatis.type.UnknownTypeHandler)4 HashMap (java.util.HashMap)3 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)3 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 ResultFlag (org.apache.ibatis.mapping.ResultFlag)2 ResultMap (org.apache.ibatis.mapping.ResultMap)2 XNode (org.apache.ibatis.parsing.XNode)2 List (java.util.List)1 Map (java.util.Map)1 Arg (org.apache.ibatis.annotations.Arg)1 Case (org.apache.ibatis.annotations.Case)1 Result (org.apache.ibatis.annotations.Result)1