【踩坑记录】mybatis-plus的insert方法,默认会生成一个uuid作为主键,导致类型不一致,存入数据库报错

报错记录

Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1
; Data truncation: Out of range value for column 'id' at row 1; nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1

在使用 mybatis-plus的insert方法,底层会默认生成一个Long类型的UUID,用这个作为主键id。这就导致跟数据库里面类型不一致导致错误。

解决方案

推荐方案

在主键上加上@TableId(value = "id",type = IdType.INPUT)@TableId(value = "id",type = IdType.AUTO)

	// 这种方式是主键手动输入
    @TableId(value = "id",type = IdType.INPUT)
    private Long id;
    
	/** 主键生成方式类型如下(IdType):
	1.AUTO(0, “数据库ID自增”),
	2.INPUT(1, “用户输入ID”),
	3.ID_WORKER(2, “全局唯一ID”),
	4.UUID(3, “全局唯一ID”),
	5.NONE(4, “该类型为未设置主键类型”),
	6.ID_WORKER_STR(5, “字符串全局唯一ID”);
	**/

使用uuid作为主键,修改id的类型为bigint

在这里插入图片描述

Logo

一站式 AI 云服务平台

更多推荐