数据字典核心代码实现:
@Component public classDictMap { @Autowired privateSysDictDataMapper dictDataMapper; private static HashMap<String, String> hashMap = new HashMap<>(); public staticDictMap dictMap; /** * 从数据库中取值放入到HashMap中(存储字典) */@PostConstruct public voidqueryDic() { 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 staticString getFieldDetail(String fieldName, String fieldValue) { StringBuilder sb = newStringBuilder(); StringBuilder keySb = sb.append(fieldName).append("_").append(fieldValue); String key =keySb.toString(); String value = hashMap.get(key); returnvalue; } }
代码引用:
@Data public classUserVo implements Serializable { privateLong userId; privateString sex; publicString getSex() { return sex = DictMap.getFieldDetail("sex_type", sex); } }