项目场景:
在Oracle数据库和MySQL数据库添加null值
问题描述
1. Oracle:如果插入的字段是null,提示错误:
Error setting null for parameter #2 with JdbcType OTHER,
提示错误:应该是Other而不是null
2. MySQL:如果插入的字段是null,可以正常执行(没有约束)
原因分析:
各个数据库 在mybatis中 对各种数据类型的 默认值不一致。
mybatis中,jdbcTypeForNull(如果是null),则默认值输出为OTHER。插入到数据库中,对于这个Other,MySQL里面能够处理成(NULL),但是Oracle不行。
解决方案:
对于Oracle :把null -》other,需要手工告诉oracle :other就是null 即当某个数据类型Oracle无法处理时,告诉它用默认值null
<insert id="insertStudent" parameterType="Student" databaseId="oracle">
insert into student(grade,stuName,classId)
values(#{grade},#{stuName,jdbcType=NULL},#{classId});
</insert>