当前位置:网站首页>枚举根据参数获取值
枚举根据参数获取值
2022-07-06 12:09:00 【Mr_ZhangAdd】
问题背景
业务中经常会遇到固定业务类型,数据库中常常以数值依次标识其含义 0-女 1-男
主体思路:
定义枚举》获取枚举中得值组成map生成bean》通过上下文获取bean获取对应的值
枚举
import lombok.Getter;
import lombok.Setter;
/**
* @author Xuewei.Zhang
* @date 2022/7/5
*/
public enum FlowEnum {
ORDER(0, "订单"),
PROCUREMENT(1, "采购"),
PRODUCTION(2, "生产"),
REIMBURSE(3, "报销"),
VACATE(4, "请假"),
;
@Getter
@Setter
private Integer code;
@Getter
@Setter
private String msg;
FlowEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}组装bean
import com.zxw.common.core.base.enums.FlowEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Xuewei.Zhang
* @date 2022/7/5
*/
@Configuration
public class FlowBean {
@Bean("FlowEnumBean")
public Map<String, Integer> FlowEnumBean() {
Map<String, Integer> map = new ConcurrentHashMap<>();
for (FlowEnum flowEnum : FlowEnum.values()) {
map.put(flowEnum.getMsg(), flowEnum.getCode());
}
return map;
}
}
获取对应的值
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.Map;
/**
* @author Xuewei.Zhang
* @date 2022/7/5
*/
@Configuration
public class BeanUtils {
private static ApplicationContext applicationContext;
@Resource
public void setApplicationContext(ApplicationContext applicationContext) {
BeanUtils.applicationContext = applicationContext;
}
public static Integer getCodeByServiceName(String serviceName) {
Map<String, Integer> map = (Map<String, Integer>) applicationContext.getBean("FlowEnumBean");
return map.get(serviceName);
}
}使用
@Override
@InitInsertInfo
public FlowTypeVO add(FlowTypeVO flowTypeVO) {
//使用
flowTypeVO.setServiceType(BeanUtils.getCodeByServiceName(flowTypeVO.getServiceTypeName()));
flowTypeMapper.insert(flowTypeVO);
return flowTypeVO;
}
测试结果:

边栏推荐
猜你喜欢

Tencent T3 teaches you hand in hand. It's really delicious
![[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer](/img/8c/afb90128e7a523bbee4c6c4166363f.png)
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer

PowerPivot——DAX(初识)

Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe

MySQL information schema learning (II) -- InnoDB table

社招面试心得,2022最新Android高频精选面试题分享
腾讯T4架构师,android面试基础

Standardized QCI characteristics

(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of

VMware virtual machine cannot open the kernel device "\.\global\vmx86"
随机推荐
广州首个数据安全峰会将在白云区开幕
学习打卡web
Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
22-07-05 七牛云存储图片、用户头像上传
Phoenix Architecture 2 - accessing remote services
AsyncHandler
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
利用 clip-path 绘制不规则的图形
腾讯T3大牛手把手教你,大厂内部资料
Information System Project Manager - Chapter VIII project quality management
颜色(color)转换为三刺激值(r/g/b)(干股)
深入浅出,面试突击版
js实现力扣71题简化路径
Dom 操作
Synchronization of data create trigger synchronization table for each site
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Period compression filter
Test Li hi
[translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022