java bean 对象与map对象属性相互拷贝

/**
 * @author  zsl
 */
public class BeanUtil {

    /**
     * 将map中的值拷贝到bean中
     * @param source  属性来源map
     * @param target  目标Bean
     */
    public static  void copyProperties2Bean(Map<String,Object> source, Object target) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
            BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor prop: properties) {
                String key = prop.getName();
                if(source.containsKey(key) && source.get(key) != null){
                    Object value = source.get(key);
                    Method setMethod = prop.getWriteMethod();
                    setMethod.invoke(target,value);
                }
            }


    }

    /**
     * 将bean中的属性存入到map中
     * @param source  来源bean
     * @param target
     */
    public static  void copyProperties2Map(Object source,Map<String,Object>  target) throws IntrospectionException, InvocationTargetException, IllegalAccessException {


            //1.获取bean信息
            BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
            if(properties != null && properties.length>0){
                for (PropertyDescriptor prop :properties) {
                    //2.得到属性名
                    String name = prop.getName();
                    //3.过滤class属性
                    if(!"class".equals(name)){
                        //4.得到属性的get方法
                        Method getter = prop.getReadMethod();

                        //5.获取属性值
                        Object value = getter.invoke(source);
                        //6.放入map中
                        if(value != null ){
                            target.put(name,value);
                        }
                    }
                }
            }
    }


    /**
     * 根据字段名从对象反射取值
     * @param source
     * @param filed
     * @return
     * @throws IntrospectionException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object getValue(Object source,String filed) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        //1.获取bean信息
        BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
        PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();

        if(properties != null && properties.length>0){
            for (PropertyDescriptor prop :properties) {
                //2.得到属性名
                String name = prop.getName();
                //3.过滤class属性
                if(!"class".equals(name) && Objects.equals(filed,name)){
                    //4.得到属性的get方法
                    Method getter = prop.getReadMethod();

                    //5.获取属性值
                   Object  value = getter.invoke(source);
                    return value;

                }
            }
        }
        return  null;
    }

    /**
     * 实现bean之间的属性对拷
     * 之间使用spring 的BeanUtils工具进行实现
     * @param source
     * @param target
     * @param ignoreProperties 忽略的属性
     */
    public static void copyProperties(Object source ,Object target,String... ignoreProperties){
        BeanUtils.copyProperties(source,target,ignoreProperties);
    }




}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值