相关文章推荐
挂过科的大蒜  ·  Python Pandas ...·  2 年前    · 
任性的小熊猫  ·  利用 Blob 处理 node ...·  2 年前    · 
package




    
 com.nebula.module.organization.utils.dozer;
import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 * Function:DTO/VO/DO等之间的转换
 * Date: 2018/9/14 15:40
public class DozerUtils {
     * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
    private static DozerBeanMapper dozer;
    static {
        if (dozer == null) {
            dozer = new DozerBeanMapper();
            List<String> mappingFileUrls = Lists.newArrayList("dozer-config.xml");
            dozer.setMappingFiles(mappingFileUrls);
     * @param source           源对象
     * @param destinationClass 目标对象
     * @return
     * @title: map
     * @description: 单个对象相互转换
    public static <T> T map(Object source, Class<T> destinationClass) {
        return dozer.map(source, destinationClass);
     * @param sourceList
     * @param destinationClass
     * @return
     * @title mapList
     * @description 集合对象相互转换
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        List<T> destinationList = new ArrayList<T>();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        return destinationList;
     * 基于Dozer将对象A的值拷贝到对象B中
     * @param source 需要转换的对象
     * @param toObj  转换后对象类型
    public static void copy(Object source, Object toObj) {
        if (null != source) {
            dozer.map(source, toObj);

3.dozer-config.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">
    <configuration>	
    	//customer节点可省略,后面的converter文件也可省略
        <custom-converters> <!-- these are always bi-directional -->
            <converter type="com.lhc.util.dozer.convert.LocalDateTimeToDateDozerConverter">
                <class-a>java.time.LocalDateTime</class-a>
                <class-b>java.util.Date</class-b>
            </converter>
            <converter type="com.lhc.util.dozer.convert.StringToDateConverter">
                <class-a>java.lang.String</class-a>
                <class-b>java.util.Date</class-b>
            </converter>
            <converter type="com.lhc.util.dozer.convert.LocalDateTimeToLocalDateTimeConverter">
                <class-a>java.time.LocalDateTime</class-a>
                <class-b>java.time.LocalDateTime</class-b>
            </converter>
            <converter type="com.lhc.util.dozer.convert.LocalDateToLocalDateConverter">
                <class-a>java.time.LocalDate</class-a>
                <class-b>java.time.LocalDate</class-b>
            </converter>
            <converter type="com.lhc.util.dozer.convert.LocalTimeToLocalTimeConverter">
                <class-a>java.time.LocalTime</class-a>
                <class-b>java.time.LocalTime</class-b>
            </converter>
        </custom-converters>
    </configuration>
</mappings>

4. 五个Converter

dozer-date.xml custom-converters有几个convert就有几个自定义的转换对象

package com.nebula.module.organization.utils.dozer.converter;
import org.dozer.DozerConverter;
import java.time.LocalDateTime;
import java.time.ZoneId;




    

import java.util.Date;
public class LocalDateTimeToDateDozerConverter extends DozerConverter<LocalDateTime, Date> {
    public LocalDateTimeToDateDozerConverter() {
        super(LocalDateTime.class, Date.class);
    @Override
    public LocalDateTime convertFrom(Date source, LocalDateTime destination) {
        return LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
    @Override
    public Date convertTo(LocalDateTime source, Date destination) {
        return Date.from(source.atZone(ZoneId.systemDefault()).toInstant());
package com.nebula.module.organization.utils.dozer.converter;
import org.apache.commons.lang3.StringUtils;
import org.dozer.DozerConverter;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Date;
public class StringToDateConverter extends DozerConverter<String, Date> {
    public StringToDateConverter() {
        super(String.class, Date.class);
    @Override
    public String convertFrom(Date source, String destination) {
        if (source == null) {
            return StringUtils.EMPTY;
        DateTime dateTime = new DateTime(source);
        destination = dateTime.toString("yyyy-MM-dd HH:mm:ss");
        return destination;
    @Override
    public Date convertTo(String source, Date destination) {
        DateTimeFormatter dateTimeFormatter;
        if (StringUtils.isBlank(source)) {
            return null;
        if (10 == source.length()) {
            dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        } else if (14 == source.length()) {
            dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmss");
        } else {
            dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = dateTimeFormatter.parseDateTime(source);
        destination = dateTime.toDate();
        return destination;
public class LocalDateTimeToLocalDateTimeConverter extends DozerConverter<LocalDateTime, LocalDateTime> {
    public LocalDateTimeToLocalDateTimeConverter() {
        super(LocalDateTime.class, LocalDateTime.class);
    @Override
    public LocalDateTime convertFrom(LocalDateTime source, LocalDateTime destination) {
        return source;
    @Override
    public LocalDateTime convertTo(LocalDateTime source, LocalDateTime destination) {
        return source;
public class LocalDateToLocalDateConverter extends DozerConverter<LocalDate, LocalDate> {
    public LocalDateToLocalDateConverter() {
        super(LocalDate.class, LocalDate.class);
    @Override
    public LocalDate convertFrom(LocalDate source, LocalDate destination) {
        return source;
    @Override
    public LocalDate convertTo(LocalDate source, LocalDate destination) {
        return source;
public class LocalTimeToLocalTimeConverter extends DozerConverter<LocalTime, LocalTime> {
    public LocalTimeToLocalTimeConverter() {
        super(LocalTime.class, LocalTime.class);
    @Override
    public LocalTime convertFrom(LocalTime source, LocalTime destination) {
        return source;
    @Override
    public LocalTime convertTo(LocalTime source, LocalTime destination) {
        return source;

二.MapStruct

1、pom加入依赖
        <!--mapstruct-->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.3.0.Final</version>
        </dependency>

2、mapstruct接口

import org.mapstruct.Mapper;
@Mapper
public interface UserMapStruct {
	// ClassLoader 加载方式
	UserMapStruct INSTANCE = Mappers.getMapper(UserMapStruct .class);
    UserEntity userAddDTO2UserEntity(UserAddDTO userAddDTO);
    UserAddVO  UserEntity2UserAddVO (UserEntity userEntity );

3、service层转换

UserEntity userEntity = UserMapStruct.INSTANCE.userAddDTO2UserEntity(userAddDto);
POSTED BYMARIUSZ WYSZOMIERSKI
In multi-tier applications there is often a need for mapping between two data models. In this article you can read about comparing two mapping libraries:
Dozer (http://dozer.sourceforge.net/...
                                    VO (view object/value object)表示层对象
1、前端展示的数据,在接口数据返回给前端的时候需要转成VO
2、个人理解使用场景,接口层服务中,将DTO转成VO,返回给前台
B0(bussines object)业务层对象
1、主要在服务内部使用的业务对象
2、可以包含多个对象,可以用于对象的聚合操作
3、个人理解使用场景,在服务层服务中,由DTO转成BO然后进行业务处理后,转成DTO返回到接口层
PO(persistent object)持久对象
1、出现位置为数据库数据,用来存储数
                                    mapstruct是一种实体类映射框架,能够通过Java注解将一个实体类的属性安全地赋值给另一个实体类。有了mapstruct,只需要定义一个映射器接口,声明需要映射的方法,在编译过程中,mapstruct会自动生成该接口的实现类,实现将源对象映射到目标对象的效果。.........
为哈么,你的代码也就仅仅是能用而已?
没有技术深度、短缺知识储备、匮乏经验积累的前提下,怎么写代码?百度呀,遇到问题这搜一点,那查一块,不管它是什么原理还是适合哪种场景,先粘贴到自己的工程里,看,能跑了,能跑就行。那这样的代码也就仅仅是能用程度的交付,根本没有一定的质量保证,也更别提数据结构、算法逻辑和设计模式了,那看的编程资料刷的LeetCode,全歇菜了。
当你感觉看了很多资料又不会用的时候,会说什么,真卷,都学到这样了。但其实我并不觉对技术的深度挖掘、梳理全套的知识体系,一点点耕耘一点点收
                                    在Dozer中,我们能够在字段映射期间提示提示中的接口 . 我们如何在MapStruct中实现相同的目标?我无法在这里输入确切的代码 . 但是,它与下面类似 . 我们这里有一个Domain类示例:Class A extends C{...};其中,B是抽象类 .  C是一个包含我们必须映射的List项的类 .类似的是DTO端的类和接口的结构 . 因此,Dozer中的映射如下:Domain.ADTO...
                                    DozerJava Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象dozer是用来对两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的调用set和get方法了。dozer其实是对我们熟知的beanutils的封装。
                                    dozer是一个很方便的bean映射工具包,可以很轻松地做到两个bean对象的属性值复制,但是dozer包在2014年的时候停止更新了,而jdk1.8也是2014年发布的,所以对于java8中的新日期的映射关系并没有在dozer中配置,所以在项目中jdk使用了1.8,在使用了LocalDateTime类型设置日期的时候,即使两个bean对象都是相同的LocalDateTime,使用dozer的api,即DozerBeanMapper.map()方法进行日期类映射的时候,是会报错的:jav...
对于分布式系统,需要在不同系统之间传递与转换对象。因为我们不希望外部公开内部域对象,也不允许外部域对象渗入系统。传统上,数据对象之间的映射通过手工编码(getter/setter)的方式实现,或对象组装器(或转换器)来解决。我们可能会开发某种自定义映射框架来满足我们的映射转换需求,但这一切都显得不够灵巧。
Dozer
DozerJava Bean 到 Java Bean 映射器,它以...
随着软件架构分层越来越多,那么各个层次之间的数据模型就要面临着相互转换的问题,典型的就是我们可以在代码中见到各种O,如DO、DTO、VO等。
如在数据存储层,我们使用DO来抽象一个业务实体;在业务逻辑层,我们使用DTO来表示数据传输对象;到了展示层,我们又把对象封装成VO来与前端进行交互。
先来看一张图
想必大家伙原来都写过类似格式的代码,额···看着就不说了,冗长而繁琐,于是MapStruct这一类框架应运而生 包括:
Spring BeanUtils
Cglib 
对象映射大体分为两种:
运行期:反射调用set/get 或者是直接对成员变量赋值 。 * 该方式通过invoke执行赋值,实现时一般会采用beanutil, Javassist等开源库。这类的代表:Dozer,ModelMapper
编译期:动态生成set/get代码的class文件 ,在运行时直接调用该class文件。* 该方式实际上仍会存在set/get...
                                    我有一个自定义转换器的推土机映射:com.xyz.Customercom.xyz.CustomerDAOcustomerNamecustomerName和转换器:public class DozerEmptyString2NullConverter extends DozerConverter {public DozerEmptyString2NullConverter() {super(Stri...