这篇文章主要介绍“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自增长无效如何解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://blog.csdn.net/qq_47431361/article/details/122609698