ID突然变成很长且随机的雪花算法ID了

经检查,先排除是数据库的问题。

drop table if exists `product_resources`;
create table `product_resources`  (
`id` bigint not null auto_increment,
primary key (`id`)
) engine = innodb auto_increment = 1 character set = utf8mb4 collate = utf8mb4_general_ci comment = '产品资源表';

在数据库客户端工具插入数据能正常插入递增的ID,但是需要先删除所有雪花ID的数据,然后再执行alter table product_resources AUTO_INCREMENT =1;

然后我找到mybatis的sql日志:
发现下面代码:

2022-09-21 15:32:23.061 [http-nio-9999-exec-2] DEBUG com.xxx.ProductResourcesMapper.insert - ==>  Preparing: INSERT INTO product_resources ( id, product_id, type, product_doc_id, attachment_id, remote_file_id, title, status, create_by, create_time, update_by, update_time, content ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) 
2022-09-21 15:32:23.062 [http-nio-9999-exec-2] DEBUG com.xxx.ProductResourcesMapper.insert - ==> Parameters: 1572488896689885186(Long), 2(Long), 1(Integer), 0(Long), 0(Long), 0(Long), 333(String), 1(Integer), 1(Long), 2022-09-21 15:32:23.061(Timestamp), 1(Long), 2022-09-21 15:32:23.061(Timestamp), <p>55555555555</p>(String)

这个1572488896689885186就是mybatis生成雪花ID,可恶!

	<insert id="insertSelective" parameterType="com.xxx.ProductResources" useGeneratedKeys="true" keyProperty="id">
		insert into `product_resources`(id, product_id, type, product_doc_id, attachment_id, remote_file_id, title, status, create_by, create_time, update_by, update_time, content)
		values(
			#{id,jdbcType=BIGINT},
			#{productId,jdbcType=BIGINT},
			#{type,jdbcType=TINYINT},
			#{productDocId,jdbcType=BIGINT},
			#{attachmentId,jdbcType=BIGINT},
			#{remoteFileId,jdbcType=BIGINT},
			#{title,jdbcType=VARCHAR},
			#{status,jdbcType=TINYINT},
			#{createBy,jdbcType=BIGINT},
			#{createTime,jdbcType=TIMESTAMP},
			#{updateBy,jdbcType=BIGINT},
			#{updateTime,jdbcType=TIMESTAMP})
	</insert>

实体类:

    @TableField("id")
    private Long id;

解决:
@TableField("id")的问题,换成@TableId(value="id",type = IdType.AUTO)

但问题的关键是,我之前一直都是那样写的为什么没出问题?都是数据库自增。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值