我們知道要將DO物件轉換成VO這種時候一般都用BeanUtils.copyProperties(),但是有個要求就是屬性名和屬性的型別都必須完全一致才行,否則該屬性就不會拷貝,依舊是null(名稱或者型別不同本來也就沒辦法拷貝)。
我們公司的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);
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/269945.html
標籤:Java EE
下一篇:Mac用來開發Java適合嗎
