MyBatis-Plus枚舉和自定義主鍵ID的實現(xiàn)步驟
一、枚舉
當我們在開發(fā)時,在往數(shù)據(jù)庫表的某個屬性字段插入數(shù)據(jù)時,希望把這個值限定在一定的范圍,例如性別,只有男和女,而年級只有小學(xué),初中,高中。通過這樣子的規(guī)范可以讓我們的代碼看起來更加簡潔,MyBatis-Plus中也提供了這樣的一個功能。
??步驟一:編寫配置文件
#枚舉類所在的包 mybatis-plus.type-enums-package=com.yixin.myenum mybatis-plus.configuration.default-enum-type-handler=org.apache.ibatis.type.EnumOrdinalTypeHandler
??步驟二:編寫枚舉類
Tip:我們將枚舉類放在com.yixin.myenum這個包下。
@EnumValue的作用就是將我們的描述(小學(xué),中心,高中,大學(xué))插入數(shù)據(jù)庫,如果沒有這個注解,那么插入的就是枚舉前面的編號(1,2,3,4)。
package com.yixin.myenum;
import com.baomidou.mybatisplus.annotation.EnumValue;
public enum GradeEnum {
PRIMARY(1, "小學(xué)"),
SECONDORY(2, "中學(xué)"),
HIGH(3, "高中"),
UNIVERSITY(4, "大學(xué)");
private int code;
@EnumValue//描述作為枚舉值保存到數(shù)據(jù)庫
private String desc;
GradeEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}??步驟三:更改實體類
將我們的實體類的屬性類型更替為我們相對應(yīng)的枚舉
package com.yixin.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import com.yixin.myenum.GradeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
//枚舉類型
private GradeEnum grade;
}這樣就可以了!
我們進行測試:
@Test
void test2() {
Student student=new Student();
student.setAge(18);
student.setName("一心同學(xué)");
student.setGrade(GradeEnum.UNIVERSITY);
int result=studentMapper.insert(student);
System.out.println(result);
}控制臺輸出:

數(shù)據(jù)庫:

可以發(fā)現(xiàn),已經(jīng)成功將我們的grade數(shù)據(jù)插進去了。
二、自定義ID生成器
??數(shù)據(jù)庫準備
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(20) DEFAULT NULL COMMENT '名字', `age` int DEFAULT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
??步驟一:編寫主鍵策略
注意:我這里導(dǎo)入mybatis-plus-boot-starter版本3.4.0,因為如果是低版本的話是沒有IdentifierGenerator這個類的。
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>需要重寫方法nextId,這個方法返回的值就是我們的主鍵ID,前面的代碼主要是為了后臺輸出方便查看。
我們的策略是隨機生成100以內(nèi)的數(shù)字充當主鍵(當然,在開發(fā)中不會這樣,這里這是為了演示自定義主鍵的功能)
package com.yixin.config;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class StudentIdGenerator implements IdentifierGenerator {
Random random=new Random();
@Override
public Long nextId(Object entity) {
//可以將當前傳入的class全類名來作為bizKey或者提取參數(shù)來生成bizKey進行分布式Id調(diào)用生成
String bizKey = entity.getClass().getName();
System.out.println("bizKey:" + bizKey);
MetaObject metaObject = SystemMetaObject.forObject(entity);
String name = (String) metaObject.getValue("name");
final long id=random.nextInt(100);
System.out.println("為" + name + "生成主鍵值->:" + id);
return id;
}
}??步驟二:注冊到容器中
package com.yixin.config;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
// 掃描我們的 mapper 文件夾
@MapperScan("com.yixin.mapper")
@EnableTransactionManagement
@Configuration // 配置類
public class MyBatisPlusConfig {
@Bean
public IdentifierGenerator customIdGenerator(){
return new StudentIdGenerator();
}
}??步驟三:修改實體類的主鍵策略
@TableId(type = IdType.ASSIGN_ID)
private Long id;??步驟四:測試
@Test
void test2() {
Student student=new Student();
student.setAge(20);
student.setName("一心同學(xué)");
student.setGrade(GradeEnum.UNIVERSITY);
int result=studentMapper.insert(student);
System.out.println(result);
}后臺輸出:

數(shù)據(jù)庫:

可以發(fā)現(xiàn),我們自定義的主鍵策略就生效了!
小結(jié)
到此這篇關(guān)于MyBatis-Plus枚舉和自定義主鍵ID的文章就介紹到這了,更多相關(guān)MyBatis-Plus枚舉和自定義主鍵ID內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot中如何使用Redisson實現(xiàn)分布式鎖淺析
redisson是redis的java客戶端程序,國內(nèi)外很多公司都有在用,下面這篇文章主要給大家介紹了關(guān)于Springboot中如何使用Redisson實現(xiàn)分布式鎖的相關(guān)資料,需要的朋友可以參考下2021-10-10
通過反射注解批量插入數(shù)據(jù)到DB的實現(xiàn)方法
今天小編就為大家分享一篇關(guān)于通過反射注解批量插入數(shù)據(jù)到DB的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java基于迭代器模式實現(xiàn)的訪問人員列表操作示例
這篇文章主要介紹了Java基于迭代器模式實現(xiàn)的訪問人員列表操作,簡單描述了迭代器模式的概念、原理以及使用迭代器模式實現(xiàn)訪問人員列表的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Java基礎(chǔ)之Comparable與Comparator概述
這篇文章主要介紹了Java基礎(chǔ)之Comparable與Comparator詳解,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Java中如何將符號分隔的文本文件txt轉(zhuǎn)換為excel
這篇文章主要介紹了Java中如何將符號分隔的文本文件txt轉(zhuǎn)換為excel,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
淺析springboot通過面向接口編程對控制反轉(zhuǎn)IOC的理解
這篇文章主要介紹了springboot通過面向接口編程對控制反轉(zhuǎn)IOC的理解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-08-08
springboot登錄攔截器+ThreadLocal實現(xiàn)用戶信息存儲的實例代碼
ThreadLocal 為變量在每個線程中創(chuàng)建了一個副本,這樣每個線程都可以訪問自己內(nèi)部的副本變量,這篇文章主要介紹了springboot登錄攔截器+ThreadLocal實現(xiàn)用戶信息存儲的實例代碼,需要的朋友可以參考下2024-03-03

