• beanUtils 对象之前的 相互赋值转换


    public final class BeanUtils {
    
        /**
         * 将srcObject的字段值赋值到destObject对象中去
         *
         * @param srcObject
         * @param destObject
         * @param <S>
         * @param <T>
         * @return
         */
        public static <S, T> T initDestObject(S srcObject, T destObject) {
            Map<String,Object> srcMap = new HashMap<>();
            Class srcClass = srcObject.getClass();
            Field[] declaredFields = srcClass.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                try {
                    Field field = declaredFields[i];
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    Object value = field.get(srcObject);
                    srcMap.put(fieldName, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            try {
                destObject = initProperty(destObject, srcMap);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            System.out.println(srcMap);
            return destObject;
        }
    
        private static <T> T initProperty(T obj, Map<String,Object> map) throws IllegalAccessException {
            Class<?> clazz = obj.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                declaredFields[i].setAccessible(true);
                String fieldName = declaredFields[i].getName();
                Set<String> keySet = map.keySet();
                for (String s : keySet) {
                    if (fieldName.equals(s)) {
                        declaredFields[i].set(obj, map.get(s));
                    }
                }
            }
            return obj;
        }
    }
  • 相关阅读:
    oracle(八)块清除
    oracle(七)索引
    oracle(六) physical read and logical read
    oracle动态视图(一)stat
    oracle(五)tkprof 使用 transient kernal profile 侧面 轮廓
    dbms_stats.gather_table_stats详解
    oracle(四) 常用语句
    oracle(三) SQL语句
    [Swift]LeetCode269. 外星人词典 $ Alien Dictionary
    [Mac]如何让两个窗口各占半个屏幕
  • 原文地址:https://www.cnblogs.com/woshuyuqiang/p/9437135.html
Copyright © 2020-2023  润新知