前言
实际项目开发过程中,经常需要进行数据转换。例如将Bo转换为Vo对象等
常用对象转化工具
-
mapstruct 官网
-
Spring BeanUtils
-
Apache BeanUtils
-
ModelMapper 官网
-
...
kits实现
本文主要简单封装了Spring BeanUtils
, 基本可以满足日常需求。
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;
public final class BeanCopierKits {
private BeanCopierKits() {
}
public static <S, T> void copyProperties(S source, T target) {
if (source == null) return;
BeanUtils.copyProperties(source, target);
}
public static <S, T> T copyProperties(S source, Class<T> targetClazz) {
return copyProperties(source, targetClazz, null);
}
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;
}
}
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());
}
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());
}
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());
}
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);
}
private static String generateKey(Class<?> source, Class<?> target) {
return source.toString() + "-" + target.toString();
}
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);
}
public Set<T> fromSet(Set<S> sources) {
return BeanCopierKits.copyProperties(sources, targetClazz, after);
}
public List<T> fromList(List<S> sources) {
return BeanCopierKits.copyProperties(sources, targetClazz, after);
}
}
}
__EOF__
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)