对象转换之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);

    }
}
相关推荐
Dicky-_-zhang1 分钟前
分布式ID生成方案详解与实战
java·jvm
m0_474606782 分钟前
JAVA - 使用Apache POI 自定义报表字段手写导出(支持-合并单元格)
java·开发语言·apache
念何架构之路3 分钟前
Go pprof性能剖析
开发语言·后端·golang
zhz52143 分钟前
Spring Boot 接入国密实战:传输加密(TLCP)+ 密码加密(SM4)
java·spring boot·后端·国密·sm4
人道领域6 分钟前
【LeetCode刷题日记】617.合并二叉树(空间换安全,还是原地省内存)
java·数据结构·算法·leetcode
独自破碎E11 分钟前
机器人Java后端算法笔试题解析
java·windows·算法
我是一颗柠檬11 分钟前
【JDK8新特性】函数式接口Day2
java·开发语言·后端·intellij-idea
Trouvaille ~11 分钟前
【Redis篇】Redis 安装与启动:快速搭建一个 Redis 环境
数据库·redis·后端·ubuntu·缓存·环境搭建·安装教程
Bat U12 分钟前
JavaEE|JVM
java·jvm·java-ee
Mahir0814 分钟前
Spring Boot 自动装配深度解密:从原理到自定义 Starter 实战
java·spring boot·后端·自动装配·自定义starter·大厂面试题