二、java项目常用工具类之beancopy,bean和map转换工具类

 

项目环境:

jdk1.8+spring4.3.12

一、问题描述及试用场景:

在项目规范中,要求类名以DO为尾的类作为数据库层实体bean,类名以MO为尾的类作为系统传输层实体bean,类名以VO为尾的类作为服务端与前端交互的实体bean。由于以上要求,需要在各个bean直接进行copy数据,除了傻瓜式的set/get or constructor来copy数据外,spring提供了直接copybean的工具类,原理其实就是通过java反射来根据目标bean的字段名调用其set方法来设值注入。除此之外,项目中还常见map与bean之间的转换。特封装此类,以供大家使用。

二、样例代码:

 

 

package org.egg.utils;

import org.egg.enums.CommonErrorEnum;
import org.egg.exception.CommonException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dataochen
 * @Description bean工具类
 * @date: 2017/11/7 17:30
 */
public class BeanUtil {

    /**
     * 拷贝实体,source,target不允许为空
     *
     * @param source
     * @param target
     */
    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * 拷贝实体集合,sourceList
     *只支持自定义实体集合拷贝 
     *应用场景:DTO <=> DO 等
     */
public static void copyPropertiesList(List sourceList, List targetList,Class clazz) throws InstantiationException,IllegalAccessException {
    if (CollectionUtils.isEmpty(sourceList)) {
        throw new CommonException(CommonErrorEnum.PARAM_ERROR);
    }
    for (Object items : sourceList) {
        Object target = clazz.newInstance();
        BeanUtils.copyProperties(items, target);
        targetList.add(target);
    }

}
 
/**
 * Map --> Bean 2: 利用org.apache.commons.beanutils 工具类实现 Map --> Bean
 *
 * @param map
 * @param obj
 */
public static void transMap2Bean2(Map<String, Object> map, Object obj) throws InvocationTargetException, IllegalAccessException {
    if (map == null || obj == null) {
        return;
    }
    org.apache.commons.beanutils.BeanUtils.populate(obj, map);
}

/**
 * Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
 *
 * @param map
 * @param obj
 */
public static void transMap2Bean(Map<String, Object> map, Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
    BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

    for (PropertyDescriptor property : propertyDescriptors) {
        String key = property.getName();
        if (map.containsKey(key)) {
            Object value = map.get(key);
            // 得到property对应的setter方法
            Method setter = property.getWriteMethod();
            setter.invoke(obj, value);
        }
    }
}

/**
 * Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
 *
 * @param obj
 */
public static Map<String, Object> transBean2Map(Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {

    if (obj == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<String, Object>();
    BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor property : propertyDescriptors) {
        String key = property.getName();

        // 过滤class属性
        if (!key.equals("class")) {
            // 得到property对应的getter方法
            Method getter = property.getReadMethod();
            Object value = getter.invoke(obj);

            map.put(key, value);
        }

    }
    return map;
}

 

 

 

 

 

 

 

代码所用jar包maven坐标:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>

 

 

项目地址:https://github.com/SuperEggMan/renting_frame_finish_bek; ps:感兴趣的可以start哦!

声明:此项目仅是抛砖引玉,内容不是特别完善。如有转载,请注明此处。

 

 

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值