MyBatis-plus saveBatch()方法无法插入主键问题记录
问题产生:
我在进行书签表的插入功能测试时,将相关参数传递给saveBatch()进行批量插入,根据SpringBoot中打印的SQL中看到,执行了insert语句,但只插入了部分需要外部输入数据的字段,并有下列提示:
RETURNING """Id""" was aborted: ERROR: column ""Id"" does not exist
建议:Perhaps you meant to reference the column "DashboardBookmark.Id".
位置:181 Call getNextException to see other errors in the batch.
提示Id不存在,然后去debug输入的参数,确认数据无误,去检查数据库表结构,发现Id是主键,于是猜想是Id未插入导致报错。
问题解决方案:
解决思路:https://blog.csdn.net/zw521cx/article/details/99707298
该博客写道,被插入的表主键非自增的话,由外部插入主键,需要在设置实体类的主键字段时,@Table注解需要额外增加一个属性,如下所示:
@TableId(value = “xxx”,type = IdType.INPUT)
问题前后的SQL语句变化:
解决后:
INSERT INTO "DashboardBookmark" ( "Id", "CreateTime", "CreatorName", "DashboardId", "LastModifierName", "LastModifyTime", "Name" ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
解决前:
INSERT INTO "DashboardBookmark" ( "CreateTime", "CreatorName", "DashboardId", "LastModifierName", "LastModifyTime", "Name" ) VALUES ( ?, ?, ?, ?, ?, ? )