用mybatis-plus去insert数据时报错Cause: java.sql.SQLException: 无效的列类型: 1111
我的日常脑瘫报错环节
实体类错误相关字段
@ApiModelProperty(value = "报名时间")
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
private Long createTime;
@ApiModelProperty(value = "更新时间")
@TableField(value = "UPDATE_TIME", fill = FieldFill.INSERT_UPDATE)
private Long updateTime;
报错代码:
int insert = baseMapper.insert(trainingUser);
insert失败,并报错
org.mybatis.spring.MyBatisSystemException: nested exception is
org.apache.ibatis.type.TypeException: Could not set parameters for mapping:
ParameterMapping{property='createTime', mode=IN, javaType=class java.lang.Long,
jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null',
expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for
parameter #9 with JdbcType OTHER . Try setting a different JdbcType for this parameter
or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无
效的列类型: 1111
原因:insert时会自动填充时间,而mybatis-plus默认是LocalDateTime类型的,我这里是Long类型的,所以会发生时间类型不匹配问题;
解决办法:
添加一个MymetaObjectHandler类并实现metaObjectHandler接口
@Slf4j
@Component
public class MymetaObjectHandler implements metaObjectHandler {
@Override
public void insertFill(metaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", Integer.class, (int) (System.currentTimeMillis() / 1000));
this.strictInsertFill(metaObject, "updateTime", Integer.class, (int) (System.currentTimeMillis() / 1000));
//this.setFieldValByName("createTime", new Date().getTime() / 1000, metaObject);
//this.setFieldValByName("updateTime", new Date().getTime() / 1000, metaObject);
}
@Override
public void updateFill(metaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Integer.class, (int) (System.currentTimeMillis() / 1000));
//this.setFieldValByName("updateTime", new Date().getTime() / 1000, metaObject);
//this.strictUpdateFill(metaObject, "updateTime", Integer.class, new Date().getTime() / 1000);
}
}
这里指定对应的类型了,需要与他相对应,所以我的实体类的俩个字段需要改成Integer类型
@ApiModelProperty(value = "报名时间")
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
private Integer createTime;//类型与配置中的一致
@ApiModelProperty(value = "更新时间")
@TableField(value = "UPDATE_TIME", fill = FieldFill.INSERT_UPDATE)
private Integer updateTime;//类型与配置中的一致
改完之后就可以正常insert数据了
总结:就是字段的类型问题,如果发现这种错误,就去检查自己的SQL中的字段是否指定类型
或者检查自动填充时的字段是否跟配置或者默认的相对应



