错误

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

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

版权声明:本文为qq_45847507原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_45847507/article/details/127396094