- MapStruct - Discussion
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Customizing Mapper
- MapStruct - Throwing Exceptions
- MapStruct - Mapping Enum
- MapStruct - Mapping Streams
- MapStruct - Mapping Map
- MapStruct - Mapping List
- MapStruct - Using defaultExpression
- MapStruct - Using defaultValue
- MapStruct - Using constant
- MapStruct - Using expression
- MapStruct - Using dateFormat
- MapStruct - Using numberFormat
- MapStruct - Implicit Type Conversion
- MapStruct - Builder
- MapStruct - Mapping Direct Field
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Multiple
- MapStruct - Custom Mapping
- MapStruct - Basic Mapping
- MapStruct - Environment Setup
- MapStruct - Overview
- MapStruct - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MapStruct - Overview
MapStruct is an annotation processor which is plugged into Java Compiler. Once plugged in, it can be used by command pne tools pke maven, gradle to process the mapping annotation to create a mapper class at compile time.
When Mapping is required?
In multilayered apppcations, data objects are used to fetch data from database and UI interacts with Models. Now data fetched into data models is required to map to Model or java beans to be passed to UI.Consider the following case.
Entity class connected with database.
StudentEntity.java
@Entity class StudentEntity { String id; String name; }
Model class connected with UI.
Student.java
class Student { String id; String name; }
How MapStruct Works?
MapStruct automates the process of creating a mapper to map data objects with model objects using annotations. It creates a mapper implementation at compile time which helps developer to figure out error during development and make is easy to understand. For example −
StudentMapper.java
@Mapper class StudentMapper { StudentMapper INSTANCE = Mappers.getMapper( StudentMapper.class ); StudentEntity modelToEntity(Student student); }
Now StudentMapper.INSTANCE can be used to get mapped objects easily.
StudentEntity studentEntity = StudentMapper.INSTANCE.modelToEntity(student);Advertisements