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>;

相关文章:

  • 2021-10-31
  • 2022-12-23
  • 2022-02-24
  • 2021-05-13
  • 2021-12-14
  • 2022-12-23
  • 2020-03-18
猜你喜欢
  • 2021-06-22
  • 2022-12-23
  • 2019-08-30
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案