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);
        }
    }
  • 相关阅读:
    springboot(十二)-分布式锁(redis)
    springboot(十一)-为什么要用springboot
    HashMap和HashTable区别
    ArrayList和LinkedList区别
    springcloud(三)-Eureka
    springboot(十)-监控应用
    springboot(九)-log配置
    springcloud(二)-最简单的实战
    rest版的webservice
    sqlite与android交互 (封装)
  • 原文地址:https://www.cnblogs.com/youcong/p/14063742.html
Copyright © 2011-2022 走看看