Mybatis-plus中的@EnumValue注解使用詳解
前言
在實(shí)際開(kāi)發(fā)中,對(duì)于一些狀態(tài)類(lèi)的字段,我們通常使用的是枚舉,而保存到數(shù)據(jù)庫(kù)時(shí),我們是用的枚舉的某一個(gè)屬性進(jìn)行保存的,這里就會(huì)有一個(gè)問(wèn)題,在PO類(lèi)中,如果我們直接使用枚舉類(lèi)型去映射數(shù)據(jù)庫(kù)的對(duì)應(yīng)字段保存時(shí),往往就會(huì)因?yàn)轭?lèi)型不匹配導(dǎo)致映射失敗,如果要解決這個(gè)問(wèn)題,辦法有很多種,Mybatis-plus提供了一種解決辦法,就是使用@EnumValue注解,這里我們就使用這種方式。
Maven引入的依賴(lài)
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Demo
PO類(lèi)
@Data @TableName(value = "urge_reduce_rule") public class ReduceRule { @TableId(value = "id", type = IdType.AUTO) private Long id; @TableField(value = "charge_category") private ChargeCategoryEnum chargeCategoryEnum; @TableField(value = "name") private String name; }
枚舉類(lèi)(@EnumValue注解就用在這里)
@Getter public enum ChargeCategoryEnum { CHARGE("CHARGE",1,"基本費(fèi)"), PENALTY("PENALTY",2,"違約金"); private String code; @EnumValue //在需要保存到數(shù)據(jù)庫(kù)的值上面加上注解 private Integer value; private String text; public String getCode() { return code; } ChargeCategoryEnum(String code, Integer value, String text) { this.code = code; this.value = value; this.text = text; } }
mapper類(lèi)
@Mapper public interface ReduceRuleMapper extends BaseMapper<ReduceRule> { }
配置文件
#配置枚舉 支持通配符 * 或者 ; 分割。指定枚舉類(lèi)所在的包 mybatis-plus: type-enums-package: com.demo.mybatisplus.enum configuration: default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler #handler配置可以省略不寫(xiě),默認(rèn)配置就是上面這個(gè)Handler
測(cè)試代碼
@SpringBootTest(classes = DemoApplication.class) class DemoApplicationTests { @Resource private ReduceRuleMapper reduceRuleMapper; @Test void test1(){ ReduceRule reduceRule = new ReduceRule(); reduceRule.setName("名字"); reduceRule.setChargeCategoryEnum(ChargeCategoryEnum.PENALTY); reduceRuleMapper.insert(reduceRule); } @Test void test2(){ ReduceRule reduceRule = reduceRuleMapper.selectById(32L); System.out.println(reduceRule); } }
拓展
如果返回給前端不希望直接將枚舉返回的話(huà),需要在枚舉類(lèi)上加上 @JsonValue 注解
@Getter public enum ChargeCategoryEnum { CHARGE("CHARGE",1,"基本費(fèi)"), PENALTY("PENALTY",2,"違約金"); private String code; @EnumValue //在需要保存到數(shù)據(jù)庫(kù)的值上面加上注解 private Integer value; @JsonValue //需要在前端展示哪個(gè)值就在哪個(gè)屬性上加上該注解 private String text; public String getCode() { return code; } ChargeCategoryEnum(String code, Integer value, String text) { this.code = code; this.value = value; this.text = text; } }
到此這篇關(guān)于Mybatis-plus中的@EnumValue注解使用詳解的文章就介紹到這了,更多相關(guān)@EnumValue注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用@Autowired注解警告Field injection is not recommended的解決
這篇文章主要介紹了使用@Autowired注解警告Field injection is not recommended的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Spring中@RequestMapping、@RestController和Postman
本文介紹了Spring框架中常用的@RequestMapping和@RestController注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10springcloud + mybatis + seate集成示例
本文主要介紹了springcloud + mybatis + seate集成示例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解
這篇文章主要介紹了Java弱鍵集合WeakHashMap及ConcurrentCache原理詳解,基于哈希表的Map接口實(shí)現(xiàn),支持null鍵和值,但是WeakHashMap具有弱鍵,可用來(lái)實(shí)現(xiàn)緩存存儲(chǔ),在進(jìn)行GC的時(shí)候會(huì)自動(dòng)回收鍵值對(duì),需要的朋友可以參考下2023-09-09JDBC如何訪問(wèn)MySQL數(shù)據(jù)庫(kù),并增刪查改
這篇文章主要介紹了JDBC如何訪問(wèn)MySQL數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)java與MySQL,感興趣的朋友可以了解下2020-08-08在SpringBoot中使用jwt實(shí)現(xiàn)token身份認(rèn)證的實(shí)例代碼
你還不會(huì)在SpringBoot中使用jwt實(shí)現(xiàn)token身份認(rèn)證嗎,本文小編就給大家詳細(xì)的介紹一下在SpringBoot中使用jwt實(shí)現(xiàn)token身份認(rèn)證的實(shí)例代碼,感興趣的同學(xué)可以自己動(dòng)手試一試2023-09-09