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);
posted @ 2023-08-03 11:15  Hui_Li  阅读(19)  评论(0编辑  收藏  举报