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

    }
}
相关推荐
刘立军30 分钟前
RESTful 与契约优先:规范接口定义,统一接口设计范式
后端·架构·ai编程
JavaGuide1 小时前
GitHub 4.5 万+ Star!GitNexus 把代码仓库变成了 Claude Code / Codex 能查询的知识图谱
后端·ai编程
Lam Tang1 小时前
APS 系列文章 08
java·代理模式
子兮曰1 小时前
DeepSeek Harness 架构深潜:一个把 Agent 运行时做成纯插件树的开源 Harness
前端·后端·deepseek
萧瑟余晖2 小时前
Java深入解析篇三十三之消息队列
java·开发语言
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的序列容器
java·c++·面试
IT爱学堂2 小时前
尚硅谷Java+AI大模型应用开发革新版本 2025年3月
java·开发语言·人工智能
子兮曰2 小时前
AI Agent 完整入门指南:从 LLM 到生产落地的 30+ 个核心概念
前端·后端·agent
渔夫正在掘金3 小时前
Cordis 插件热插拔能力深度解析
后端·node.js
不爱编程的小九九3 小时前
小九源码-springboot004-springboot智能阅读推荐系统
java·spring boot·后端