import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ft.suse.core.bean.Car;
import com.ft.suse.core.bean.People;
import com.ft.suse.core.bean.Person;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import ma.glasnost.orika.BoundMapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.Type;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.StopWatch;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 测试属性复制功能:
* - [x] 同名属性
* - [x] 非同名属性
* - [x] 导航属性,List,Map也可导航list[0],map['a']
* - [x] List & Array
* - [x] 集合泛型
* - [x] 递归
* - [x] 类型转换
* - [x] 排除
* ===========================================================
* 同类型属性复制使用 CopyByReferenceConverter
* 当同名属性不同类型,又没使用converter,或converter无效,会报错
* ===========================================================
* * 注意不要使用 MapperFacade.mapList ,应该使用foreach: BoundMapperFacade.map
* * 性能差10倍+,在复制10_000_000个对象的情况下
*/
public class OrikaTest {
private MapperFactory mapperFactory;
private ObjectMapper objectMapper;
@Before
public void setup() {
objectMapper = new ObjectMapper();
mapperFactory = new DefaultMapperFactory.Builder().build();
/* 注册类型转换器(全局)*/
// mapperFactory.getConverterFactory().registerConverter(new LocalDatetimeConverter());
/* 注册类型转换器(非全局) */
mapperFactory.getConverterFactory().registerConverter("LocalDateTime2Long", new LocalDateTime2LongConverter());
/* 注册<PollutantSource.class,PollutantSourceDTO> 属性复制配置 */
mapperFactory.classMap(PollutantSource.class, PollutantSourceDTO.class)
.field("socialCode", "orgCode") //非同名属性
.field("longitude", "geo.longitude") //导航属性
.field("latitude", "geo.latitude") //导航属性
.fieldMap("gmtCreated", "gmtCreated").converter("LocalDateTime2Long") // 这个属性使用 converter
.add()
.byDefault().register();
/* 递归 */
mapperFactory.classMap(Xzqh.class, Xzqh2.class)
.field("name", "name2")
.field("code", "code2")
.byDefault().register();
}
@Test
public void testOrika() throws JsonProcessingException {
/* 通用的 MapperFacade ,屏蔽对mapperFactory 底层接口 */
// MapperFacade mapperFacade = mapperFactory.getMapperFacade();
/* 专用的 BoundMapperFacade<<PollutantSource, PollutantSourceDTO>> ,屏蔽对mapperFactory 底层接口*/
BoundMapperFacade<PollutantSource, PollutantSourceDTO> mapperFacade = mapperFactory.getMapperFacade(PollutantSource.class, PollutantSourceDTO.class);
PollutantSource ps = new PollutantSource();
ps.addresses = List.of("贵州省贵阳市花果园", "贵州省贵安新区马场镇", "贵州省遵义市");
ps.gmtCreated = LocalDateTime.now();
ps.id = "123123";
ps.latitude = "24.24";
ps.longitude = "110.10";
ps.socialCode = "520510500";
ps.xzqhs = List.of(new Xzqh("5201", "贵州省贵阳市"), new Xzqh("5203", "贵州省遵义市"), new Xzqh("529001", "贵州省贵安新区"));
/* map */
PollutantSourceDTO psDTO = mapperFacade.map(ps);
System.out.println(objectMapper.writeValueAsString(psDTO));
/* reverse map */
PollutantSource ps2 = mapperFacade.mapReverse(psDTO);
System.out.println(objectMapper.writeValueAsString(ps2));
}
// 自定义单向 converter
// class LocalDateTime2LongConverter extends CustomConverter<LocalDateTime, Long> {
//
// @Override
// public Long convert(LocalDateTime localDateTime, Type<? extends Long> type) {
// return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
// }
// }
// 自定义双向 converter
class LocalDateTime2LongConverter extends BidirectionalConverter<LocalDateTime, Long> {
@Override
public Long convertTo(LocalDateTime localDateTime, Type<Long> type) {
return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
}
@Override
public LocalDateTime convertFrom(Long aLong, Type<LocalDateTime> type) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(aLong), ZoneOffset.of("+8").normalized());
}
}
@Test
public void benchmarkOrika() throws JsonProcessingException {
long num = 10_000_000;
ArrayList<Person> personList = Lists.newArrayList();
ArrayList<Car> cars = Lists.newArrayList(new Car("bmw", new BigDecimal("150.34"), false),
new Car("honda", new BigDecimal("77.88"), true));
StopWatch sw = new StopWatch("2");
sw.start("new objects");
for (int i = 0; i < num; i++) {
Person p = new Person();
p.setAge(i);
p.setName("person" + i);
p.setCars(cars);
personList.add(p);
}
sw.stop();
mapperFactory.classMap(Person.class, People.class).exclude("cars").byDefault().register();
// mapperFactory.classMap(Car.class, SUV.class).byDefault().register();
BoundMapperFacade<Person, People> mapperFacade = mapperFactory.getMapperFacade(Person.class, People.class);
Runtime rt = Runtime.getRuntime();
long l = rt.freeMemory();
sw.start("测试Orika map(" + num + ")");
List<People> peopleList = personList.stream().map(p -> mapperFacade.map(p)).collect(Collectors.toList());
sw.stop();
System.out.println(sw.prettyPrint());
System.out.println("memory-usage=" + ((l - rt.freeMemory()) / 1024) + "K");
System.out.println(peopleList.get(11).getCars());
// ObjectMapper om = new ObjectMapper();
// System.out.println("person: " + om.writeValueAsString(personList.get(personList.size() - 11)));
// System.out.println("person cars: " + personList.get(personList.size() - 11).getCars());
// System.out.println("people: " + om.writeValueAsString(peopleList.get(peopleList.size() - 11)));
// System.out.println("people cars: " + peopleList.get(peopleList.size() - 11).getCars());
// SUV suv = peopleList.get(0).getCars().get(0);
// System.out.println(suv);
/**
* 复制cars
* StopWatch '2': running time (millis) = 188517
* -----------------------------------------
* ms % Task name
* -----------------------------------------
* 07349 004% new objects
* 181168 096% 测试Orika mapAsList(10000000)
*/
/**
* 排除cars
* memory-usage=512000K
* StopWatch '2': running time (millis) = 101239
* -----------------------------------------
* ms % Task name
* -----------------------------------------
* 07108 007% new objects
* 94131 093% 测试Orika mapAsList(10000000)
*/
/**
* 排除cars
* memory-usage=593920K
* -----------------------------------------
* ms % Task name
* -----------------------------------------
* 06336 036% new objects
* 11050 064% 测试Orika map(10000000)
*/
}
//==================================================================================
// Entity & DTO
//==================================================================================
@Getter
@Setter
public static class PollutantSource {
private String id;
private List<Xzqh> xzqhs; //递归测试,集合泛型测试
private String socialCode; //非同名测试
private LocalDateTime gmtCreated; //类型转换
private List<String> addresses; //List & Array
/* 导航属性 */
private String latitude;
private String longitude;
}
@Getter
@Setter
public static class PollutantSourceDTO {
private String id;
private List<Xzqh2> xzqhs; //递归测试,集合泛型测试
private String orgCode; //非同名测试
private Long gmtCreated; //类型转换
private String[] addresses; //List & Array
private Geo geo;
}
@Getter
@Setter
public static class Geo {
private String latitude;
private String longitude;
}
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class Xzqh {
private String code;
private String name;
}
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class Xzqh2 {
private String code2;
private String name2;
}
//==================================================================
// more advance example
//==================================================================
/*
mapperFactory.classMap(Source.class, Destination.class)
.byDefault()
.customize(
new CustomMapper<Source, Destination> {
public void mapAtoB(A a, B b, MappingContext context) {
// add your custom mapping code here
}
}
.register();
*/
/*
mapperFactory.classMap(BasicPerson.class, BasicPersonDto.class)
.mapNulls(false).mapNullsInReverse(false)
.fieldMap("field1", "fieldOne").mapNulls(true).mapNullsInReverse(true).add()
.field("field2", "fieldTwo")
.byDefault()
.register();
*/
}
Orika
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...