对象转换之modelmapper

1. 官网地址:http://modelmapper.org

源码地址:GitHub - modelmapper/modelmapper: Intelligent object mapping

2.实现原理:

主要是基于匹配策略进行属性的转化,目前支持三种策略:

2.1 Standard(默认标准规则)

2.2 Loose 松散型

2.3 Strict 严格匹配

3.实战:

这里我们以springboot集成案例演示,

方式一:Spring Boot Devtools+modelmapper

配置文件增加(需要集成springboot devtools)

    restart.include.modelmapper=/modelmapper-(.*).jar

方式二: maven包

java 复制代码
<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>3.1.1</version>
</dependency>

3.1 属性映射

3.2 类型映射继承

3.3 校验

3.4 配置

3.5 转化映射

3.6 Provider

3.7 通用

附demo:

java 复制代码
  public class Game {

    Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}


}
java 复制代码
public class Address {
    String street;
    String city;
    String address;
    public Address() {
    }

    public Address(String street, String city, String address) {
        this.street = street;
        this.city = city;
        this.address = address;
    }

    public Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

  public class Game {

    Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
java 复制代码
public class GameDTO {


    String street;
    String city;

    String address;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("GameDTO{");
        sb.append("street='").append(street).append('\'');
        sb.append(", city='").append(city).append('\'');
        sb.append(", address='").append(address).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
java 复制代码
import org.junit.jupiter.api.Test;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.TypeMap;
import org.modelmapper.convention.MatchingStrategies;

public class MainTest {

    @Test
    public  void testStrage() {
        ModelMapper modelMapper = new ModelMapper();
        PropertyMap<Game, GameDTO> personMap = new PropertyMap<Game, GameDTO>() {
            protected void configure() {
                map().setStreet(source.getAddress().getStreet());
                map(source.getAddress().city, destination.city);
            }
        };
        modelMapper.addMappings(personMap);
        //
        Game game=new Game();
        Address address=new Address();
            address.setStreet("street");
            address.setCity("city");
            game.setAddress(address);
        GameDTO gameDTO = modelMapper.map(game, GameDTO.class);
        System.out.println("PropertyMap="+gameDTO);
        //设置策略
        ModelMapper modelMapper1 = new ModelMapper();
        modelMapper1.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);
        GameDTO gameDTO1 = modelMapper1.map(game, GameDTO.class);
        System.out.println("STANDARD="+gameDTO1);
        //设置策略
        ModelMapper modelMapper2 = new ModelMapper();
        modelMapper2.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        GameDTO gameDTO2 = modelMapper2.map(game, GameDTO.class);
        System.out.println("STRICT="+gameDTO2);
         //设置策略
        ModelMapper modelMapper3 = new ModelMapper();
        modelMapper3.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
        GameDTO gameDTO3 = modelMapper3.map(game, GameDTO.class);
        System.out.println("LOOSE="+gameDTO3);
    }

    //测试深度映射
    @Test
    public  void testDeepMapping() {
        ModelMapper modelMapper = new ModelMapper();
        TypeMap<Game, GameDTO> typeMap = modelMapper.createTypeMap(Game.class, GameDTO.class);
        typeMap.addMappings(
            mapper -> mapper.map(src ->src.getAddress().getAddress(),GameDTO::setAddress)
        );
        Game game = new Game();
        game.setAddress(new Address("1", "John","address"));
        GameDTO gameDTO = modelMapper.map(game, GameDTO.class);
        System.out.println("dto:"+gameDTO);

    }
    //测试 skip
    @Test
    public  void testSkipMapping() {
        ModelMapper modelMapper = new ModelMapper();
        TypeMap<Game, GameDTO> typeMap = modelMapper.createTypeMap(Game.class, GameDTO.class);
        typeMap.addMappings(
                mapper -> mapper.skip(GameDTO::setAddress)
        );
        Game game = new Game();
        game.setAddress(new Address("1", "John","address"));
        GameDTO gameDTO = modelMapper.map(game, GameDTO.class);
        System.out.println("dto:"+gameDTO);

    }
    //测试 skip null 属性
    @Test
    public  void testSkipNullMapping() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setSkipNullEnabled(true);
        Game game = new Game();
        //Address 的address属性为空,不copy
        game.setAddress(new Address("1", "John",""));
        GameDTO gameDTO = modelMapper.map(game, GameDTO.class);
        System.out.println("dto:"+gameDTO);

    }
}
相关推荐
m0_675447087 分钟前
Solon 拉取 maven 包很慢或拉不了,怎么办?
java·maven
武昌库里写JAVA11 分钟前
SpringCloud+SpringCloudAlibaba学习笔记
java·开发语言·算法·spring·log4j
夏天吃哈密瓜12 分钟前
用Scala来解决成绩排名的相关问题
开发语言·后端·scala
爱编程的小生12 分钟前
SpringBoot Task
java·spring boot·后端
CoderJia程序员甲19 分钟前
重学SpringBoot3-异步编程完全指南
java·spring boot·后端·异步编程
小咖拉眯22 分钟前
第十六届蓝桥杯模拟赛第二期题解—Java
java·数据结构·算法·蓝桥杯·图搜索算法
扬子鳄00823 分钟前
Spring Boot自动配置机制
java·数据库·spring boot
岁岁岁平安25 分钟前
springboot实战(19)(条件分页查询、PageHelper、MYBATIS动态SQL、mapper映射配置文件、自定义类封装分页查询数据集)
java·spring boot·后端·mybatis·动态sql·pagehelper·条件分页查询
一直学习永不止步30 分钟前
LeetCode题练习与总结:数组中两个数的最大异或值--421
java·算法·leetcode·字典树·数组·位运算·哈希表
桃园码工1 小时前
第一章:Go 语言概述 1.什么是 Go 语言? --Go 语言轻松入门
开发语言·后端·golang