Java中ModelMapper 的高级使用

目录
  • ModelMapper高级使用
  • 使用实例
  • 实体类
  • ModelMapper配置类
  • ModelMapperService类
  • 测试类
  • 测试结果

ModelMapper 高级使用

  ModelMapper 是一个 Object To Object 的工具,类似于 MapStruct又不同于 MapStruct。主要原因是 ModelMapper 是利用反射的原理实现的 Object To Object。

  ModelMapper 官方API : http://modelmapper.org/user-manual/property-mapping/

使用实例

本实例实现了条件映射、嵌套映射(对象中有对象映射)、自定义属性映射、List 集合映射(对象中有集合映射)、Map集合映射(对象中有集合映射)、忽略映射,默认值设置(ModelMapper 的默认值设置时一不小心就会入坑,如果直接设置默认值,当再赋值转换时,默认值会覆盖赋值的值,所以设置默认值需要结合条件映射)等。
验证了对象属性为集合,集合中还有集合能够使用 ModelMapper 进行转换。不足点是这个实例中没有验证有继承关系时的映射(使用 modelMapper.includeBase(父类1, 父类2) 方法),多个属性映射为一个属性或一个属性映射为多个属性(使用 PropertyMap 转换器)。

  • 使用条件映射设置默认值。当 age/createTime 没有值时设置默认值为18/当前时间,有值时不设置默认值;
  • 嵌套映射,自定义属性映射。Source 的 sourceSon 成员变量 映射到 Destination 的 destinationSon 成员变量;
  • List集合映射。Source 的 listSon 成员变量 映射到 Destination 的 sonList 成员变量;
  • Map集合映射。Source 的 mapSon 成员变量 映射到 Destination 的 sonMap 成员变量;
  • 忽略映射。忽略 Destination 的 excessParam 成员变量,如果不忽略将验证不过,报 org.modelmapper.MappingException: ModelMapper mapping errors;

实体类

(1)BaseClass

@Getter
@Setter
public class BaseClass {

    private String id;
    private String name;

    public BaseClass() {

    }

    public BaseClass(String id, String name) {
        this.id = id;
        this.name = name;
    }
}

(2)SouSubClass

@Getter
@Setter
public class SouSubClass {

    private String sonId;
    private String sonName;
    private List<BaseClass> grandSons;

    public SouSubClass() {

    }

    public SouSubClass(String sonId, String sonName) {
        this.sonId = sonId;
        this.sonName = sonName;
    }
}

(3)DestSubClass

@Getter
@Setter
public class DestSubClass {

    private String dsonId;
    private String sonName;
    private String excessParam;
    private List<BaseClass> grandSons;

    public DestSubClass(){

    }

    public DestSubClass(String dsonId, String sonName) {
        this.dsonId = dsonId;
        this.sonName = sonName;
    }
}

(4)Source

@Getter
@Setter
public class Source {
    private String id;
    private String name;
    private Integer age;
    private SouSubClass sourceSon;
    private List<SouSubClass> listSon;
    private Map<Integer, SouSubClass> mapSon;
    private Date createTime;

    public Source() {

    }

    public Source(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public Source(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

(5)Destination

@Getter
@Setter
public class Destination {
    private Long id;
    private String name;
    private Integer age;
    private DestSubClass destinationSon;
    private List<DestSubClass> sonList;
    private Map<Integer, DestSubClass> sonMap;
    private String excessParam;
    private Date createTime;

    public Destination() {

    }

    public Destination(Long id, String name) {
        this.id = id;
        this.name = name;
    }
}

ModelMapper 配置类

/**
 * 描述:ModelMapper 配置
 */
@Configuration
public class ModelMapperConfig {

    @Bean
    @Scope("singleton")
    public ModelMapper getModelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        //默认为standard模式,设置为strict模式
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        // 类型映射代码
        sourceSonToDestinationSon(modelMapper);
        sourceToDestination(modelMapper);

        //验证映射
        modelMapper.validate();

        // 配置代码
        return modelMapper;
    }

    /**
     * 描述:声明 Source 类转 Destination 类的 Mapper
     * @param modelMapper
     * @Date  2019/05/09
     */
    private void sourceSonToDestinationSon(ModelMapper modelMapper) {
        modelMapper.createTypeMap(SouSubClass.class, DestSubClass.class)
                .addMapping(SouSubClass::getSonId, DestSubClass::setDsonId)
                .addMapping(SouSubClass::getSonName, DestSubClass::setSonName)
                .addMappings(mapper -> mapper.skip(DestSubClass::setExcessParam));
    }

    private void sourceToDestination(ModelMapper modelMapper) {
        modelMapper.createTypeMap(Source.class, Destination.class)
                .addMappings(mapper -> mapper.using((Converter<Integer, Integer>) context -> {
                        if (context.getSource() == null) {
                            return 18;
                        }
                        return context.getSource();
                    }).map(Source::getAge, Destination::setAge))
                .addMappings(mapper -> mapper.using((Converter<Date, Date>) context -> {
                        if (context.getSource() == null) {
                            return new Date();
                        }
                        return context.getSource();
                    }).map(Source::getCreateTime, Destination::setCreateTime))
                .addMapping(Source::getSourceSon, Destination::setDestinationSon)
                .addMapping(Source::getListSon, Destination::setSonList)
                .addMapping(Source::getMapSon, Destination::setSonMap)
                .addMappings(mapper -> mapper.skip(Destination::setExcessParam));
    }
}

ModelMapper Service 类

public interface TestService {

    Destination testSourceToDestination(Source source);

    List<Destination> testSourceToDestinationList(List<Source> sources);
}
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public Destination testSourceToDestination(Source source) {
        Destination destination = modelMapper.map(source, Destination.class);

        return destination;  // a 处
    }

    @Override
    public List<Destination> testSourceToDestinationList(List<Source> sources) {
        Type type = new TypeToken<List<Destination>>(){}.getType();
        List<Destination> destinations = modelMapper.map(sources, type);

        return destinations; // b 处
    }
}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestServiceImplTest {

    @Autowired
    private TestService testService;

    private static Source source1 = new Source("a", "发生的", 24);
    private static Source source2 = new Source("b", "发生的");

    private static List<Source> sources = new ArrayList<>();

    static {
        List<BaseClass> baseClasses1 = new ArrayList<>();
        baseClasses1.add(new BaseClass("aa", "发生的111"));
        baseClasses1.add(new BaseClass("bb", "发生的222"));
        SouSubClass subClass1 = new SouSubClass("aaa", "发生的3333");
        subClass1.setGrandSons(baseClasses1);

        List<BaseClass> baseClasses2 = new ArrayList<>();
        baseClasses2.add(new BaseClass("cc", "国防观"));
        baseClasses2.add(new BaseClass("dd", "国防观"));
        SouSubClass subClass2 = new SouSubClass("ccc", "规范的大哥");
        subClass2.setGrandSons(baseClasses2);

        List<SouSubClass> sourceSonList = new ArrayList<>();
        sourceSonList.add(subClass1);
        sourceSonList.add(subClass2);

        Map<Integer, SouSubClass> sourceSonMap = new HashMap<>();
        sourceSonMap.put(1, subClass1);
        sourceSonMap.put(2, subClass2);

        source1.setCreateTime(new Date(System.currentTimeMillis()-98978609));
        source1.setSourceSon(subClass1);
        source1.setListSon(sourceSonList);
        source1.setMapSon(sourceSonMap);

        source2.setSourceSon(subClass1);
        source2.setListSon(sourceSonList);
        source2.setMapSon(sourceSonMap);

        sources.add(source1);
        sources.add(source2);
    }

    @Test
    public void testSourceToDestination() {
        testService.testSourceToDestination(source1);
        testService.testSourceToDestination(source2);
    }

    @Test
    public void testSourceToDestinationList() {
        testService.testSourceToDestinationList(sources);
    }
}

测试结果

在 ab 两处打上断点,查看变量转换前后的值,证实转换成功。

到此这篇关于Java中ModelMapper 的高级使用的文章就介绍到这了,更多相关Java ModelMapper内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 浅谈javaSE 面向对象(Object类toString)

    每一个对象,都有一个在内存中的地址哈希值,这个哈希值是十六进制的 调用Object对象的hashCode()方法,返回这个对象的哈希值 调用Integer.toHexString()方法,转换十六进制 调用Object对象的toString()方法,得到:类名@哈希值 通常我们会复写toString()方法,因为默认的没有太大意义 实现原理是类的反射 当我们创建一个对象,会在硬盘上生成xxx.class的文件,jdk定义了Class类来描述这些class文件 调用Object对象的getClas

  • ObjectInputStream 和 ObjectOutputStream 介绍_动力节点Java学院整理

    ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操作支持. 创建"文件输出流"对应的ObjectOutputStream对象,该ObjectOutputStream对象能提供对"基本数据或对象"的持久存储:当我们需要读取这些存储的"基本数据或对象"时,可以创建"文件输入流"对应的ObjectInputStream,进而读取出这些"基本数据或对象&quo

  • 一篇文章带你了解java Object根类中关于toString,equals的方法

    目录 toString: 代码案例: equals: 代码案例: 总结 toString: 概念:拼接对象的地址值:toString()方法用于返回表示对象值的字符串(返回的是String对象). 快捷写法:Alt+Insert,直接会显示toString的方法,选取需要返回的对象就行. 代码案例: 定义一个person类,属性如下: (1)身份证号,性别,姓名,年龄,户籍,出生日期(Data类型,需要引用java.uitl.Data)功能: (2)自我介绍:介绍格式:(toString) 身份

  • Java Object toString方法原理解析

    在Java中,所有的对象都是继承自Object,自然继承了toString方法,在当使用System,out.println()里面为一个对象的引用时,自动调用toString方法将对象打印出来.如果重写了tostring方法则调用重写的toString 方法. 先看下面一段代码 public class ToStringTest { static int i = 1; public static void main(String[] args) { System.out.println("lo

  • Java中ModelMapper 的高级使用

    目录 ModelMapper高级使用 使用实例 实体类 ModelMapper配置类 ModelMapperService类 测试类 测试结果 ModelMapper 高级使用   ModelMapper 是一个 Object To Object 的工具,类似于 MapStruct又不同于 MapStruct.主要原因是 ModelMapper 是利用反射的原理实现的 Object To Object.   ModelMapper 官方API : http://modelmapper.org/u

  • java中RabbitMQ高级应用

    目录 1.消息可靠性投递 1.1.确认模式 1.2.退回模式 1.3.确认机制 2.消费端限流 3.消息过期时间 4.死信队列 4.1.死信概念 4.2.延迟队列 1.消息可靠性投递  在使用 RabbitMQ 的时候,生产者在进行消息投递的时候如果想知道消息是否成功的投递到对应的交换机和队列中,有两种方式可以用来控制消息投递的可靠性模式 .  由上图的整个消息的投递过程来看,生产者的消息进入到中间件中会首先到达交换机,然后再从交换机传递到队列中去,也就是分为两步走战略.那么消息的丢失情况也就是

  • JAVA中正则表达式小总结(整理)

    ]js正则表达式基本语法(精粹):  http://www.jb51.net/article/72044.htm 许多语言,包括Perl.PHP.Python.JavaScript和JScript,都支持用正则表达式处理文本,一些文本编辑器用正则表达式实现高级"搜索-替换"功能.所以JAVA语言也不例外.正则表达式已经超出了某种语言或某个系统的局限,成为被人们广为使用的工具,我们完全可以用它来解决实际开发中碰到的一些实际的问题. 在JDK1.3及之前的JDK版本中并没有包含正则表达式的

  • Java中Spring获取bean方法小结

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢? Bean工厂(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机制.BeanFactory使管理不同类型的Java对象成为可能,应用上下文(com.springframework.context.ApplicationContext)建立在BeanFactory基础之上,提供

  • 详谈Java中的Object、T(泛型)、?区别

    因为最近重新看了泛型,又看了些反射,导致我对Object.T(以下代指泛型).?产生了疑惑. 我们先来试着理解一下Object类,学习Java的应该都知道Object是所有类的父类,注意:那么这就意味着它的范围非常广!首先记住这点,如果你的参数类型时Object,那么的参数类型将非常广! <Thinking in Java>中说很多原因促成了泛型的出现,最引人注目的一个原因就是为了创造容器类.这个要怎么来理解呢?我的理解是,可以抛开这个为了创造容器类这个,而是回到泛型的目的是限定某种类型上来.

  • Java中的动态和静态编译实例详解

    Java中的动态和静态编译实例详解 首先,我们来说说动态和静态编译的问题. Q: java和javascript有什么区别?    总结了一下:有以下几点吧: 1.首先从运行环境来说java代码是在JVM上编译成class文件,而javascript则直接在浏览器上加载运行. 2.由第一点可看出,java代码需要编译,而javascript不需要编译. 3.从语言性质来说,java是一种高级编程语言,对变量检查要求严格,javascript只是一个简单的解释性的脚本语言,对变量检查及要求很弱.

  • 全面了解Java中的内部类和匿名类

    Java内部类(Inner Class),类似的概念在C++里也有,那就是嵌套类(Nested Class),乍看上去内部类似乎有些多余,它的用处对于初学者来说可能并不是那么显著,但是随着对它的深入了解,你会发现Java的设计者在内部类身上的确是用心良苦.学会使用内部类,是掌握Java高级编程的一部分,它可以让你更优雅地设计你的程序结构.下面从以下几个方面来介绍: 第一次见面 public interface Contents { int value(); } public interface

  • 介绍Python中的一些高级编程技巧

     正文: 本文展示一些高级的Python设计结构和它们的使用方法.在日常工作中,你可以根据需要选择合适的数据结构,例如对快速查找性的要求.对数据一致性的要求或是对索引的要求等,同时也可以将各种数据结构合适地结合在一起,从而生成具有逻辑性并易于理解的数据模型.Python的数据结构从句法上来看非常直观,并且提供了大量的可选操作.这篇指南尝试将大部分常用的数据结构知识放到一起,并且提供对其最佳用法的探讨. 推导式(Comprehensions) 如果你已经使用了很长时间的Python,那么你至少应该

  • 谈谈Java中整数类型(short int long)的存储方式

    在java中的整数类型有四种,分别是 byte  short int long 其中byte只有一个字节 0或1,在此不详细讲解. 其他的三种类型如下: 1. 基本类型:short 二进制位数:16 包装类:java.lang.Short 最小值:Short.MIN_VALUE=-32768 (-2的15此方) 最大值:Short.MAX_VALUE=32767 (2的15次方-1) 2. 基本类型:int 二进制位数:32 包装类:java.lang.Integer 最小值:Integer.M

  • 深入理解Java中的弱引用

    不久之前,我面试了一些求职Java高级开发工程师的应聘者.我常常会面试他们说,"你能给我介绍一些Java中得弱引用吗?",如果面试者这样说,"嗯,是不是垃圾回收有关的?",我就会基本满意了,我并不期待回答是一篇诘究本末的论文描述. 然而事与愿违,我很吃惊的发现,在将近20多个有着平均5年开发经验和高学历背景的应聘者中,居然只有两个人知道弱引用的存在,但是在这两个人之中只有一个人真正了解这方面的知识.在面试过程中,我还尝试提示一些东西,来看看有没有人突然说一声&quo

随机推荐