BeanCopier和BeanUtils实现对象复制,处理date类型

没有废话

先是BeanUtils的实现类型:

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import com.huateng.common.tools.ConvertTools;
import com.huateng.common.tools.subTools.form.ProductFormVo;
import com.huateng.common.tools.subTools.form.ProductFormVo1;

/**
 * @author Jira
 *
 */
public class LocalBeanUtils  extends BeanUtils {
    
    static {   
        ConvertUtils.register(new DateConvert(), java.util.Date.class);   
        ConvertUtils.register(new DateConvert(), java.sql.Date.class);   
        ConvertUtils.register(new DateConvert(), java.lang.String.class);
//        ConvertUtils.register(new DateConvert(), java.lang.Long.class);   //可以添加新的转换工具
    }   
 
   public static void copyProperties(Object dest, Object orig) {   
       try {   
            BeanUtils.copyProperties(dest, orig);   
        } catch (IllegalAccessException ex) {   
            ex.printStackTrace();   
        } catch (InvocationTargetException ex) {   
            ex.printStackTrace();   
        }   
    }
    
    public static void main(String...strings) throws Exception{
        ProductFormVo pf;
        Long l=System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            ProductFormVo1 p = new ProductFormVo1();
            p.setArticleId("a");
            p.setCreateStamp(new Date());
            p.setThruDate("2000-11-11 12:00:00");
            pf = new ProductFormVo();
            LocalBeanUtils.copyProperties(p, pf);
        }
        System.out.println(System.currentTimeMillis()-l);
    }

}

  它用到的DateConvert

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class DateConvert implements Converter {

    public Object convert(Class arg0, Object arg1) {
        if (null == arg1)
            return null;
        /** 输入String ,输出Date */
        if (arg1 instanceof String && arg0 != java.lang.String.class) {
            String p = (String) arg1;
            if (p == null || p.trim().length() == 0) {
                return null;
            }
            try {

                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.parse(p.trim());
            } catch (Exception e) {
                try {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    return df.parse(p.trim());
                } catch (ParseException ex) {
                    return arg1;
                }
            }
        }/** 输入String ,输出String */
        else if (arg1 instanceof String && arg0 == java.lang.String.class) {
            return arg1;
        }/** 输入Date ,输出String */
        else if (arg1 instanceof java.util.Date) {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.format((java.util.Date) arg1);
            } catch (Exception e) {
                return null;
            }
        }/** 输入Date ,输出String */
        else if (arg1 instanceof java.sql.Date) {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.format((java.sql.Date) arg1);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

}

 

下面是BeanCopier:

 

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.cglib.beans.BeanCopier;

import com.huateng.common.tools.subTools.DateConverterBeanCopier;
import com.huateng.common.tools.subTools.form.ProductFormVo;
import com.huateng.common.tools.subTools.form.ProductFormVo1;

/**
 * @author Jira
 *vo对象间的赋值操作
 *DateConverterBeanCopier 可以添加新的对象转换功能
 */
public class BeanCopierUtils {
    public static Map<String,BeanCopier> beanCopierMap = new HashMap<String,BeanCopier>();
    
    public static void copyProperties(Object source, Object target){
        String beanKey =  generateKey(source.getClass(), target.getClass());
        BeanCopier copier =  null;
        if(!beanCopierMap.containsKey(beanKey)){
             copier = BeanCopier.create(source.getClass(), target.getClass(), true);
             beanCopierMap.put(beanKey, copier);
        }else{
             copier = beanCopierMap.get(beanKey);
        }
        copier.copy(source, target, new DateConverterBeanCopier());
    }   
    private static String generateKey(Class<?> class1,Class<?>class2){
        return class1.toString() + class2.toString();
    }
    
    public static void main(String...strings){
        ProductFormVo pf;
        Long l=System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            ProductFormVo1 p = new ProductFormVo1();
            p.setArticleId("a");
            p.setCreateStamp(new Date());
            p.setThruDate("2000-11-11 12:00:00");
            pf = new ProductFormVo();
            BeanCopierUtils.copyProperties(p, pf);
        }
        System.out.println(System.currentTimeMillis()-l);
//        System.out.println(pf.getArticleId());
//        System.out.println(pf.getCreateStamp());
//        System.out.println(ConvertTools.formatDataStyle3(pf.getThruDate()));
    }
}

 它用到的DateConverterBeanCopier:

 

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author Jira
 *
 */
public class DateConverterBeanCopier implements net.sf.cglib.core.Converter
{
    public Object convert(Object arg1, Class arg0, Object context){
        if (arg1 instanceof String && arg0 != java.lang.String.class) {
            String p = (String) arg1;
            if (p == null || p.trim().length() == 0) {
                return null;
            }
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.parse(p.trim());
            } catch (Exception e) {
                try {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    return df.parse(p.trim());
                } catch (ParseException ex) {
                    return arg1;
                }
            }
        }/** 输入String ,输出String */
        else if (arg1 instanceof String && arg0 == java.lang.String.class) {
            return arg1;
        }/** 输入Date ,输出String */
        else if (arg1 instanceof java.util.Date) {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.format((java.util.Date) arg1);
            } catch (Exception e) {
                return null;
            }
        }/** 输入Date ,输出String */
        else if (arg1 instanceof java.sql.Date) {
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return df.format((java.sql.Date) arg1);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
}

 

你可能感兴趣的:(BeanCopier)