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

本文探讨了MyBatis在处理NULL参数时的工作原理及BaseTypeHandler类的作用。特别是当未指定JdbcType时,MyBatis如何抛出异常,并在参数非NULL时调用PreparedStatement的不同方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看一看 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
### MyBatis 中 `JdbcType` 的用法 在 MyBatis 映射文件中,`JdbcType` 是用于指定 Java 类型和数据库 JDBC 类型之间转换的关键属性。当处理可能为空的结果集字段,设置 `JdbcType` 可以帮助防止潜在的异常。 #### 基本概念 MyBatis 使用 `JdbcType` 来定义如何将 SQL 查询结果中的列映射到 Java 对象的属性上。对于某些特定类型的字段(如日期、布尔值),显式声明 `JdbcType` 能够提高数据一致性和可靠性[^1]。 #### 配置方式 可以通过 XML 或者注解的方式配置 `JdbcType`: - **XML 方式** ```xml <resultMap id="userResultMap" type="User"> <id property="id" column="USER_ID" jdbcType="INTEGER"/> <result property="name" column="USERNAME" jdbcType="VARCHAR"/> </resultMap> ``` 在这个例子中,`jdbcType` 属性指定了每一列对应的 JDBC 数据类型,这有助于确保即使遇到 NULL 值也能正确解析并赋给相应的对象属性。 - **注解方式** ```java @Results({ @Result(property = "id", column = "USER_ID", jdbcType=JdbcType.INTEGER), @Result(property = "name", column = "USERNAME", jdbcType=JdbcType.VARCHAR) }) public List<User> selectUsers(); ``` 这里展示了通过注解来设定 `JdbcType` 的方法,适用于更简洁的应用场景下。 #### 处理空值的情况 为了更好地管理数据库中的 NULL 值,在 resultMap 定义里加入 `nullValue` 和 `typeHandler` 参数是非常有用的实践之一。例如: ```xml <result property="emailVerified" column="EMAIL_VERIFIED" jdbcType="BIT" nullValue="false" typeHandler="org.example.BooleanTypeHandler"/> ``` 这段代码片段说明了怎样利用自定义的 Type Handler 来控制 NULL 到默认值之间的转换逻辑。 #### 批量更新操作注意事项 需要注意的是,当执行批量更新语句,只有当 ExecutorType 设置为 BATCH 模式才会生效存储于 JDBC Driver 内部的一系列待提交命令,并可通过调用 flushStatements 方法强制刷新这些缓存的操作[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值