dozer入门

最近使用dozer,用于Dto转换,如下:

1、依赖


    net.sf.dozer
    dozer
    5.5.1


    net.sf.dozer
    dozer-spring
    5.5.1
 
  
2、spring配置。不知道为什么“classpath*:”不行??
 
  
 
  
@Bean
public Mapper mapper() {
    DozerBeanMapper mapper = new DozerBeanMapper();
    mapper.setMappingFiles(Lists.newArrayList("dozer/dozer-mapping.xml"));
    return mapper;
}

3、dozer map ,source 为null的处理,添加DozerUtils.java。
 
  
private static  T map(Object source, Class destinationClass, String mapId) throws MappingException {
    if (source == null) return null;
    return BeanApiUtils.getApplicationContext().getBean(Mapper.class).map(source, destinationClass, mapId);
}

public static  T map(Object source, Class destinationClass) throws MappingException {
    if (source == null) return null;
    return BeanApiUtils.getApplicationContext().getBean(Mapper.class).map(source, destinationClass);
}

public static void map(Object source, Object destination) throws MappingException {
    if (source == null) return;
    BeanApiUtils.getApplicationContext().getBean(Mapper.class).map(source, destination);
}

4、List to List转换,网上看了一堆资料,也没搞明白,还是debug源码,终于搞定了!!!
 
  
map-id="category2">
    java.util.List
    java.util.List
    
        this
        this
        type2 class
    
根据官网例子,配置如上mapping,转换List到List
第一个坑,List的成员名称(field)彻底搞不懂,List有这个吗,还是debug,看到fieldMapping中name为“this”
第二个坑,dest type 竟然不是,而是

5、总结
dozer的简单使用,未深入了解。
有看到说某些高要求场景性能不够好,待测试。
debug源码解决问题,很有成就感。

你可能感兴趣的:(dozer)