Fork me on GitHub

封装属于自己的对象拷贝类,让代码更加优雅

前言

实际项目开发过程中,经常需要进行数据转换。例如将Bo转换为Vo对象等

常用对象转化工具

  • mapstruct 官网

  • Spring BeanUtils

  • Apache BeanUtils

  • ModelMapper 官网

  • ...

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);

__EOF__

本文作者Hui Li
本文链接https://www.cnblogs.com/leedev-blog/p/17602787.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Hui_Li  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示