DozerMapper:对象转换器使用教程

dozer GitHub地址:https://github.com/DozerMapper/dozer

官方wiki:Introduction · GitBook

第一步:导包

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

第二步:配置转换器

DozerConfig.java文件

package com.ymf.invoice.config;


import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class DozerConfig {

    @Bean
    public Mapper dozerBeanMapper() {
        List<String> mappingFiles = Arrays.asList(
                "dozer-configration-mapping.xml"
        );
        return DozerBeanMapperBuilder.create()
                .withMappingFiles(mappingFiles)
                .build();
    }
}

dozer-configration-mapping.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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">

</mappings>

第三步:简单测试

import com.github.dozermapper.core.Mapper;

@Autowired
private Mapper mapper;


InvoiceTaskDto invoiceTaskDto = mapper.map(invoiceTask, InvoiceTaskDto.class);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DozerMapper是一个Java类库,用于对象之间的转换。它提供了一个简单的API来实现对象之间的射。可以通过以下步骤使用DozerMapper: 1. 首先,需要创建一个DozerMapper对象: ```java Mapper mapper = new DozerBeanMapper(); ``` 2. 使用DozerMapper进行对象转换: ```java DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class); ``` 这将把sourceObject对象的属性值映射到DestinationObject类的对应属性上。 3. 如果需要特殊字段转换或自定义映射规则,可以使用mapping文件来配置。可以通过以下步骤实现: a. 创建一个包含映射规则的mapping文件,比如"dozer-mapping.xml"。 b. 将mapping文件添加到DozerMapper中: ```java List myMappingFiles = new ArrayList(); myMappingFiles.add("dozer-mapping.xml"); mapper.setMappingFiles(myMappingFiles); ``` 这样,DozerMapper会根据mapping文件中定义的规则进行转换。 4. 如果需要使用自定义转换器,需要实现org.dozer.CustomConverter接口,并在mapping文件中定义转换器的配置。 综上所述,使用DozerMapper进行对象之间的转换,你需要创建DozerMapper对象,使用map方法进行转换,可以配置mapping文件来定义特殊字段转换规则,也可以实现自定义转换器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Dozer 使用小结](https://blog.csdn.net/weixin_34306446/article/details/86353213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO之dozer mapper使用](https://blog.csdn.net/ZYC88888/article/details/83505251)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值