Sample program to learn the basic usage of ModelMapper.
If you’re a Maven user just add the modelmapper library as a dependency:
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>1.1.0</version>
</dependency>
Let’s try mapping some objects. Consider the following source and destination object models:
User to UserDTO
We can use ModelMapper to implicitly map an user instance to a new UserDTO:
ModelMapper modelMapper = new ModelMapper();
UserDTO userDTO = modelMapper.map(user, UserDTO.class);
When the map method is called, the source and destination types are analyzed to determine which properties implicitly match according to a matching strategy and other configuration.
ModelMapper will do its best to determine reasonable matches between properties.
If required we can also do the explicit mapping between properties.(inform the mapper about the properties explicitly)
modelMapper.addMappings(new PropertyMap<User, UserDTO>() {
protected void configure() {
map().setUserCity(source.getCity());
}
});