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);

    }

}

结果:

dozer的使用

结论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>

执行代码结果:

dozer的使用

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

上一篇:SpringBoot-数据对象


下一篇:实体类到底如何定义,怎么使用(浅析VO、BO、PO、DTO)