Top Related Projects
Quick Overview
ModelMapper is a Java library that simplifies object mapping between different object models. It automates the process of mapping fields from one object to another, reducing boilerplate code and making it easier to work with complex object structures.
Pros
- Reduces boilerplate code for object mapping
- Supports deep mapping and custom converters
- Integrates well with popular frameworks like Spring and Jakarta EE
- Offers flexible configuration options for mapping strategies
Cons
- May have a learning curve for complex mapping scenarios
- Performance overhead compared to manual mapping in some cases
- Limited support for generic types in certain situations
- Requires careful configuration to avoid unexpected mappings
Code Examples
- Basic mapping between two classes:
ModelMapper modelMapper = new ModelMapper();
UserDto userDto = modelMapper.map(user, UserDto.class);
- Custom mapping with explicit property mapping:
ModelMapper modelMapper = new ModelMapper();
modelMapper.typeMap(User.class, UserDto.class)
.addMapping(User::getFirstName, UserDto::setName)
.addMapping(User::getLastName, UserDto::setLastName);
UserDto userDto = modelMapper.map(user, UserDto.class);
- Using converters for complex type conversions:
ModelMapper modelMapper = new ModelMapper();
modelMapper.addConverter(new AbstractConverter<String, Date>() {
protected Date convert(String source) {
return new SimpleDateFormat("yyyy-MM-dd").parse(source);
}
});
Getting Started
To use ModelMapper in your Java project, follow these steps:
- Add the ModelMapper dependency to your project:
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>
- Create a ModelMapper instance:
ModelMapper modelMapper = new ModelMapper();
- Use the
map
method to perform object mapping:
DestinationType dest = modelMapper.map(source, DestinationType.class);
For more advanced configurations and usage, refer to the ModelMapper documentation.
Competitor Comparisons
Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another.
Pros of Dozer
- More configurable and flexible, allowing for complex mappings
- Supports XML-based configuration for mapping rules
- Better performance for large-scale mapping operations
Cons of Dozer
- Steeper learning curve due to more complex configuration options
- Requires more setup and boilerplate code for simple mappings
- Less active development and community support compared to ModelMapper
Code Comparison
Dozer:
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);
ModelMapper:
ModelMapper modelMapper = new ModelMapper();
DestinationObject destObject = modelMapper.map(sourceObject, DestinationObject.class);
Both libraries provide similar basic mapping functionality, but Dozer offers more advanced configuration options for complex scenarios. ModelMapper, on the other hand, focuses on simplicity and convention-based mapping.
Dozer excels in scenarios requiring intricate mapping rules and high performance for large datasets. ModelMapper is more suitable for projects prioritizing ease of use and quick setup, especially for simpler mapping requirements.
The choice between the two depends on the specific needs of the project, with Dozer being more powerful but complex, and ModelMapper offering a more straightforward approach for common use cases.
Simpler, better and faster Java bean mapping framework
Pros of Orika
- Generally faster performance, especially for large-scale mapping operations
- More flexible configuration options for complex mapping scenarios
- Built-in support for multi-threaded mapping
Cons of Orika
- Steeper learning curve due to more complex API
- Requires more setup and configuration compared to ModelMapper
- Less active development and community support
Code Comparison
ModelMapper:
modelMapper.createTypeMap(Source.class, Destination.class)
.addMappings(mapper -> {
mapper.map(Source::getFirstName, Destination::setName);
mapper.map(Source::getAge, Destination::setYears);
});
Orika:
mapperFactory.classMap(Source.class, Destination.class)
.field("firstName", "name")
.field("age", "years")
.register();
Both ModelMapper and Orika are popular Java object mapping libraries. ModelMapper focuses on simplicity and convention-based mapping, making it easier to use for straightforward scenarios. Orika, on the other hand, offers more powerful features and better performance at the cost of increased complexity. The choice between the two depends on the specific requirements of your project, such as performance needs, mapping complexity, and development team expertise.
An annotation processor for generating type-safe bean mappers
Pros of MapStruct
- Compile-time code generation, resulting in faster execution and earlier error detection
- Type-safe mapping with minimal runtime overhead
- Extensive customization options through annotations and custom mapping methods
Cons of MapStruct
- Steeper learning curve due to annotation-based configuration
- Requires an additional build step for code generation
- Less flexibility for dynamic mapping scenarios at runtime
Code Comparison
MapStruct:
@Mapper
public interface CarMapper {
@Mapping(source = "numberOfSeats", target = "seatCount")
CarDto carToCarDto(Car car);
}
ModelMapper:
ModelMapper modelMapper = new ModelMapper();
CarDto carDto = modelMapper.map(car, CarDto.class);
MapStruct generates type-safe mapping code at compile-time, while ModelMapper performs reflection-based mapping at runtime. MapStruct offers better performance and earlier error detection, but ModelMapper provides more flexibility for dynamic mappings without additional build steps.
Both libraries have their strengths, and the choice depends on project requirements, performance needs, and developer preferences. MapStruct is ideal for large-scale projects with complex mapping requirements, while ModelMapper excels in rapid prototyping and scenarios requiring runtime flexibility.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ModelMapper
ModelMapper is an intelligent object mapping library that automatically maps objects to each other. It uses a convention based approach while providing a simple refactoring safe API for handling specific use cases.
Visit modelmapper.org to learn more.
Modules
In v2.0, ModelMapper provides modules for 3rd library integration you can easily register by modelMapper.registerModule(new TheModule())
.
Related Projects
Contributing
Bug reports and feature requests are welcome via the issue tracker. Fixes and enhancements are also welcome via pull requests. If you're unsure about a contribution idea, feel free to contact me.
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot