Dozer简单入门

maven引入依赖

 <dependency>
            <groupId>net.sf.dozer</groupId>
            <artifactId>dozer</artifactId>
            <version>5.2.1</version>
</dependency>

创建映射文件xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
      http://dozer.sourceforge.net/schema/beanmapping.xsd">
    <mapping>
        <class-a>handler.entity.Vo</class-a>
        <class-b>handler.entity.Po</class-b>
        <field>
            <a>ss</a>
            <b>id</b>
        </field>
        <field>
            <a>username</a>
            <b>name</b>
        </field>
        <field>
            <a>time</a>
            <b>localTime</b>
        </field>
        <field>
            <a>path1</a>
            <b>image1</b>
        </field>
        <field>
            <a>path2</a>
            <b>image2</b>
        </field>
        <field>
            <a>path3</a>
            <b>image3</b>
        </field>
    </mapping>
</mappings>

与spring整合

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
       default-autowire="byName" default-lazy-init="false">

    <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFactoryBean">
        <property name="mappingFiles">
            <list>
                <value>classpath*:dozer/dozerBeanMapping.xml</value>
            </list>
        </property>
    </bean>
</beans>

test测试

import handler.entity.Po;
import handler.entity.Vo;
import org.dozer.Mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author whh
 * @date 2019/3/15 15:22
 */
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:spring-dozer.xml"})
public class Test2 {

    @Autowired
    Mapper mapper;
    @Test
    public void ss() {
        Po po = new Po();
        po.setId(110L);
        po.setName("测试");
        po.setImage1("11");
        po.setImage2("22");
        po.setImage3("33");
        Vo map = mapper.map(po, Vo.class);
        System.out.println("map = " + map);
    }
}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值