码迷,mamicode.com
首页 > 其他好文 > 详细

自定义BeanUtils

时间:2018-10-09 19:05:25      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:cto   get   tin   getc   bug   ret   ati   new   nta   

package com.icil.booking.service.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.icil.fw.logging.ICILLogger;
import com.icil.fw.logging.ICILLoggerFactory;

import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.core.Converter;

public class CglibBeanCopierUtils {

    private static final ICILLogger logger = ICILLoggerFactory
            .getInstance(CglibBeanCopierUtils.class);

    public static Map<String, BeanCopier> beanCopierMap = new HashMap<String, BeanCopier>();

    /**
     * @Title: copyProperties
     * @Description: TODO(bean属性转换)
     * @param source
     *            资源类
     * @param target
     *            目标类
     * @author yushaojian
     * @date 2015年11月25日下午4:56:44
     */
    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 Converter() {
            private final Map<String, BeanCopier> bcMap = new HashMap<String, BeanCopier>();

            @Override
            public Object convert(Object sourcePropVal, Class targetPropClazz,
                    Object targetSetMethodName) {
                /*logger.debug("======>>>>> sourcePropVal " + sourcePropVal
                        + "======>>>>>targetPropClazz " + targetPropClazz
                        + "======>>>>>targetSetMethodName "
                        + targetSetMethodName);*/
                /*if (BigDecimal.class.equals(targetPropClazz)) {
                    BigDecimal bd = null;
                    if (sourcePropVal != null && !"".equals(sourcePropVal)) {
                        bd = new BigDecimal(sourcePropVal.toString());
                        bd=bd.setScale(4, BigDecimal.ROUND_HALF_UP);  
                    }
                    return bd;
                } else if (Integer.class.equals(targetPropClazz)) {
                    Integer value = null;
                    if (sourcePropVal != null && !"".equals(sourcePropVal)) {
                        value = Integer.parseInt(sourcePropVal.toString());
                    }
                    return value;
                } else if (Date.class.equals(targetPropClazz)) {
                    Date date = null;
                    if(sourcePropVal!=null && !"".equals(sourcePropVal)){
                        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
                        try {
                            date = sdf.parse((String)sourcePropVal);
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    return date;
                }else if (int.class.equals(targetPropClazz)){
                    int value =0 ;
                    if (sourcePropVal != null && !"".equals(sourcePropVal)) {
                        value = Integer.parseInt(sourcePropVal.toString());
                    }
                    return value;
                }*/
                return sourcePropVal;
            }
        });
    }

    protected static Object convertListGenericType(Object sourcePropVal) {
        // TODO Auto-generated method stub
        return null;
    }

    private static String generateKey(Class<?> class1, Class<?> class2) {
        return class1.toString() + class2.toString();
    }
}

 

自定义BeanUtils

标签:cto   get   tin   getc   bug   ret   ati   new   nta   

原文地址:https://www.cnblogs.com/lshan/p/9761994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!