List<Class>通用批量转化字典



fieldToDictMap格式:
// 定义字段和字典类型的映射关系
Map<String, String> fieldToDictMap = new HashMap<>();
fieldToDictMap.put("one", "字典标识符");
fieldToDictMap.put("two", "字典标识符");
systemClient是用来获取字典数据的,根据自己实际情况来
type,是因为我写的通用的批量转化,但是个别字段是需要额外做处理的

public
static void getWordGetSet(List<?> wordList, Map<String, String> fieldToDictMap,SystemClient systemClient,String type) { Map<String, Map<String, String>> allDictionaries = new HashMap<>(); for (Map.Entry<String, String> entry : fieldToDictMap.entrySet()) { String fieldName = entry.getValue(); SysDictItem dictItemQuery = new SysDictItem(); dictItemQuery.setNbbsf(fieldName); Result<List<SysDictItem>> result = systemClient.getDictItem(dictItemQuery); // 防止空结果 if (result != null && result.getData() != null) { Map<String, String> dictMap = result.getData().stream() .filter(Objects::nonNull) .collect(Collectors.toMap(SysDictItem::getXxdm, SysDictItem::getXxdmmc)); allDictionaries.put(fieldName, dictMap); } } for (Object word : wordList) { for (Map.Entry<String, String> entry : fieldToDictMap.entrySet()) { String fieldName = entry.getKey(); String dictType = entry.getValue(); Map<String, String> dictMap = allDictionaries.get(dictType); // 动态调用 getter 和 setter 方法 if (dictMap != null) { try { // 获取字段值 Method getter = word.getClass().getMethod("get" + capitalize(fieldName)); Object fieldValue = getter.invoke(word); if (fieldValue != null) { // 设置转换后的值 Method setter = word.getClass().getMethod("set" + capitalize(fieldName), String.class); setter.invoke(word, dictMap.getOrDefault(fieldValue.toString(), "未知")); } } catch (Exception e) { // 捕获异常并记录日志 log.error("getter或setter方法调用失败,请检查字段名是否正确,例如:get" + capitalize(fieldName)); e.printStackTrace(); } } } switch (type) { case "zdrygkjl": getWordGetSetZdryGkjl(word); break; case "zdry": getWordGetSetZdry(word); break; } } }

public static <T> void getWordGetSetZdryGkjl(T word){
        // 通过反射机制来判断是否存在类似jzdpcsmc的属性,并进行处理(更通用但性能有一定损耗的方式)
        try {
            // 获取名为jzdpcsmc的get方法
            java.lang.reflect.Method getMethod = word.getClass().getMethod("getGxdwmc");
            if (getMethod.getReturnType() == String.class) {
                String value = (String) getMethod.invoke(word);
                if (value!= null) {
                    // 获取对应的set方法
                    java.lang.reflect.Method setMethod = word.getClass().getMethod("setGxdwmc", String.class);
                    // 调用CacheUtils.getGajgmc处理值并设置回去
                    setMethod.invoke(word, CacheUtils.getGajgmc(value));
                }
            }
            java.lang.reflect.Method getMethodZkzt = word.getClass().getMethod("getZkztmc");
            if (getMethodZkzt.getReturnType() == String.class) {
                String value = (String) getMethodZkzt.invoke(word);
                if (value!= null) {
                    // 获取对应的set方法
                    java.lang.reflect.Method setMethod = word.getClass().getMethod("setZkztmc", String.class);
                    // 调用CacheUtils.getGajgmc处理值并设置回去
                    if("2".equals(value)){
                        setMethod.invoke(word, "在");
                    }else{
                        setMethod.invoke(word, "不在");
                    }
                }
            }
        } catch (Exception e) {
            // 处理反射调用可能出现的异常,比如方法不存在等情况,可以根据实际需求更精细地处理异常
            e.printStackTrace();
        }
    }


public static <T> void getWordGetSetZdry(T word){
        // 通过反射机制来判断是否存在类似jzdpcsmc的属性,并进行处理(更通用但性能有一定损耗的方式)
        try {
            // 获取名为jzdpcsmc的get方法
            java.lang.reflect.Method getMethod = word.getClass().getMethod("getJzdpcsmc");
            if (getMethod.getReturnType() == String.class) {
                String value = (String) getMethod.invoke(word);
                if (value!= null) {
                    // 获取对应的set方法
                    java.lang.reflect.Method setMethod = word.getClass().getMethod("setJzdpcsmc", String.class);
                    // 调用CacheUtils.getGajgmc处理值并设置回去
                    setMethod.invoke(word, CacheUtils.getGajgmc(value));
                }
            }

            java.lang.reflect.Method getMethodGxdw = word.getClass().getMethod("getGxdwmc");
            if (getMethodGxdw.getReturnType() == String.class) {
                String value = (String) getMethodGxdw.invoke(word);
                if (value!= null) {
                    // 获取对应的set方法
                    java.lang.reflect.Method setMethod = word.getClass().getMethod("setGxdwmc", String.class);
                    // 调用CacheUtils.getGajgmc处理值并设置回去
                    setMethod.invoke(word, CacheUtils.getGajgmc(value));
                }
            }
        } catch (Exception e) {
            // 处理反射调用可能出现的异常,比如方法不存在等情况,可以根据实际需求更精细地处理异常
            e.printStackTrace();
        }
    }

 

/**
     * 首字母大写辅助方法
     */

    private static String capitalize(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

 



 

posted @ 2024-12-11 10:54  阿迪di  阅读(23)  评论(0)    收藏  举报
Title