当前位置:网站首页>枚举根据参数获取值
枚举根据参数获取值
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;
}
测试结果:
边栏推荐
- [infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
- golang的超时处理使用技巧
- Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
- Hudi vs Delta vs Iceberg
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
- 系统与应用监控的思路和方法
- In simple terms, interview surprise Edition
- Microservice architecture debate between radical technologists vs Project conservatives
- 腾讯云数据库公有云市场稳居TOP 2!
猜你喜欢
Leetcode 30. 串联所有单词的子串
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
It's enough to read this article to analyze the principle in depth
OceanBase社区版之OBD方式部署方式单机安装
[play with Linux] [docker] MySQL installation and configuration
蓝桥杯 微生物增殖 C语言
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
Configuration and simple usage of the EXE backdoor generation tool quasar
学习打卡web
随机推荐
Period compression filter
Color is converted to tristimulus value (r/g/b) (dry stock)
BUUCTF---Reverse---easyre
从sparse.csc.csr_matrix生成邻接矩阵
Tensorflow2.0 self defined training method to solve function coefficients
社招面试心得,2022最新Android高频精选面试题分享
Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
Hudi vs Delta vs Iceberg
Poj1149 pigs [maximum flow]
Hudi vs Delta vs Iceberg
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
JVM_常见【面试题】
Transformer model (pytorch code explanation)
Analysis of rainwater connection
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
Unbalance balance (dynamic programming, DP)
深入浅出,面试突击版
Synchronization of data create trigger synchronization table for each site
Standardized QCI characteristics