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

詳解mybatis-plus使用@EnumValue注解的方式對枚舉類型的處理

 更新時(shí)間:2020年12月18日 16:44:07   作者:Zephyr_Syn  
這篇文章主要介紹了詳解mybatis-plus使用@EnumValue注解的方式對枚舉類型的處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言:

在開發(fā)中,我們經(jīng)常會(huì)用到諸如:性別(男/女)、審核狀態(tài)(未審核/審核中/已審核)之類的數(shù)據(jù),通常會(huì)在數(shù)據(jù)庫中使用一個(gè)數(shù)字類型的字段來標(biāo)識,比如:性別,用1來表示男,2來表示女,而在代碼中一般會(huì)定義成enum類型或靜態(tài)常量來避免在業(yè)務(wù)代碼中出現(xiàn)“0/1”這種魔法值,但是在數(shù)據(jù)庫存儲(chǔ)及前后端交互的時(shí)候,就需要進(jìn)行轉(zhuǎn)化;無論是在SQL、前端還是后臺轉(zhuǎn)化,都需要寫相應(yīng)的代碼,無形中增加了開發(fā)工作量;mybatis-plus實(shí)現(xiàn)了對該問題的處理,能夠讓我們在查詢數(shù)據(jù)庫時(shí),直接能夠返回字段標(biāo)識的意思。配置如下:

第一步:

創(chuàng)建枚舉類,在需要存儲(chǔ)數(shù)據(jù)庫的屬性上添加@EnumValue注解,在需要前端展示的屬性上添加@JsonValue注解;

package com.demo.mybatisplus.constant;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;

public enum SexEnum {

 MAN(1, "男"),
 WOMAN(2, "女");

 @EnumValue
 private Integer key;

 @JsonValue
 private String display;

 SexEnum(Integer key, String display) {
  this.key = key;
  this.display = display;
 }

 public Integer getKey() {
  return key;
 }

 public String getDisplay() {
  return display;
 }
}

第二步:

application.properties文件里添加配置,定義掃描枚舉類的包路徑;

#配置枚舉 支持通配符 * 或者 ; 分割
mybatis-plus.type-enums-package=com.demo.mybatisplus.constant
#mybatis-plus.configuration.default-enum-type-handler=org.apache.ibatis.type.EnumOrdinalTypeHandler

第三步:

pojo中的sex屬性設(shè)置為枚舉SexEnum;

 @ApiModelProperty(value = "性別")
 @TableField("sex")
 private SexEnum sex;

測試:

 @Test
 public void insert() {
  UserInfo userInfo = new UserInfo();
  userInfo.setAge(22);
  userInfo.setName("李四");
  userInfo.setSex(SexEnum.WOMAN);
  userInfoMapper.insert(userInfo);
  System.out.println(userInfo);
 }

數(shù)據(jù)庫保存的值:

ID NAME AGE SEX
1 張三 11 1
2 李四 22 2
3 王五 33 1

前端顯示的值:

[
 {"id":"1","name":"張三","age":11,"sex":"男"},
 {"id":"2","name":"李四","age":22,"sex":"女"},
 {"id":"3","name":"王五","age":33,"sex":"男"}
]

注意事項(xiàng):

@EnumValue標(biāo)記的枚舉類屬性的類型要和數(shù)據(jù)庫字段的類型對應(yīng),否則在查詢數(shù)據(jù)的時(shí)候無法轉(zhuǎn)化為枚舉類型,并顯示為null;

如果查詢的時(shí)候,數(shù)據(jù)庫字段的值匹配不到枚舉,程序運(yùn)行時(shí)并不會(huì)報(bào)錯(cuò),而是顯示為null;

在保存的時(shí)候,前端需要傳遞@JsonValue標(biāo)記的枚舉類屬性的值,即"男/女";因?yàn)镋num的屬性ordinal(int),在測試過程中,傳枚舉值在枚舉類中的定義順序(或者稱為索引,順序從0開始),也可以轉(zhuǎn)換為相應(yīng)的枚舉值,比如:上面定義的SexEnum枚舉,前端傳0或者"0",會(huì)轉(zhuǎn)換成MAN,傳1或者"1"會(huì)轉(zhuǎn)換成WOMAN;傳其他值會(huì)報(bào)異常:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type com.demo.mybatisplus.constant.SexEnum from String "3": not one of the values accepted for Enum class: [女, 男]或com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of typecom.demo.mybatisplus.constant.SexEnum from number 3: index value outside legal index range [0..2];

到此這篇關(guān)于詳解mybatis-plus使用@EnumValue注解的方式對枚舉類型的處理的文章就介紹到這了,更多相關(guān)mybatis-plus EnumValue枚舉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論