auto 数据库需要设置自增,会自动获取自增结果

Originally posted by @qmdx in https://github.com/baomidou/mybatis-plus/issues/2054#issuecomment-571870493

id列已经配置成自增了 id bigint auto_increment primary key,

Comment From: miemieYaho

给出你的复现demo

Comment From: haoyousb

建表语句: create table test ( id bigint auto_increment primary key, nick_name varchar(255) not null comment '用户昵称' ) comment '测试表'

测试代码:

    Test t1 = new Test();
    t1.setNickName("111");
    testService.save(t1);
    System.out.println("-用Service插入数据-->" + t1.getId());

    Test t2 = new Test();
    t2.setNickName("2222");
    testMapper.insert(t2);
    System.out.println("-用Mapper插入数据-->" + t2.getId());

实体类(不让传java文件 我改成txt了)

Test.txt

Comment From: haoyousb

这个是我这边的运行结果

1578454839282

Comment From: miemieYaho

https://github.com/baomidou/mybatis-plus/blob/3.0/mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java 无法复现 1. 注释掉 build.gradle 180行 2. 修改 DBConfig 44-46行数据库连接信息 3. 直接运行 Test 94行的method

Comment From: haoyousb

关于第三步 :直接运行 Test 94行的method

第103行不要用 mysqlMapper.insert 改用mysqlDataService.save

成功之后 id是空的

Comment From: haoyousb

mapper的insert方法是有效的

service的save方法有问题

Comment From: miemieYaho

service就是调用的mapper的insert

Comment From: haoyousb

您这边方便用MysqlDataService试一下么

Comment From: miemieYaho

要试你自己试,试出来了你就写个demo传git上发出来看看