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


    org.modelmapper
    modelmapper
    3.1.1

        3.1 属性映射

        3.2 类型映射继承

       3.3  校验

        3.4   配置

        3.5 转化映射

        3.6 Provider

        3.7 通用

附demo:

  public class Game {

    Address address;

    public Address getAddress() {
        return address;
    }

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


}

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;
    }
}


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();
    }
}

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 personMap = new PropertyMap() {
            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 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 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);

    }
}

你可能感兴趣的:(spring,boot,后端,java)