MybatisPlus使用@TableId主键id自增长无效的解决

本文主要介绍了MybatisPlus使用@TableId主键id自增长无效的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

问题情况:

在使用 @TableId(type = IdType.AUTO)之后添加的id数字特别大

原因:

因为在第一次使用的时候没有加注解 所以mybatis自动生成了一个特别大的数字
当我们第二次加上注解之后他的id实际上还是第一次那个特别大的数字+1

解决方法

修改表的自动添加值再添加
因为第一次添加的id值特别大我就把那一行给删了
然后改了自增长的数字
如图所示

修改之后就好了

package com.tong.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @TableName("tb_user") public class User { @TableId(type = IdType.AUTO) //指定id类型为自增长 private Long id; private String user_name; private String password; private String name; private Integer age; private String email; } 
package org.example; import com.tong.MyApplication; import com.tong.mapper.UserMapper; import com.tong.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes= MyApplication.class) public class TestUserMapper { @Autowired private UserMapper userMapper; 上面这一行报错是正常现象 @Test public void test(){ User user = new User(); user.setEmail("12345.com"); user.setAge(20); user.setUser_name("caocao1"); user.setName("曹操1"); user.setPassword("123456"); //user.setAddress("北京"); int insert = userMapper.insert(user); System.out.println(insert); System.out.println(user.getId()); } } 

到此这篇关于MybatisPlus使用@TableId主键id自增长无效的解决的文章就介绍到这了,更多相关MybatisPlus @TableId主键id自增长无效内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是MybatisPlus使用@TableId主键id自增长无效的解决的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java