栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus去insert数据时报错Cause: java.sql.SQLException: 无效的列类型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus去insert数据时报错Cause: java.sql.SQLException: 无效的列类型

用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中的字段是否指定类型
或者检查自动填充时的字段是否跟配置或者默认的相对应

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号