微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这篇文章主要介绍“MybatisPlus使用@TableId主键id自增长无效如何解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“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());
    }
}

关于“MybatisPlus使用@TableId主键id自增长无效如何解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程之家行业资讯频道,小编每天都会为大家更新不同的知识点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐