工具类:

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DictHelper<T> {
    private static final Logger logger = LoggerFactory.getLogger(DictHelper.class);
    @Autowired
    private DictUtil dictUtil;

    public DictHelper() {
    }

    public List<T> loadListDict(List<T> list, String bean, String getField, String setField) {
        Map<String, Object> dictMap = this.dictUtil.getDictMap(bean);
        Iterator var6 = list.iterator();

        while(var6.hasNext()) {
            T t = var6.next();
            this.loadObjectDict(t, dictMap, getField, setField);
        }

        return list;
    }

    public T loadObjectDict(T t, Map<String, Object> dicts, String getField, String setField) {
        String getMethodStr = "get" + StringUtils.toUpperCaseFirstOne(getField);
        String setMethodStr = "set" + StringUtils.toUpperCaseFirstOne(setField);

        try {
            Method getMethod = t.getClass().getMethod(getMethodStr);
            Method setMethod = t.getClass().getMethod(setMethodStr, String.class);
            String dictId = getMethod.invoke(t) + "";
            if (dictId.equals("-1")) {
                setMethod.invoke(t, "全部");
            } else {
                Object o = dicts.get(dictId);
                if (o != null) {
                    Dict dict = new Dictionary();
                    if (o instanceof Map) {
                        Map map = (Map)o;
                        ((Dict)dict).setCode(map.get("code"));
                        ((Dict)dict).setName(map.get("name").toString());
                    } else {
                        dict = (Dict)o;
                    }

                    setMethod.invoke(t, ((Dict)dict).getName());
                }
            }
        } catch (Exception var13) {
            String errorMsg = "为对象设置字典错误\n";
            errorMsg = errorMsg + "getField:" + getField + "\n";
            errorMsg = errorMsg + "setField:" + setField + "\n";
            errorMsg = errorMsg + "设置对象的类:" + t.getClass();
            LoggerUtils.warn(errorMsg, var13);
        }

        return t;
    }
}

方法调用:

List<DriverLine> driverLineList = new ArrayList<>();
DriverLine driverLine = new DriverLine();
driverLine.setLineId(2);
driverLineList.add(driverLine);
Object o = dictHelper.loadListDict(driverLineList, "basic.line", "lineId","lineName");//basic.line
System.err.println(o.toString());