软件编程
位置:首页>> 软件编程>> java编程>> MybatisPlus使用@TableId主键id自增长无效的解决

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

作者:Str_Null  发布时间:2023-01-30 15:59:41 

标签:MybatisPlus,@TableId,主键id

问题情况:

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

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

原因:

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

解决方法

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

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

修改之后就好了

MybatisPlus使用@TableId主键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());
   }
}

来源:https://blog.csdn.net/qq_47431361/article/details/122609698

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com