zoukankan      html  css  js  c++  java
  • 属性复制方法,当属性名字不一致时候可以传入匹配的Map

      /**
         * @param src
         * @param dest
         * @param filedMapping the diffrent fieldName  mapping,key is src filename and value is dest filedname
         */
        public <S, T> void copyProperties(S src, T dest, Map<String, String> filedMapping) {
            BeanUtils.copyProperties(src, dest);
            Class sClass = src.getClass();
            Class tClass = dest.getClass();
            Set<Map.Entry<String, String>> mappingSet = filedMapping.entrySet();
            for (Map.Entry<String, String> en : mappingSet) {
                String srcFiledName = en.getKey();
                String destFiledName = en.getValue();
                try {
                    Field srcFiled = sClass.getDeclaredField(srcFiledName);
                    Field destFiled = tClass.getDeclaredField(destFiledName);
                    srcFiled.setAccessible(true);
                    destFiled.setAccessible(true);
                    Object value = srcFiled.get(src);
                    destFiled.set(dest, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    // igonre the not found field error
                }
            }
        }
    

      

  • 相关阅读:
    hutool工具
    lombok
    混入
    postMan
    jsr303常用注解
    网页兼容性
    C/C++ 一点笔记(1)
    VS2010 灵活运用快捷操作功能(新手必看)
    HTML中meta作用
    C/C++ 一点笔记(2)
  • 原文地址:https://www.cnblogs.com/leodaxin/p/10411822.html
Copyright © 2011-2022 走看看