java 两个实体类转换_实体类之间的转换

这个博客介绍了如何使用 `BeanUtils` 工具类进行Java实体类之间的转换,包括单个对象转换、两个对象合并转换、忽略指定属性转换、批量对象转换等功能。该工具类基于Dozer库实现,提供了灵活的属性赋值和对象类型转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

packagecom.dongl.utils;importorg.dozer.DozerBeanMapper;importorg.springframework.beans.BeansException;importorg.springframework.beans.FatalBeanException;importorg.springframework.util.Assert;importorg.springframework.util.StringUtils;importjava.beans.PropertyDescriptor;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.lang.reflect.Modifier;import java.util.*;/***@authorD-L

* @Classname BeanUtils

* @Version 1.0

* @Description 通过无参数实例化对象和复制属性值的方式

* 完成实体类之间的转换

* 完成map->实体类

* 实体类->map

* 基于Dozer转换对象的类型

* 对bean的特定属性赋值

* @Date 2020/8/5*/

public class BeanUtils extendsorg.springframework.beans.BeanUtils{/*** 持有Dozer单例, 避免重复创建DozerMapper消耗资源.*/

private static DozerBeanMapper dozer = newDozerBeanMapper();/*** 通过无参数实例化目标对象和复制属性,将POJO对象转换成相应的对象

*

*@paramsource 原对象

*@paramtype 目标类型

*@return转换后的对象*/

public static T convert(Object source, Classtype) {

T target=instantiateClass(type);if (source == null) {returntarget;

}

copyProperties(source, target);returntarget;

}/*** 通过无参数实例化目标对象和复制属性,将两个POJO对象按照顺序转换成相应的对象

* 如果原对象有相同的属性 会出现属性值覆盖

*@paramsource1 原对象1

*@paramsource2 原对象2

*@paramtype 目标类型

*@param

*@return返回转换后的对象*/

public static T converts(Object source1, Object source2, Classtype) {

T target=instantiateClass(type);if (source1 == null && source2 == null) {returntarget;

}

copyProperties(source1, target);

copyProperties(source2, target);returntarget;

}/*** 通过无参数实例化目标对象和复制属性,将POJO对象转换成相应的对象,可忽略部分属性

*@paramsource 原对象

*@paramtype 目标类型

*@paramignoreProperties 忽略的属性列表

*@return转换后的对象*/

public static T convert(Object source, Classtype, String[] ignoreProperties) {

T target=instantiateClass(type);

copyProperties(source, target, ignoreProperties);returntarget;

}/*** 通过无参数实例化目标对象和复制属性,将POJO对象批量转换成指定类型的对象

*@paramsources List 原对象列表

*@paramtype 目标类型

*@returnList 目标类型对象列表*/

public static List convert(List sources, Classtype) {

List items = new ArrayList();if (sources == null) {returnitems;

}

Iterator iter =sources.iterator();while(iter.hasNext()) {

items.add(convert(iter.next(), type));

}returnitems;

}/*** 通过无参数实例化目标对象和复制属性,将POJO对象批量转换成指定类型的对象

*

*@paramsources

* List 原对象列表

*@paramtype

* 目标类型

*@paramignoreProperties

* 忽略的属性列表

*@returnList 目标类型对象列表*/

public static List convert(List sources, Classtype,

Class>editable, String[] ignoreProperties) {

List items = new ArrayList();if (sources == null) {returnitems;

}

Iterator iter =sources.iterator();while(iter.hasNext()) {

items.add(convert(iter.next(), type, ignoreProperties));

}returnitems;

}/*** 将Map的属性设置到bean中

*

*@parambean

*@paramproperties

*@throwsIllegalAccessException

*@throwsInvocationTargetException*/

public static void populate(Object bean, Mapproperties)throwsIllegalAccessException, InvocationTargetException {

org.apache.commons.beanutils.BeanUtils.populate(bean, properties);

}/*** 将Bean转化为Map对象

*

*@parambean

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

public static Map, ?>describe(Object bean)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {returnorg.apache.commons.beanutils.BeanUtils.describe(bean);

}/*** 对bean的特定属性赋值

*

*@parambean

*@paramname

*@paramvalue

*@throwsIllegalAccessException

*@throwsInvocationTargetException*/

public static voidcopyProperty(Object bean, String name, Object value)throwsIllegalAccessException, InvocationTargetException {

org.apache.commons.beanutils.BeanUtils.copyProperty(bean, name, value);

}/*** 获取bean的特定属性

*

*@parambean

*@paramname

*@throwsNoSuchMethodException

*@throwsIllegalAccessException

*@throwsInvocationTargetException*/

public staticString getProperty(Object bean, String name)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {returnorg.apache.commons.beanutils.BeanUtils.getProperty(bean, name);

}/*** 基于Dozer转换对象的类型.

*

*@paramsource

*@paramdestinationClass

*@return

*/

public static T map(Object source, ClassdestinationClass) {returndozer.map(source, destinationClass);

}/*** 基于Dozer转换List中对象的类型.

*

*@paramsourceList

*@paramdestinationClass

*@return

*/

public static List map(ListsourceList,

ClassdestinationClass) {

List destinationList = new ArrayList();for(Object sourceObject : sourceList) {

T destinationObject=dozer.map(sourceObject, destinationClass);

destinationList.add(destinationObject);

}returndestinationList;

}/*** 复制对象到一个已经存在的对象

*

*@paramsource

* 源对象

*@paramtarget

* 目标对象*/

public static voidmapProperties(Object source, Object target) {

dozer.map(source, target);

}/*** 复制对象的指定属性

*@paramsource

*@paramtarget

*@paramnames

*@throwsBeansException*/

public static voidcopyPropertiesByNames(Object source, Object target,

String names)throwsBeansException {

Assert.notNull(source,"Source must not be null");

Assert.notNull(target,"Target must not be null");

Assert.hasText(names,"names must not be empty");

String[] keys= names.split(",");for(String name : keys) {

name=name.trim();

PropertyDescriptor targetPd=getPropertyDescriptor(

target.getClass(), name);

PropertyDescriptor sourcePd=getPropertyDescriptor(

source.getClass(), name);if (sourcePd != null && sourcePd.getReadMethod() != null

&& targetPd != null && targetPd.getWriteMethod() != null) {try{

Method readMethod=sourcePd.getReadMethod();if (!Modifier.isPublic(readMethod.getDeclaringClass()

.getModifiers())) {

readMethod.setAccessible(true);

}

Object value=readMethod.invoke(source);

Method writeMethod=targetPd.getWriteMethod();if (!Modifier.isPublic(writeMethod.getDeclaringClass()

.getModifiers())) {

writeMethod.setAccessible(true);

}

writeMethod.invoke(target, value);

}catch(Throwable ex) {throw newFatalBeanException("Could not copy properties from source to target",

ex);

}

}

}

}/*** 将Bean的指定属性集转化为Map对象

*

*@parambean

*@paramnames

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

public static Mapdescribe(Object bean, String names)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {

String[] attrs= StringUtils.split(names, ",");for (int i = 0; i < attrs.length; i++) {

attrs[i]=attrs[i].trim();

}returndescribe(bean, attrs);

}/*** 将Bean的指定属性集转化为Map对象

*

*@parambean

*@return*@throwsIllegalAccessException

*@throwsInvocationTargetException

*@throwsNoSuchMethodException*/

public static Mapdescribe(Object bean, String[] names)throwsIllegalAccessException, InvocationTargetException,

NoSuchMethodException {

Assert.notNull(bean,"Source must not be null");

Assert.notEmpty(names,"names must not be empty");

Map map = new HashMap();for(String name : names) {

map.put(name, getProperty(bean, name));

}returnmap;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值