【问题标题】:Insert null in a database在数据库中插入空值
【发布时间】:2013-04-17 23:06:57
【问题描述】:

我使用select 语句检索了List<SomeBean>。现在,我正在尝试insert 相同。这个插入语句工作正常;但是,我无法插入空值。该表没有任何NOT NULL 约束。引发以下异常:

org.springframework.jdbc.UncategorizedSQLException: Error setting null for parameter #4 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: Invalid column type

如何在mybatis中插入空值?

【问题讨论】:

  • 如果你没有在insert 语句中设置它的值会发生什么???!!!

标签: java oracle mybatis


【解决方案1】:

当在 Prepared Statement 或 Callable Statement 中设置空参数时,MyBatis 需要知道 jdbc 类型。像这样:

#{myNullParamenter, jdbcType=VARCHAR2}

【讨论】:

  • 谢谢!在 mybatis 教程中经历了同样的事情。NOTE The JDBC Type is required by JDBC for all nullable columns, if null is passed as a value. You can investigate this yourself by reading the JavaDocs for the PreparedStatement.setNull() method.
【解决方案2】:

将此行插入MyBatis的配置文件中:

<settings>
        <setting name="jdbcTypeForNull" value="NULL" />
</settings>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多