MybatisPlus公共字段填充踩坑【DataIntegrityViolationException】

错误

利用公共字段填充日期时出现错误

org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'create_time' cannot be null
### The error may exist in com/qst/api/mapper/UserMapper.java (best guess)
### The error may involve com.qst.api.mapper.UserMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO user  ( username, password, sex, phone, email, birth,  create_time, update_time )  VALUES  ( ?, ?, ?, ?, ?, ?,  ?, ? )
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'create_time' cannot be null

报错说日期为空,检查了实体类的日期,没出错,是LocalDateTime类型,注解 @TableField(value = “create_time” ,fill = FieldFill.INSERT)也没有错误。
自动填充数据对象处理器:

@Component
public class MyMetaObjecthandler implements MetaObjectHandler {
    /*
    * 插入操作自动填充
    * */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
    }

    /*
    * 更新自动填充
    * */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]");
        metaObject.setValue("updateTime", LocalDateTime.now());
    }
}

同样没有错误,@Component也有,但是还是报错。

原因

找了好久原因,最后是因为我同时使用了mybatis和mybatisPlus,所以MybatisPlus公共字段填充失效。

解决方案

把Mybatis依赖删除,只使用mybatisPlus,在yml中把mybatis的配置改成mybatisplus的即可

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

String_name_null

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值