zoukankan      html  css  js  c++  java
  • Java之数据字典实现

    数据字典核心代码实现:

    @Component
    public class DictMap {
        @Autowired
        private SysDictDataMapper dictDataMapper;
    
        private static HashMap<String, String> hashMap = new HashMap<>();
    
        public static DictMap dictMap;
    
        /**
         * 从数据库中取值放入到HashMap中(存储字典)
         */
        @PostConstruct
        public void queryDic() {
    
            dictMap = this;
            dictMap.dictDataMapper = this.dictDataMapper;
    
            System.out.println("初始化");
    
            List<SysDictData> dics = dictMap.dictDataMapper.selectDictDataAll();
    
            for (int i = 0; i < dics.size(); i++) {
                SysDictData dic = dics.get(i);
    
                String fieldName = dic.getDictType();
                String fieldValue = dic.getDictValue();
                String key = fieldName + "_" + fieldValue;
                String value = dic.getDictLabel();
                System.out.println(key + "=" + value);
                hashMap.put(key, value);
            }
        }
    
        /**
         * 获取字典
         *
         * @param fieldName
         * @param fieldValue
         * @return
         */
        public static String getFieldDetail(String fieldName, String fieldValue) {
            StringBuilder sb = new StringBuilder();
            StringBuilder keySb = sb.append(fieldName).append("_").append(fieldValue);
            String key = keySb.toString();
            String value = hashMap.get(key);
            return value;
        }
    }

    代码引用:

    @Data
    public class UserVo implements Serializable {
    
    
        private Long userId;
    
        private String sex;
    
        public String getSex() {
            return sex = DictMap.getFieldDetail("sex_type", sex);
        }
    }
  • 相关阅读:
    周鸿祎笔录
    set multiset 集合实现众数的统计
    栈实现 汉诺塔 操作是否符合规范
    优先队列----解决排序问题
    字符串的应用(续一)
    优先队列 的实例(未完----待续)
    字符串的应用
    栈实现括号
    Jave垃圾回收算法
    Java 按代回收垃圾以及回收算法
  • 原文地址:https://www.cnblogs.com/youcong/p/14063742.html
Copyright © 2011-2022 走看看