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

    }
}
相关推荐
Moshow郑锴3 小时前
实战分享:用 SpringBoot-API-Scheduler 构建 API 监控闭环 —— 从断言验证到智能警报
java·spring boot·后端·任务调度
掘我的金3 小时前
播放器最怕“首帧黑屏”?我给 LibreTV 加了一套缓冲与预加载策略
java
金融数据出海3 小时前
日本股票市场渲染 KlineCharts K 线图
前端·后端
低客的黑调3 小时前
为你的项目选择一个适合的[垃圾收集器]
java·jvm·算法
雨中飘荡的记忆3 小时前
优惠券系统设计与实现
java
1***t8273 小时前
将 vue3 项目打包后部署在 springboot 项目运行
java·spring boot·后端
芬加达3 小时前
leetcode34
java·数据结构·算法
__万波__3 小时前
二十三种设计模式(三)--抽象工厂模式
java·设计模式·抽象工厂模式
疯狂的程序猴4 小时前
iOS 日志管理的工程化实践 构建从开发调试到系统日志分析的多工具协同体系
后端
申阳4 小时前
Day 17:03. 基于 Tauri 2.0 开发后台管理系统-登录页面开发
前端·后端·程序员