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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM