zoukankan      html  css  js  c++  java
  • spring 中 PO与DTO相互转换的工具类

     1 public class BeanMapper {
     2 
     3     /**
     4      * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
     5      */
     6     private static DozerBeanMapper dozer = new DozerBeanMapper();
     7 
     8     /**
     9      * 基于Dozer转换对象的类型.
    10      */
    11     public static <T> T map(Object source, Class<T> destinationClass) {
    12         return dozer.map(source, destinationClass);
    13     }
    14 
    15     /**
    16      * 基于Dozer转换Collection中对象的类型.
    17      */
    18     public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
    19         List<T> destinationList = Lists.newArrayList();
    20         for (Object sourceObject : sourceList) {
    21             T destinationObject = dozer.map(sourceObject, destinationClass);
    22             destinationList.add(destinationObject);
    23         }
    24         return destinationList;
    25     }
    26 
    27     /**
    28      * 基于Dozer将对象A的值拷贝到对象B中.
    29      */
    30     public static void copy(Object source, Object destinationObject) {
    31         dozer.map(source, destinationObject);
    32     }
    33 }
    //清晰版
    import java.util.Collection;
    import java.util.List;
    import org.dozer.DozerBeanMapper;
    import com.google.common.collect.Lists;
    
    public class BeanMapper {
    
        
        private static DozerBeanMapper dozer = new DozerBeanMapper();
    
        public static <T> T map(Object source, Class<T> destinationClass) {
            return dozer.map(source, destinationClass);
        }
    
        public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
            List<T> destinationList = Lists.newArrayList();
            for (Object sourceObject : sourceList) {
                T destinationObject = dozer.map(sourceObject, destinationClass);
                destinationList.add(destinationObject);
            }
            return destinationList;
        }
        
        public static void copy(Object source, Object destinationObject) {
            dozer.map(source, destinationObject);
        }
    }
  • 相关阅读:
    shiro中 UnknownAccountException
    shiro Filter--拦截器
    java构造器执行顺序一个有趣的简单实例
    Java Serializable接口(序列化)理解及自定义序列化
    js中绑定事件处理函数,使用event以及传递额外数据
    js中的this
    jQuery + ashx 实现图片按比例预览、异步上传及显示
    asp.net中的参数传递:Context.Handler 的用法
    javascript 对象详解
    ashx 文件的运用
  • 原文地址:https://www.cnblogs.com/baizhuang/p/9580800.html
Copyright © 2011-2022 走看看