MyBatisPlus 主鍵策略的實(shí)現(xiàn)(4種)
說(shuō)明
MyBatis Plus 集成了多種主鍵策略,幫助用戶快速生成主鍵。
- 雪花算法ID(默認(rèn)策略)(推薦)
- UUID
- 自增ID
- 用戶輸入ID(必須用戶每次插入數(shù)據(jù)時(shí),手動(dòng)傳入ID)
雪花算法ID:IdType.ASSIGN_ID(推薦)
默認(rèn)情況,全局使用的,就是雪花算法ID。也就是說(shuō),id字段在沒(méi)有指定任何主鍵策略時(shí),插入數(shù)據(jù)就是使用的雪花算法生成的ID。
注解
如果全局使用雪花算法ID,這個(gè)注解可以不加。
@TableId(type = IdType.ASSIGN_ID)
代碼
package com.example.web.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.ASSIGN_ID)
private Long id;
private String name;
private Integer age;
private String email;
} @Test
public void insert() {
User user = new User();
user.setName("趙一");
user.setAge(26);
user.setEmail("zhaoyi@example.com");
mapper.insert(user);
}效果


UUID:IdType.ASSIGN_UUID
注解
@TableId(type = IdType.ASSIGN_UUID)
代碼
package com.example.web.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.ASSIGN_UUID)
private Long id;
private String name;
private Integer age;
private String email;
} @Test
public void insert() {
User user = new User();
user.setName("趙一");
user.setAge(26);
user.setEmail("zhaoyi@example.com");
mapper.insert(user);
}效果


自增ID:IdType.AUTO
該類型請(qǐng)確保數(shù)據(jù)庫(kù)設(shè)置了 ID自增,否則無(wú)效(會(huì)報(bào)錯(cuò))。
報(bào)錯(cuò)信息查看文章最后的《報(bào)錯(cuò)示例》
注解
@TableId(type = IdType.AUTO)
代碼與測(cè)試
package com.example.web.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
} @Test
public void insert() {
User user = new User();
user.setName("趙一");
user.setAge(26);
user.setEmail("zhaoyi@example.com");
mapper.insert(user);
}效果


用戶輸入ID:IdType.INPUT
注解
@TableId(type = IdType.INPUT)
代碼
package com.example.web.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.INPUT)
private Long id;
private String name;
private Integer age;
private String email;
} @Test
public void insert() {
User user = new User();
user.setId(9L);
user.setName("趙一");
user.setAge(26);
user.setEmail("zhaoyi@example.com");
mapper.insert(user);
}效果
指定id之后,插入數(shù)據(jù)成功


未指定ID報(bào)錯(cuò)
如果不指定id,也就是 setId() 方法沒(méi)調(diào)用,會(huì)報(bào)錯(cuò):
Column ‘id’ cannot be null

補(bǔ)充:報(bào)錯(cuò)示例(IdType.AUTO)
當(dāng) MySQL 數(shù)據(jù)庫(kù)中的表,ID并沒(méi)有自增,但是代碼中的id是自增,此時(shí)新增一條數(shù)據(jù),會(huì)報(bào)錯(cuò)。
實(shí)體類
package com.example.web.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}插入數(shù)據(jù)方法
@Test
public void insert() {
User user = new User();
user.setName("趙一");
user.setAge(26);
user.setEmail("zhaoyi@example.com");
mapper.insert(user);
}報(bào)錯(cuò)信息

到此這篇關(guān)于MyBatisPlus 主鍵策略的實(shí)現(xiàn)(4種)的文章就介紹到這了,更多相關(guān)MyBatisPlus 主鍵策略內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
輕松學(xué)會(huì)使用JavaMail?API發(fā)送郵件
想要輕松學(xué)會(huì)使用JavaMail?API發(fā)送郵件嗎?本指南將帶你快速掌握這一技能,讓你能夠輕松發(fā)送電子郵件,無(wú)論是個(gè)人還是工作需求,跟著我們的步驟,很快你就可以在Java應(yīng)用程序中自如地處理郵件通信了!2023-12-12
部署springboot項(xiàng)目讀取外部配置文件實(shí)現(xiàn)過(guò)程
本文介紹了Spring Boot項(xiàng)目如何讀取外部配置文件,以實(shí)現(xiàn)應(yīng)用與配置分離,主要通過(guò)將配置文件放在jar包的同級(jí)目錄中,或通過(guò)啟動(dòng)命令指定配置文件路徑等方式實(shí)現(xiàn),這種方法可以避免每次部署時(shí)修改配置文件的麻煩2026-04-04
基于數(shù)據(jù)庫(kù)的用戶認(rèn)證實(shí)現(xiàn)SpringBoot3整合SpringSecurity6的過(guò)程
這篇文章主要介紹了基于數(shù)據(jù)庫(kù)的用戶認(rèn)證實(shí)現(xiàn)SpringBoot3整合SpringSecurity6的過(guò)程,本文通過(guò)實(shí)例圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-05-05
springboot讀取resource配置文件生成容器對(duì)象的示例代碼
這篇文章主要介紹了springboot讀取resource配置文件生成容器對(duì)象的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
jdk7 中HashMap的知識(shí)點(diǎn)總結(jié)
HashMap的原理是老生常談了,不作仔細(xì)解說(shuō)。一句話概括為HashMap是一個(gè)散列表,它存儲(chǔ)的內(nèi)容是鍵值對(duì)(key-value)映射。這篇文章主要總結(jié)了關(guān)于jdk7 中HashMap的知識(shí)點(diǎn),需要的朋友可以參考借鑒,一起來(lái)看看吧。2017-01-01
一文詳解Java中的動(dòng)態(tài)填充Html模版并轉(zhuǎn)PDF
在后端技術(shù)中,模板引擎和PDF生成工具是兩個(gè)非常重要的領(lǐng)域,Thymeleaf和wkhtmltopdf是這兩個(gè)領(lǐng)域的杰出代表,下面就來(lái)詳細(xì)介紹一下Thymeleaf和wkhtmltopdf的技術(shù)特點(diǎn)吧2023-12-12
Jenkins build記錄清理,釋放磁盤(pán)空間方式
Jenkins構(gòu)建記錄會(huì)占用大量磁盤(pán)空間,需要定期清理,可以通過(guò)JenkinsJob配置中的“丟棄舊的構(gòu)建”(DiscardOldBuilds)來(lái)清理2025-11-11

