Authored by kun

增加orika依赖

@@ -239,6 +239,11 @@ @@ -239,6 +239,11 @@
239 <groupId>com.yoho.service.model</groupId> 239 <groupId>com.yoho.service.model</groupId>
240 <artifactId>shops-service-model</artifactId> 240 <artifactId>shops-service-model</artifactId>
241 </dependency> 241 </dependency>
  242 + <dependency>
  243 + <groupId>ma.glasnost.orika</groupId>
  244 + <artifactId>orika-core</artifactId>
  245 + <version>1.5.2</version>
  246 + </dependency>
242 247
243 </dependencies> 248 </dependencies>
244 </project> 249 </project>
  1 +package com.yohoufo.common.utils;
  2 +
  3 +import ma.glasnost.orika.MapperFacade;
  4 +import ma.glasnost.orika.MapperFactory;
  5 +import ma.glasnost.orika.impl.DefaultMapperFactory;
  6 +import ma.glasnost.orika.metadata.ClassMapBuilder;
  7 +
  8 +import java.util.List;
  9 +
  10 +/**
  11 + * @author kun.wang
  12 + * @date 2018/9/11
  13 + */
  14 +public final class OrikaUtils {
  15 + private OrikaUtils() {
  16 +
  17 + }
  18 +
  19 + private static MapperFactory mapperFactory;
  20 +
  21 + private static MapperFacade mapperFacade;
  22 +
  23 + static {
  24 + mapperFactory = new DefaultMapperFactory.Builder().build();
  25 + mapperFacade = mapperFactory.getMapperFacade();
  26 + }
  27 +
  28 + public static <S, D> void map(S source, D dest) {
  29 + mapperFacade.map(source, dest);
  30 + }
  31 +
  32 + public static <S, D> D map(S source, Class<D> destinationClass) {
  33 + return mapperFacade.map(source, destinationClass);
  34 + }
  35 +
  36 + public static <S, D> List<D> mapToList(Iterable<S> source, Class<D> destinationClass) {
  37 + return mapperFacade.mapAsList(source, destinationClass);
  38 + }
  39 +
  40 + public static <S, D> D mapWithExclude(S source, Class<D> destinationClass, String... fieldNames) {
  41 + MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
  42 + ClassMapBuilder<S, D> classMapBuilder = mapperFactory.classMap((Class<S>) source.getClass(), destinationClass);
  43 + for (String fieldName : fieldNames) {
  44 + classMapBuilder.exclude(fieldName);
  45 + }
  46 + classMapBuilder.byDefault().register();
  47 + MapperFacade mapperFacade = mapperFactory.getMapperFacade();
  48 + return mapperFacade.map(source, destinationClass);
  49 + }
  50 +}