封裝屬于自己的對象拷貝類,讓代碼更加優雅
前言
實際項目開發過程中,經常需要進行數據轉換。例如將Bo轉換為Vo對象等
常用對象轉化工具
kits實現
本文主要簡單封裝了Spring BeanUtils, 基本可以滿足日常需求。
- 引入依賴
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>
- 代碼實現
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
/**
* <p>
* Bean拷貝工具類
* </p>
*
* @author lishaohui
* @since 2022-08-15
*/
public final class BeanCopierKits {
private BeanCopierKits() {
// ignore
}
/**
* Bean拷貝
*/
public static <S, T> void copyProperties(S source, T target) {
if (source == null) return;
BeanUtils.copyProperties(source, target);
}
/**
* Bean拷貝
*/
public static <S, T> T copyProperties(S source, Class<T> targetClazz) {
return copyProperties(source, targetClazz, null);
}
/**
* Bean拷貝
*/
public static <S, T> T copyProperties(S source, Class<T> targetClazz, BiConsumer<S, T> consumer) {
if (source == null) return null;
Preconditions.checkArgument(targetClazz != null);
try {
T target = targetClazz.newInstance();
copyProperties(source, target);
if (consumer != null) consumer.accept(source, target);
return target;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
/**
* Bean拷貝
*/
public static <S, T> List<T> copyProperties(List<S> sources, Class<T> targetClazz) {
return sources == null ? null : sources.stream()
.map(source -> copyProperties(source, targetClazz, null))
.collect(Collectors.toList());
}
/**
* Bean拷貝
*/
public static <S, T> List<T> copyProperties(List<S> sources, Class<T> targetClazz, BiConsumer<S, T> consumer) {
return sources == null ? null : sources.stream()
.map(source -> copyProperties(source, targetClazz, consumer))
.collect(Collectors.toList());
}
/**
* Bean拷貝
*/
public static <S, T> Set<T> copyProperties(Set<S> sources, Class<T> targetClazz) {
return sources == null ? null : sources.stream()
.map(source -> copyProperties(source, targetClazz, null)).collect(Collectors.toSet());
}
/**
* Bean拷貝
*/
public static <S, T> Set<T> copyProperties(Set<S> sources, Class<T> targetClazz, BiConsumer<S, T> consumer) {
return sources == null ? null : sources.stream()
.map(source -> copyProperties(source, targetClazz, consumer))
.collect(Collectors.toSet());
}
/**
* 獲取拷貝幫助類
*/
public static <S, T> BeanCopierHelper<S, T> copy(Class<T> targetClazz) {
return new BeanCopierHelper<S, T>().targetClazz(targetClazz);
}
/**
* 生成key
*/
private static String generateKey(Class<?> source, Class<?> target) {
return source.toString() + "-" + target.toString();
}
/**
* 拷貝幫助類
*
* @param <S>
* @param <T>
*/
private static class BeanCopierHelper<S, T> {
/**
* 拷貝完后的操作
*/
private BiConsumer<S, T> after;
/**
* 目標類型
*/
private Class<T> targetClazz;
/**
* 設置拷貝完的操作
*/
public BeanCopierHelper<S, T> after(BiConsumer<S, T> after) {
this.after = after;
return this;
}
/**
* 設置目標類型
*/
public BeanCopierHelper<S, T> targetClazz(Class<T> targetClazz) {
this.targetClazz = targetClazz;
return this;
}
/**
* 拷貝對象
*/
public T from(S source) {
return BeanCopierKits.copyProperties(source, targetClazz, after);
}
/**
* 拷貝Set
*/
public Set<T> fromSet(Set<S> sources) {
return BeanCopierKits.copyProperties(sources, targetClazz, after);
}
/**
* 拷貝Set
*/
public List<T> fromList(List<S> sources) {
return BeanCopierKits.copyProperties(sources, targetClazz, after);
}
}
}
- 使用
RoleInfoInGroupResp roleInfoInGroupResp = BeanCopierKits.copyProperties(groupMember, RoleInfoInGroupResp.class);

浙公網安備 33010602011771號