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

来源:互联网 发布:知乎 如何挑选浴缸 编辑:程序博客网 时间:2024/06/07 00:11

项目环境:

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,targetList不允许为空     *     * @param sourceList     * @param targetList     */    public static void copyPropertiesList(List sourceList, List targetList) {        if (CollectionUtils.isEmpty(sourceList) || CollectionUtils.isEmpty(targetList)) {            throw new CommonException(CommonErrorEnum.PARAM_ERROR);        }        sourceList.forEach(items -> {            Object target = new Object();            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 0