zoukankan      html  css  js  c++  java
  • Java 属性映射(DozerBeanMapper)

    package com.kps.common.utils;
    
    import org.dozer.DozerBeanMapper;
    import org.dozer.Mapper;
    import org.dozer.loader.api.BeanMappingBuilder;
    import org.dozer.loader.api.TypeMappingOption;
    import org.dozer.loader.api.TypeMappingOptions;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    public class DozerMapperUtil{
        static  Mapper mapper = new DozerBeanMapper();
        /**
         * 将list转换为vo的list
         * @param source
         * @param destType
         * @return
         */
        public static <T, U> List<U> mapList(final List<T> source, final Class<U> destType) {
            final List<U> dest = new ArrayList<U>();
            for (T element : source) {
                dest.add(mapper.map(element, destType));
            }
            return dest;
        }
    
    //将Collection<E>
        public static <T, U> Collection<U> mapCollection(final Collection<T> source, final Class<U> destType) {
            final Collection<U> dest = new ArrayList<>();
            for (T element : source) {
                dest.add(mapper.map(element, destType));
            }
            return dest;
        }
        /**
         * 转换单个vo对象,自动生成目标实例化对象
         * @param source
         * @param destType
         * @return
         */
        public static <U> U map(final Object source,final Class<U> destType) {
            return mapper.map(source, destType);
        }
        
        /**
         * 将source的所有属性拷贝至target,source里没有的字段,target里不覆盖
         * @param source
         * @param target
         * @return
         */
        public static <U> void map(final Object source, final U target) {
            DozerBeanMapper mapper = new DozerBeanMapper();
            mapper.addMapping(new BeanMappingBuilder() {
                @Override
                protected void configure() {
                    mapping(source.getClass(), target.getClass(),
                            new TypeMappingOption[] {TypeMappingOptions.mapNull(false) });
                }
    
            });
            mapper.map(source, target);
        }
    
    
        /**
         * @Title: map2
         * @Description: (因为在使用map方法时遇到有字段没有复制成功,所以重写了新方法,经试用没问题。)该方法是用于相同对象不同属性值的合并,如果两个相同对象中同一属性都有值,
         *               那么sourceBean中的值会覆盖tagetBean重点的值
         * @param sourceBean     *            被提取的对象bean
         * @param targetBean     *            用于合并的对象bean
         * @return targetBean 合并后的对象
         * @return: Object
         */
        @SuppressWarnings("unused")
        public static Object map2(Object sourceBean, Object targetBean) {
            Class sourceBeanClass = sourceBean.getClass();
            Class targetBeanClass = targetBean.getClass();
    
            Field[] sourceFields = sourceBeanClass.getDeclaredFields();
            Field[] targetFields = sourceBeanClass.getDeclaredFields();
            for (int i = 0; i < sourceFields.length; i++) {
                Field sourceField = sourceFields[i];
                Field targetField = targetFields[i];
                sourceField.setAccessible(true);
                targetField.setAccessible(true);
                try {
                    if (!(sourceField.get(sourceBean) == null)) {
                        targetField.set(targetBean, sourceField.get(sourceBean));
                    }
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return  targetBean;
        }
        
    }
  • 相关阅读:
    C#实现根据域名查询ip实例
    Ajax: 一个建立Web应用的新途径(转)
    CRC循环校验的具体算法(转)
    生成静态文件的新闻系统核心代码(.net C#)
    一个ajax的例子
    使用 JavaScript 实现 XMLHttpRequest,在IE,FireFox 上测试通过
    微软SQL Server 2005的30项顶尖特性(转)
    利用XMLHTTP无刷新自动实时更新数据(转)
    五子棋的核心算法(转)
    编写安全的SQL Server扩展存储过程(转)
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779687.html
Copyright © 2011-2022 走看看