MyBatis SQLIntegrityConstraintViolationException: Column ‘update_time‘ cannot be null问题解决
问题
在ELK日志查询平台看到如下报错信息:
org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'update_time' cannot be null
### The error may exist in class path resource [mapper/WidgetMapper.xml]
### The error may involve com.johnny.common.mapper.WidgetMapper.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE widget SET update_time = # {updateTime}, update_user_id = ? where widget_id = ?
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'update_time' cannot be null
; Column 'update_time' cannot be null; nested exception is java.sql.SQLIntegrityConstraintViolationException: Column 'update_time' cannot be null
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:87)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
查找到报错的MyBatis的mapper.xml
代码片段如下:
<update id="update" parameterType="com.johnny.common.po.Widget">
UPDATE widget
<set>
<if test="updateTime != null and updateTime !=''">
update_time = # {updateTime},
</if>
<if test="updateUserId != null and updateUserId !=''">
update_user_id = #{updateUserId,jdbcType=VARCHAR},
</if>
</set>
where widget_id = #{widgetId}
</update>
排查
本地调试虽然不能发现复现问题,但是也有报错如下:
具体的报错信息:
Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'update_time' at row 1
### The error may exist in file [D:\code\cbdbi\cloud-aaa\cloud-aaa-service\target\classes\mapper\WidgetMapper.xml]
### The error may involve com.johnny.common.mapper.WidgetMapper.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE widget SET update_time = # {updateTime}, update_user_id = ? where widget_id = ?
### Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'update_time' at row 1
; Data truncation: Incorrect datetime value: '1' for column 'update_time' at row 1; nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'update_time' at row 1
从截图来看,updateTime是有数据的,实体类的命名也没有问题。
细心看报错信息,不难猜出报错原因大概率是多了个空格:
update_time = # {updateTime}
Google随便看了2篇,与我的情况明显不符。事实上,外国程序员很少使用MyBatis的。
把#
后面的空格去掉,本地调试,不再继续报错:Data truncation: Incorrect datetime value: '1' for column 'update_time' at row 1;
。
生产还未发布,但是基本上可以确定,生产环境的问题:SQLIntegrityConstraintViolationException: Column 'update_time' cannot be null
已经得到解决。
反思
- 为什么本地和生产环境的报错信息不一样?
- MyBatis这种多一个空格就报错问题为啥不妨碍它成为中国公司普遍首选?事实上,表结构变更等,MyBatis也得频繁修改。