zoukankan      html  css  js  c++  java
  • dozer的使用

    <dependency>
         <groupId>com.github.dozermapper</groupId>
         <artifactId>dozer-core</artifactId>
         <version>6.5.2</version>
    </dependency>

    pom文件  导入dozer相关的 依赖
    在resource下 创建 dozer对应的 xml 配置文件

    <mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
        <mapping>
            <class-a>com.aila.vo.OrderVo</class-a>
            <class-b>com.aila.bo.OrderBo</class-b>
            <field>
                <a>orderVoExtList</a>
                <b>orderBoExtList</b>
            </field>
        </mapping>
    </mappings>

    实体类相关代码

    package com.aila.vo;
    
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    import java.util.List;
    
    /**
     * @author chenzhichao
     * @date 2021/7/7 9:33
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class OrderVo {
        private String name;
        private List<OrderVoExt> orderVoExtList;
    }
    package com.aila.vo;
    
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    import java.util.List;
    
    /**
     * @author chenzhichao
     * @date 2021/7/7 9:33
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class OrderVoExt {
        private Long skuId;
        private List<Long> numberList;
        private OrderVoExtComplex voComplex;
    }
    package com.aila.vo;
    
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    /**
     * @author chenzhichao
     * @date 2021/7/7 9:42
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class OrderVoExtComplex {
        private String value;
    }
    package com.aila.bo;
    
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    import java.util.List;
    
    /**
     * @author chenzhichao
     * @date 2021/7/7 9:33
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class OrderBo {
        private String name;
        private List<OrderBoExt> orderBoExtList;
    }
    package com.aila.bo;
    
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    import java.util.List;
    
    /**
     * @author chenzhichao
     * @date 2021/7/7 9:33
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class OrderBoExt {
        private Long skuId;
        private List<Long> numberList;
        private OrderBoExtComplex boComplex;
    }
    package com.aila.bo;
    
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    /**
     * @author chenzhichao
     * @date 2021/7/7 9:41
     */
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class OrderBoExtComplex {
        private String value;
    }

    测试类相关代码

    import com.aila.MyBootApplication;
    import com.aila.bo.OrderBo;
    import com.aila.vo.OrderVo;
    import com.aila.vo.OrderVoExt;
    import com.aila.vo.OrderVoExtComplex;
    import com.github.dozermapper.core.Mapper;
    import com.google.gson.Gson;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    import java.util.Arrays;
    
    /**
     * @author chenzhichao
     * @date 2021/6/18 15:09
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MyBootApplication.class)
    public class MyTestYy {
        /*@Resource
        private RedissonClient redisson;*/
        @Resource
        private Mapper mapper;
        @Resource
        private Gson gson;
        @Test
        public void test(){
            /*RKeys keys = redisson.getKeys();
            System.out.println(keys);
            String key="123";
            MyWork002 work002 = new MyWork002();
            MyTask002 myTask002 = new MyTask002("A", key, redisson,work002);
            MyTask002 myTask003 = new MyTask002("B", key, redisson,work002);
            new Thread(myTask002).start();
            new Thread(myTask003).start();
            System.out.println("123");*/
        }
        @Test
        public void dozerTest(){
    
            OrderVo vo = new OrderVo();
            vo.setName("testName");
            OrderVoExt voExt = new OrderVoExt();
            voExt.setSkuId(3L).setNumberList(Arrays.asList(1L,2L,3L));
            OrderVoExtComplex extComplex = new OrderVoExtComplex();
            extComplex.setValue("testValues");
            voExt.setVoComplex(extComplex);
            vo.setOrderVoExtList(Arrays.asList(voExt));
            OrderBo bo = mapper.map(vo, OrderBo.class);
            String json = gson.toJson(bo);
            System.out.println(json);
    
        }
    
    }

    结果:

    结论1:如果两个对象之间的 属性名称不一致 不能直接 转换 ,但是可以通过xml来指定需要转换的对象
    但是不清楚 如果指定的 两个属性 都是复杂属性是否也能通过xml 进行转换
    继续测试 
    向xml中添加 ext的xml配置

    <mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
        <mapping>
            <class-a>com.aila.vo.OrderVo</class-a>
            <class-b>com.aila.bo.OrderBo</class-b>
            <field>
                <a>orderVoExtList</a>
                <b>orderBoExtList</b>
            </field>
        </mapping>
        <mapping>
            <class-a>com.aila.vo.OrderVoExt</class-a>
            <class-b>com.aila.bo.OrderBoExt</class-b>
            <field>
                <a>voComplex</a>
                <b>boComplex</b>
            </field>
        </mapping>
    </mappings>

    执行代码结果:

    结论 :如果 转化的 成员属性是复杂对象   则需要 在配置  这个复杂对象的 转化对象  否则无法 转化

    最后附上配置代码

    package com.aila.config;
    
    import com.github.dozermapper.core.DozerBeanMapperBuilder;
    import com.github.dozermapper.core.Mapper;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.Resource;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     * dozer 配置
     * @author czc
     */
    @Configuration
    public class DozerConfig {
    
        @Bean
        public Mapper getDozerMapper(@Value("classpath*:mappings/*.xml") Resource[] resources) {
            List<String> fileNameList = Arrays.stream(resources).map(resource -> "mappings/"+resource.getFilename()).collect(Collectors.toList());
            Mapper mapper = DozerBeanMapperBuilder.create().withMappingFiles(fileNameList).build();
            return mapper;
        }
    }
  • 相关阅读:
    ExcelUtil工具类-1
    图论-floyd算法-python实现
    流式编程一些简单的例子
    利用Stream实现简单的等差数列求和
    根据主机IP列表自动部署指定规模的redis cluster
    使用cgroup限制磁盘io读写速率
    consul kv导出和导入(备份)
    mysql8.0 运维相关新特性(未完待续)
    Rider写ASP.NET MVC调试报错
    网络协议知识串讲:搭建一个网络试验环境:授人以鱼不如授人以渔
  • 原文地址:https://www.cnblogs.com/kyousuke/p/14980188.html
Copyright © 2011-2022 走看看