45 lines
1.3 KiB
Java
45 lines
1.3 KiB
Java
package com.ycwl.basic.utils;
|
|
|
|
import org.springframework.cglib.beans.BeanCopier;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @date 2022年03月09日 9:04
|
|
* @author wenshijia
|
|
* BeanCopier工具类
|
|
*/
|
|
public class BeanCopierUtils {
|
|
|
|
public static Map<String, BeanCopier> beanCopierCacheMap = new HashMap<>();
|
|
|
|
/**
|
|
*
|
|
* 将soruce对象的属性转换给target对象
|
|
* @date 2022/3/9 9:11
|
|
* @param source 需要转换的对象
|
|
* @param target 目标对象
|
|
*/
|
|
public static void copyProperties(Object source, Object target) {
|
|
BeanCopier beanCopier;
|
|
|
|
String cacheKey = source.getClass().toString() + target.getClass().toString();
|
|
|
|
if (!beanCopierCacheMap.containsKey(cacheKey)) {
|
|
synchronized (BeanCopierUtils.class) {
|
|
if (!beanCopierCacheMap.containsKey(cacheKey)) {
|
|
beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
|
|
beanCopierCacheMap.put(cacheKey, beanCopier);
|
|
} else {
|
|
beanCopier = beanCopierCacheMap.get(cacheKey);
|
|
}
|
|
}
|
|
} else {
|
|
beanCopier = beanCopierCacheMap.get(cacheKey);
|
|
}
|
|
|
|
beanCopier.copy(source, target, null);
|
|
}
|
|
}
|