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

编程入门 行业动态 更新时间:2024-10-28 17:22:35

ID突然变成<a href=https://www.elefans.com/category/jswz/34/1719849.html style=很长且随机的雪花算法ID了"/>

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)

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

更多推荐

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

本文发布于:2023-11-16 15:37:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:很长   算法   雪花   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!