2013-04-24 37 views
2

我使用select语句检索了List<SomeBean>。现在,我试图insert一样。这个插入语句工作正常;但是,我无法插入空值。该表没有任何NOT NULL限制。以下异常引发:在数据库中插入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的插入空值?

+0

如果您没有设置它在你的'insert'声明???值会发生什么! – 2013-04-24 11:58:07

回答

5

将空参数设置为Prepared Statement或Callable Statement时MyBatis需要知道jdbc类型。就像这样:

#{myNullParamenter, jdbcType=VARCHAR2} 
+1

Thx!在mybatis教程中经历了相同的操作。注:如果将null作为值传递,JDBC类型对于所有可为空的列都是必需的。您可以通过阅读PreparedStatement.setNull()方法的JavaDocs来自行调查 – abc 2013-04-24 12:32:38

5

插入此行成的MyBatis的配置文件:

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