MyBatis SQLIntegrityConstraintViolationException: Column ‘‘ cannot be null问题解决

问题

在ELK日志查询平台看到如下报错信息:

org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Column '' 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
	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

从截图来看,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已经得到解决。

反思

  1. 为什么本地和生产环境的报错信息不一样?
  2. MyBatis这种多一个空格就报错问题为啥不妨碍它成为中国公司普遍首选?事实上,表结构变更等,MyBatis也得频繁修改。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

johnny233

晚饭能不能加鸡腿就靠你了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值