欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MybatisPlus使用@TableId主鍵id自增長(zhǎng)無效的解決

 更新時(shí)間:2023年04月12日 15:50:14   作者:Str_Null  
本文主要介紹了MybatisPlus使用@TableId主鍵id自增長(zhǎng)無效的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

問題情況:

在使用 @TableId(type = IdType.AUTO)之后添加的id數(shù)字特別大

原因:

因?yàn)樵诘谝淮问褂玫臅r(shí)候沒有加注解 所以mybatis自動(dòng)生成了一個(gè)特別大的數(shù)字
當(dāng)我們第二次加上注解之后他的id實(shí)際上還是第一次那個(gè)特別大的數(shù)字+1

解決方法

修改表的自動(dòng)添加值再添加
因?yàn)榈谝淮翁砑拥膇d值特別大我就把那一行給刪了
然后改了自增長(zhǎng)的數(shù)字
如圖所示

修改之后就好了

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類型為自增長(zhǎng)
    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;
    上面這一行報(bào)錯(cuò)是正?,F(xiàn)象

    @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());
    }
}

到此這篇關(guān)于MybatisPlus使用@TableId主鍵id自增長(zhǎng)無效的解決的文章就介紹到這了,更多相關(guān)MybatisPlus @TableId主鍵id自增長(zhǎng)無效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論