MyBatis 插入空值时,需要指定JdbcType

看一看 org.apache.ibatis.type.BaseTypeHandler

@Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
      }
      try {
        ps.setNull(i, jdbcType.TYPE_CODE);
      } catch (SQLException e) {
        throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " +
                "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " +
                "Cause: " + e, e);
      }
    } else {
      try {
        setNonNullParameter(ps, i, parameter, jdbcType);
      } catch (Exception e) {
        throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . " +
                "Try setting a different JdbcType for this parameter or a different configuration property. " +
                "Cause: " + e, e);
      }
    }
  }

子类 BigIntegerTypeHandlersetNonNullParameter 的实现如下:

@Override
  public void setNonNullParameter(PreparedStatement ps, int i, BigInteger parameter, JdbcType jdbcType) throws SQLException {
    ps.setBigDecimal(i, new BigDecimal(parameter));
  }

可以看出,当参数的值为 NULL,且没有指定 jdbcType 时,mybatis会抛出 “JDBC requires that the JdbcType must be specified for all nullable parameters.” 异常。

而当参数不为 NULL 时,Mybatis 会针对参数自身的类型调用 PreparedStatement 上不同的方法,此时 JdbcType 则是可以省略的。

再看 JDBC 中PreparedStatement 的 setNull 方法就不难明白,JDBC 要求我们设置 NULL 参数时必须指定 JDBC 类型,Mybatis 只是照章办事而已。

void setNull(int parameterIndex,
             int sqlType)
             throws SQLException
Sets the designated parameter to SQL NULL.
Note: You must specify the parameter's SQL type.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
sqlType - the SQL type code defined in java.sql.Types
Throws:
SQLException - if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closed PreparedStatement
SQLFeatureNotSupportedException - if sqlType is a ARRAY, BLOB, CLOB, DATALINK, JAVA_OBJECT, NCHAR, NCLOB, NVARCHAR, LONGNVARCHAR, REF, ROWID, SQLXML or STRUCT data type and the JDBC driver does not support this data type

你可能感兴趣的:(Java)