Search in sources :

Example 6 with JdbcType

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

the class XMLConfigBuilder method typeHandlerElement.

private void typeHandlerElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeHandlerPackage = child.getStringAttribute("name");
                typeHandlerRegistry.register(typeHandlerPackage);
            } else {
                String javaTypeName = child.getStringAttribute("javaType");
                String jdbcTypeName = child.getStringAttribute("jdbcType");
                String handlerTypeName = child.getStringAttribute("handler");
                Class<?> javaTypeClass = resolveClass(javaTypeName);
                JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
                Class<?> typeHandlerClass = resolveClass(handlerTypeName);
                if (javaTypeClass != null) {
                    if (jdbcType == null) {
                        typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
                    } else {
                        typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
                    }
                } else {
                    typeHandlerRegistry.register(typeHandlerClass);
                }
            }
        }
    }
}
Also used : XNode(org.apache.ibatis.parsing.XNode) JdbcType(org.apache.ibatis.type.JdbcType)

Example 7 with JdbcType

use of org.apache.ibatis.type.JdbcType 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 8 with JdbcType

use of org.apache.ibatis.type.JdbcType 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

JdbcType (org.apache.ibatis.type.JdbcType)8 TypeHandler (org.apache.ibatis.type.TypeHandler)7 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)3 XNode (org.apache.ibatis.parsing.XNode)3 HashMap (java.util.HashMap)2 MetaObject (org.apache.ibatis.reflection.MetaObject)2 UnknownTypeHandler (org.apache.ibatis.type.UnknownTypeHandler)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Case (org.apache.ibatis.annotations.Case)1 ExecutorException (org.apache.ibatis.executor.ExecutorException)1 ParameterMode (org.apache.ibatis.mapping.ParameterMode)1 ResultMapping (org.apache.ibatis.mapping.ResultMapping)1 ObjectTypeHandler (org.apache.ibatis.type.ObjectTypeHandler)1 TypeException (org.apache.ibatis.type.TypeException)1