这种方式也是日常使用的最多的,性能肯定是杠杠的,就是 *** 作起来有点麻烦。尤其是一大堆属性的 VO 对象转换为 DTO 对象时候。但其实也有一些快捷的 *** 作方式,比如你可以通过 Shift+Alt 选中所有属性,Shift+Tab 归并到一列,接下来在使用 Alt 选中这一列,批量 *** 作粘贴 userDTO.set 以及快捷键大写属性首字母,最后切换到结尾补充括号和分号,最终格式化一下就搞定了。
代码
@Component public class GetSetAssembler implements IAssembler2. json2Json 不推荐{ @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); userDTO.setUserId(var.getUserId()); userDTO.setUserNickName(var.getUserNickName()); userDTO.setCreateTime(var.getCreateTime()); return userDTO; } }
把对象转JSON串,再把JSON转另外一个对象 有点绕
@Component public class Json2JsonAssembler implements IAssembler3. Apache copyProperties{ @Override public UserDTO sourceToTarget(UserVO var) { String strJson = JSON.toJSonString(var); return JSON.parseObject(strJson, UserDTO.class); } }
Introspector 机制获取到类的属性来进行赋值 *** 作
@Component public class ApacheCopyPropertiesAssembler implements IAssembler4. Spring copyProperties{ @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); try { BeanUtils.copyProperties(userDTO, var); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return userDTO; } }
@Component public class SpringCopyPropertiesAssembler implements IAssembler{ @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtils.copyProperties(var, userDTO); return userDTO; } }
同样是反射的属性拷贝,Spring 提供的 copyProperties 要比 Apache 好用的多,只要你不用错,基本不会有啥问题。
5. Bean Mapping属性拷贝
@Component public class BeanMappingAssembler implements IAssembler6. Bean Mapping ASM{ @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtil.copyProperties(var, userDTO); return userDTO; } }
与普通的 Bean Mapping 相比,性能有所提升,可以使用。
@Component public class BeanMappingAssembler implements IAssemblerBeanCopier{ @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtil.copyProperties(var, userDTO); return userDTO; } }
整体性能很不错,使用也不复杂,可以使用
@Component public class BeanCopierAssembler implements IAssembler8. Orika{ @Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanCopier beanCopier = BeanCopier.create(var.getClass(), userDTO.getClass(), false); beanCopier.copy(var, userDTO, null); return userDTO; } }
测试性能不是太突出,如果使用的话需要把 MapperFactory 的构建优化成 Bean 对象
@Component public class OrikaAssembler implements IAssembler9. Dozer{ private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); static { mapperFactory.classMap(UserDTO.class, UserVO.class) .field("userId", "userId") // 字段不一致时可以指定 .byDefault() .register(); } @Override public UserDTO sourceToTarget(UserVO var) { return mapperFactory.getMapperFacade().map(var, UserDTO.class); } }
性能有点差,不建议使用
@Component public class DozerAssembler implements IAssembler10. ModelMapper{ private static DozerBeanMapper mapper = new DozerBeanMapper(); @Override public UserDTO sourceToTarget(UserVO var) { return mapper.map(var, UserDTO.class); } }
转换对象数量较少时性能不错,如果同时大批量转换对象,性能有所下降
@Component public class ModelMapperAssembler implements IAssembler11. JMapper{ private static ModelMapper modelMapper = new ModelMapper(); static { modelMapper.addMappings(new PropertyMap () { @Override protected void configure() { // 属性值不一样可以自己 *** 作 map().setUserId(source.getUserId()); } }); } @Override public UserDTO sourceToTarget(UserVO var) { return modelMapper.map(var, UserDTO.class); } }
JMapper12. MapStructjMapper = new JMapper<>(UserDTO.class, UserVO.class, new JMapperAPI() .add(JMapperAPI.mappedClass(UserDTO.class) .add(JMapperAPI.attribute("userId") .value("userId")) .add(JMapperAPI.attribute("userNickName") .value("userNickName")) .add(JMapperAPI.attribute("createTime") .value("createTime")) ));
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, unmappedSourcePolicy = ReportingPolicy.IGNORE) public interface UserDTOMapping extends IMapping{ IMapping INSTANCE = Mappers.getMapper(UserDTOMapping.class); @Mapping(target = "userId", source = "userId") @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss") @Override UserDTO sourceToTarget(UserVO var1); @Mapping(target = "userId", source = "userId") @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss") @Override UserVO targetToSource(UserDTO var1); }
速度很快,不需要到运行期处理,结合到框架中使用方便
其实对象属性转换的 *** 作无非是基于反射、AOP、CGlib、ASM、Javassist 在编译时和运行期进行处理,再有好的思路就是在编译前生成出对应的get、set,就像手写出来的一样。所以我更推荐我喜欢的 MapStruct,这货用起来还是比较舒服的,一种是来自于功能上的拓展性,易用性和兼容性。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)