当前位置:网站首页>枚举根据参数获取值
枚举根据参数获取值
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;
}
测试结果:

边栏推荐
- LeetCode_格雷编码_中等_89.格雷编码
- Wonderful coding [hexadecimal conversion]
- Test Li hi
- 方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
- [infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
- [translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
- 面试突击63:MySQL 中如何去重?
- Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
- Example of shutter text component
- 1805. Number of different integers in the string
猜你喜欢

Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand

【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)

Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference

Learn to explore - use pseudo elements to clear the high collapse caused by floating elements

Standardized QCI characteristics

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!

Information System Project Manager - Chapter VIII project quality management

How to access localhost:8000 by mobile phone

Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
Application of clock wheel in RPC
随机推荐
CF960G - Bandit Blues(第一类斯特林数+OGF)
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
Cesium 点击绘制圆形(动态绘制圆形)
PHP与EXCEL PHPExcel
POJ3617 Best Cow Line 馋
How to access localhost:8000 by mobile phone
Phoenix Architecture 2 - accessing remote services
Dom 操作
Interpretation of Dagan paper
Logstash expressway entrance
精彩编码 【进制转换】
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
beegfs高可用模式探讨
力扣101题:对称二叉树
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
golang的超时处理使用技巧
Tensorflow2.0 self defined training method to solve function coefficients
js实现力扣71题简化路径