很长且随机的雪花算法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了
发布评论