对象之间属性赋值,改造BeanUtils.copyProperties()方法,属性类型不同也要转换
我们知道要将DO对象转换成VO这种时候一般都用BeanUtils.copyProperties(),但是有个要求就是属性名和属性的类型都必须完全一致才行,否则该属性就不会拷贝,依旧是null(名称或者类型不同本来也就没办法拷贝)。
我们公司的VO对象里以为某种原因对象参数只能使用基本数据类型,这样DO里有Date,Bigdecimal之类的就惨了,都需要单独进行处理。于是就写了这么个工具类,也是从copyProperties这个方法里面的代码改成的,目前只写了处理Date转String,Bigdecimal转Doub,有其他转换需要的自己再添加(不然不同类型会报错的)。
我们公司的VO对象里以为某种原因对象参数只能使用基本数据类型,这样DO里有Date,Bigdecimal之类的就惨了,都需要单独进行处理。于是就写了这么个工具类,也是从copyProperties这个方法里面的代码改成的,目前只写了处理Date转String,Bigdecimal转Doub,有其他转换需要的自己再添加(不然不同类型会报错的)。
package util;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/**
* @author :王可可
* @date :Created in 2021/3/27 19:07
*/
public class MyUtil {
public static void copyProperties(Object source, Object target, String dateFormat) throws InvocationTargetException, IllegalAccessException {
copy(source, dateFormat, target);
}
public static void copyProperties(Object source, Object target) throws InvocationTargetException, IllegalAccessException {
String dateFormat = null;
copy(source, dateFormat, target);
}
public static <T> T copyProperties(Object source, Class<T> targetClass) throws InvocationTargetException, IllegalAccessException, InstantiationException {
String dateFormat = null;
T target = null;
if (source != null){
target = targetClass.newInstance();
copy(source, dateFormat, target);
}
return target;
}
public static <T> T copyProperties(Object source, Class<T> targetClass, String dateFormat) throws InvocationTargetException, IllegalAccessException, InstantiationException {
T target = null;
if (source != null){
target = targetClass.newInstance();
copy(source, dateFormat, target);
}
return target;
}
public static <T> List listConverter(List list, Class<T> classT, String dateFormat) throws InvocationTargetException, IllegalAccessException, InstantiationException {
List<T> targetList = new ArrayList();
if (list != null && list.size() != 0){
Iterator var3 = list.iterator();
while(var3.hasNext()) {
Object object = var3.next();
targetList.add(copyProperties(object, classT, dateFormat));
}
}
return targetList;
}
private static void copy(Object source, String dateFormat, Object target) throws IllegalAccessException, InvocationTargetException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
if (dateFormat == null) {
dateFormat = "yyyy-MM-dd HH:mm:ss";
}
Class<?> aClass = source.getClass();
PropertyDescriptor[] apds = BeanUtils.getPropertyDescriptors(aClass);
Class<?> bClass = target.getClass();
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(bClass);
int n = pds.length;
for (int i = 0; i < n; ++i) {
PropertyDescriptor targetPd = pds[i];
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null) {
PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
for (int j = 0; j < apds.length; j++) {
PropertyDescriptor apd = apds[j];
if (apd.getReadMethod().getName().equals(readMethod.getName())) {
// 将对象2的属性get方法转换成对象1的get方法(获得的value的类型就是对象1的该属性的类型)
readMethod = apd.getReadMethod();
continue;
}
}
if (readMethod != null) {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 此处对要赋的值类型进行判断和处理
if (value.getClass().equals(Date.class)) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
value = sdf.format(value);
} else if (value.getClass().equals(BigDecimal.class)) {
BigDecimal b = (BigDecimal) value;
value = b.doubleValue();
}
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
}
}
}
}
}
...全文
相关推荐
如何使用BeanUtils.copyProperties进行对象之间的属性赋值
主要介绍了使用BeanUtils.copyProperties进行对象之间的属性赋值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
java Beanutils.copyProperties( )用法详解
主要介绍了java Beanutils.copyProperties( )用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
BeanUtilsBean对象复制使用(类型可以自动转化)
NULL 博文链接:https://yuhuiblog695685688425687986842568269.iteye.com/blog/2396139
Java实现不同的类的属性之间相互赋值
今天小编就为大家分享一篇关于Java实现不同的类的属性之间相互赋值,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
使用BeanUtils操作属性的一个小坑
详见 http://blog.csdn.net/arttecme/article/details/8566818


