<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      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  閱讀(43)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 日韩秘 无码一区二区三区| 羞羞影院午夜男女爽爽免费视频| 国产97人人超碰CAO蜜芽PROM| 亚洲国产精品综合久久2007| 国产一区二区不卡视频在线| 亚洲第一极品精品无码久久| 猫咪www免费人成网站| 久热天堂在线视频精品伊人| 内射一区二区三区四区| 亚洲国产亚洲综合在线尤物| 大兴区| 国产成人午夜福利在线观看| 日韩中av免费在线观看| 国产成人亚洲无码淙合青草| 国产成人欧美一区二区三区在线| 五月天国产成人av免费观看| 精品无码久久久久国产| 最新偷拍一区二区三区| 国产成人精品亚洲午夜| 荡乳尤物h| 久久亚洲精品无码播放| 精品日本免费一区二区三区| 国产精品乱码久久久久久小说| 性一交一乱一乱一视频| 久久蜜臀av一区三区| 国产中年熟女高潮大集合| 亚洲天堂一区二区三区三州| 亚洲欧美人成电影在线观看| 麻豆精产国品一二三区区| 在线观看视频一区二区三区| 中文字幕精品无码一区二区| 国内精品自线在拍| 日韩不卡在线观看视频不卡| 日韩一区二区三区日韩精品| 粉嫩蜜臀av一区二区绯色| 亚洲精品久综合蜜| 日本在线a一区视频高清视频| 91精品国产老熟女在线| 性夜黄a爽影免费看| 97精品伊人久久久大香线蕉| 热久久这里只有精品99|