[Mybatis]setting null for parameter #10 with JdbcType OTHER .


mybatis 插入空值時需要指定jdbcType
报错内容:
### Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #10 with JdbcType OTHER .
Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型
 
原因:
DefaultParameterHandler类在解析参数的时候,无法解析参数类型,指定默认的类型OTHER.
 public void setParameters(PreparedStatement ps) throws SQLException {
    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();
          typeHandler.setParameter(ps, i + 1, value, jdbcType);
        }
      }
    }
  } 
 
 解决办法:
  在insert或update语句中,增加jdbcType指定字段的类型
如:
  <![CDATA[
  insert into t_yp_province
  (fid,fname,fnumber,fsimpleName,fdescription,fcreateTime,flastUpdateTime,fdirect)
  values
  ( #{id,jdbcType=VARCHAR},
   #{name,jdbcType=VARCHAR},
   #{number,jdbcType=VARCHAR},
   #{simpleName,jdbcType=VARCHAR},
   #{description,jdbcType=VARCHAR},
   #{createTime,jdbcType=DATE},
   #{lastUpdateTime,jdbcType=DATE},
   #{direct,jdbcType=NUMERIC}
  )  
  ]]>
 </insert>;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM