mybatis last_insert_id() 问题

新增一条数据后,数据库表里面的ID自增是正常的,但是每次返回到应用系统这边的ID永远都是1,什么原因?

版本:TIDB 4.0

select LAST_INSERT_ID(); 可以正常返回,可以在 mysql cli 中执行看是否返回正确

image

命令行也不行,用的是TIDB 4.0

你好,
这边测试了下,不指定 auto_increment 列可以正常返回的

image

按照你说的,建表不指定auto_increment,程序运行还是不行

不是这个意思,我这边复现的是 id int not null primary key auto_increment;insert into tbl values(default); select last_insert_id(); 结果正常,应该和一楼的环境是一样的。这边只是复述下测试条件。可以在 mybatis 上,看下是否何种方式可行,(同种方式在 mysql 中是否正常呢?)

有没有技术支持帮忙解决一下?

root@172.16.4.107 : (none) 17:02:21>use test
Database changed
root@172.16.4.107 : test 17:02:26>create table test_tbl (id int primary key auto_increment);
Query OK, 0 rows affected (1.06 sec)

root@172.16.4.107 : test 17:03:36>insert into test_tbl values (default);
Query OK, 1 row affected (0.04 sec)

root@172.16.4.107 : test 17:03:55>select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.01 sec)

root@172.16.4.107 : test 17:04:04>insert into test_tbl values (default);
Query OK, 1 row affected (0.01 sec)

root@172.16.4.107 : test 17:04:08>select last_insert_id();              
+------------------+
| last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)

root@172.16.4.107 : test 17:04:09>

请先看下以上操作是否可以正常返回吧,

image

如上图没问题,但是一到mybatis就不行了。。。苦恼中

image

请问在连接 mysql 数据库时,mybatis 此用法会出现像 tidb 的问题吗

hi,如何,请问目前问题定位到了吗~

mysql没问题,tidb还未找到问题或者解决方案

mysql driver 的版本是多少?

8.0.20,难道这个还跟驱动有关系?

换成 5.7.x 吧,目前 tidb 对 8.0 支持的还不是很完善

jdbc驱动没有5.7的版本

兼容 myusql 5.7 协议的 jdbc 的 版本即可,

hi,问下更滑 mysql jdbc 版本是否有解决呢?