作者:京东零售 张宾
Orika是一个简单、快速的JavaBean拷贝框架,它能够递归地将数据从一个JavaBean复制到另一个JavaBean,这在多层应用开发中是非常有用的。
工作中,我们经常需要将对象转换成不同的形式以适应不同的api,或者在不同业务层中传输对象而不同分层的对象存在不同的格式,因此我们需要编写映射代码将对象中的属性值从一种类型转换成另一种类型。
Orika用于简化多层之间的对象映射,避免苦苦挣扎于手工编码和基于反射的映射。Orika关注尽可能地自动化,同时根据需要提供配置和扩展实现定制。
Java反射:性能问题、无法解决嵌套javabean映射复制
手工映射:硬编码、代码量大
序列化方式:通过json、xml的序列化和反序列化方式,字段参数不相同时,需要硬编码赋值
BeanUtils:apache的BeanUtils和spring的BeanUtils中拷贝方法的原理都是先用jdk中 java.beans.Introspector类的getBeanInfo()方法获取对象的属性信息及属性get/set方法,接着使用反射(Method的invoke(Object obj, Object... args))方法进行赋值。apache支持名称相同但类型不同的属性的转换,spring支持忽略某些属性不进行映射,他们都设置了缓存保存已解析过的BeanInfo信息。
BeanCopier:cglib的BeanCopier采用了不同的方法:它不是利用反射对属性进行赋值,而是直接使用ASM的MethodVisitor直接编写各属性的get/set方法(具体过程可见BeanCopier类的generateClass(ClassVisitor v)方法)生成class文件,然后进行执行。由于是直接生成字节码执行,所以BeanCopier的性能较采用反射的BeanUtils有较大提高。
Orika:底层采用了javassist类库生成Bean映射的字节码,之后直接加载执行生成的字节码文件,因此在速度上比使用反射进行赋值会快很多。Orika支持递归映射,将映射嵌套类直到用“简单”类型完成映射。它还包含故障保险,以正确处理正在尝试映射的对象中的递归引用。
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
//字段名相同映射
mapperFactory.classMap(Person.class, PersonInfo.class).byDefault().register();
MapperFacade mapperFacade = mapperFactory.getMapperFacade();
Person person = new Person();
person.setFirstName("张");
person.setLastName("三");
PersonInfo personInfo = mapperFacade.map(person, PersonInfo.class);
输出结果:
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(Person.class, PersonDesc.class)
.field("firstName", "givenName").field("lastName", "sirName")
.byDefault() .register();
MapperFacade mapperFacadeDesc = mapperFactory.getMapperFacade();
Person personNew = new Person();
personNew.setFirstName("王");
personNew.setLastName("五");
PersonDesc personDesc = mapperFacadeDesc.map(personNew, PersonDesc.class);
输出结果:
public static class PersonNameList {
private List
public PersonNameList(List
this.nameList = nameList;
}
//getter/setter方法
}
mapperFactory.classMap(PersonNameList.class, Person.class)
.field("nameList[0]", "firstName")
.field("nameList[1]", "lastName").register();
List
Person person = mapperFactory.getMapperFacade().map(new PersonNameList(nameParts), Person.class);
输出结果:
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(Person.class, Person.class).byDefault().register();
Person person = new Person();
person.setFirstName("张");
person.setLastName("三");
Person personNew = mapperFactory.getMapperFacade().map(person, Person.class);
输出结果:
mapperFactory.classMap(Flight.class, Flight.class).byDefault().register();
Cabin cabinY = new Cabin("Y", "8", 1200);
Cabin cabinF = new Cabin("C", "6", 1900);
Cabin cabinC = new Cabin("F", "A", 2400);
Flight flight = new Flight("CA1831", Lists.newArrayList(cabinY, cabinF, cabinC));
Flight flightCopy = mapperFactory.getMapperFacade().map(flight, Flight.class);
System.out.println("原对象:flight=" + JSON.toJSONString(flight));
System.out.println("新对象:flightCopy=" + JSON.toJSONString(flightCopy));
flightCopy.setFlightNo("MU5183");
flightCopy.getCabins().remove(0);
System.out.println("修改航变号和删除舱位:flightCopy=" + JSON.toJSONString(flightCopy)); System.out.println("原对象:flight=" + JSON.toJSONString(flight));
输出结果:
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(Cabin.class, CabinDesc.class).byDefault().register();
Cabin cabinY = new Cabin("Y", "8", 1200);
Cabin cabinF = new Cabin("C", "6", 1900);
Cabin cabinC = new Cabin("F", "A", 2400);
List
List
System.out.println("cabinList = " + JSON.toJSONString(cabinList));
System.out.println("cabinDescList = " + JSON.toJSONString(cabinDescList));
输出结果
继承接口CustomConverter类,覆盖实现convert方法
public static class StringToDateConverter extends CustomConverter<String, Date> {
private String dateFormat;
public StringToDateConverter(String dateFormat) {
this.dateFormat = dateFormat;
}
public Date convert(String s, Type<? extends Date> type, MappingContext mappingContext) {
try {
return DateUtils.parseDate(s, dateFormat);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.getConverterFactory()
.registerConverter(new StringToDateConverter("yyyy-MM-dd"));
mapperFactory.classMap(ArrayMapper.PersonNameList.class, Person.class)
.field("nameList[0]", "firstName").field("nameList[1]", "lastName")
.field("nameList[2]", "birthDate").register();
List
Person person = mapperFactory.getMapperFacade().map(new ArrayMapper.PersonNameList(nameParts), Person.class);
System.out.println("nameParts = " + JSON.toJSONString(nameParts));
System.out.println("person = " + JSON.toJSONStringWithDateFormat(person,"yyyy-MM-dd"));
输出结果:
每种方法先预热执行20次,而后再执行100000次获取每次执行的平均时间
官网:http://orika-mapper.github.io/orika-docs/intro.html
其他:https://www.jianshu.com/p/5daf68dc5758
http://tech.dianwoda.com/2017/11/04/gao-xing-neng-te
-xing-feng-fu-de-beanying-she-gong-ju-orika/