关于updateByPrimaryKeySelective(T)主键异常问题
Author: SMSshuaiCreated Feb 9, 2023Updated Dec 11, 2025
通过updateByPrimaryKeySelective(T)方法更新数据的时候主键ID会变化
数据库主键ID自增 如id =10 ,11, 12,13 ,因业务需求删除了其中一个id =11。 在执行updateByPrimaryKeySelective方法是传入的数据id是12 ,但是执行成功后,实体内的id变成了11(这个id数据不存在,实体内的其他数据都正常)。 后续测试后发现,执行该方法后如果该条数据的id大于空缺数据的id就会返回空数据id。 比如:删除了id11 更新id<11的数据都正常。 更新id>11的数据都会返回id=11. 如果删除多条数据,会返回正序第一个被删掉的id。
实体增加了如下注解 @Id @KeySql(useGeneratedKeys = true) @Column(insertable = false,updatable = false) @ApiModelProperty(value="id") private Long id;
打印出来的执行代码如下 UPDATE sys_configye SET business_name = 'AppHomeStyle',business_code = 'rule',begin_time = '2022-01-01 15:08:43.0',end_time = '2030-01-12 15:08:44.0',type = 0,content_A = 4,content_B = 1,content_C = '通知公告',content_D = 'notification' WHERE id = 12;
Source: abel533/Mapper