确认

  • [X] 我使用的版本是最新版, 并且使用插件确认过项目里无依赖版本冲突
  • [X] 我已经在 issue 中搜索过, 确认问题没有被提出过
  • [X] 我已经修改标题, 将标题中的 描述 替换为遇到的问题

当前程序版本

3.5.9

问题描述

SpringBoot : 3.3.3 1. yaml文件配置如下:

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
      table-prefix: t_
      logic-delete-field: deleted # 全局逻辑删除字段名
      logic-delete-value: 1 # 逻辑已删除值
      logic-not-delete-value: 0 # 逻辑未删除值
  configuration:
    map-underscore-to-camel-case: false
  1. 表结构如下:
create table if not exists t_video
(
    id                   integer primary key auto_increment,
    title                varchar(50) comment '标题',
    create_at            datetime       not null default current_timestamp,
    update_at            datetime       not null default current_timestamp on update current_timestamp
) CHARACTER SET utf8mb4 comment '视频表';
  1. MyMetaObjectHandler如下:
@Component
class MyMetaObjectHandler : MetaObjectHandler {

    override fun insertFill(metaObject: MetaObject?) {
        StaticLog.info("start insert fill ....")
        this.strictInsertFill(
            metaObject, "create_at",
            LocalDateTime::class.java, LocalDateTime.now()
        )
    }

    override fun updateFill(metaObject: MetaObject?) {
        StaticLog.info("start update fill ....")
        this.strictUpdateFill(
            metaObject, "update_at",
            LocalDateTime::class.java, LocalDateTime.now()
        )
    }
}
  1. domain代码如下:
data class Video(
        var id: Long? = null,
        var title: String? = null,

        @TableField(fill = FieldFill.INSERT)
        var create_at: Date? = null,

        @JsonIgnore
        @TableField(fill = FieldFill.UPDATE)
        var update_at: Date? = null,
 ) 

插入的时候,只要我不给create_at和update_at赋值就会报错如下:

### The error occurred while setting parameters
### SQL: INSERT INTO t_video  ( title,               create_at )  VALUES (  ?,               ?  )
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'create_at' cannot be null
; Column 'create_at' cannot be null

MyMetaObjectHandler中的日志并没有执行,太奇怪了。

详细堆栈日志

### The error occurred while setting parameters
### SQL: INSERT INTO t_video  ( title,               create_at )  VALUES (  ?,               ?  )
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'create_at' cannot be null
; Column 'create_at' cannot be null
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:97)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:107)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:116)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:93)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:347)
    at jdk.proxy2/jdk.proxy2.$Proxy81.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:224)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:59)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:152)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
    at jdk.proxy2/jdk.proxy2.$Proxy89.insert(Unknown Source)

Comment From: junixapp

原来是Date的问题,换成LocalDateTime就可以了。。。。