当前位置:网站首页>枚举根据参数获取值
枚举根据参数获取值
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;
}
测试结果:
边栏推荐
- Finally, there is no need to change a line of code! Shardingsphere native driver comes out
- [infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
- 技术分享 | 抓包分析 TCP 协议
- POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
- Recursive implementation of department tree
- From spark csc. csr_ Matrix generate adjacency matrix
- 新一代垃圾回收器—ZGC
- How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
- Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
- Hudi vs Delta vs Iceberg
猜你喜欢
社招面试心得,2022最新Android高频精选面试题分享
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
力扣101题:对称二叉树
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
理解 YOLOV1 第二篇 预测阶段 非极大值抑制(NMS)
[玩转Linux] [Docker] MySQL安装和配置
腾讯Android面试必问,10年Android开发经验
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
随机推荐
Hudi vs Delta vs Iceberg
Yyds dry goods inventory leetcode question set 751 - 760
Cesium 两点之间的直线距离
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
深入浅出,面试突击版
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
Leetcode 30. Concatenate substrings of all words
范式的数据库具体解释
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
Interview assault 63: how to remove duplication in MySQL?
PHP and excel phpexcel
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
某东短信登录复活 安装部署教程
PHP与EXCEL PHPExcel
小微企业难做账?智能代账小工具快用起来
AddressSanitizer 技术初体验
腾讯T3手把手教你,真的太香了
Example of applying fonts to flutter
从sparse.csc.csr_matrix生成邻接矩阵