
MapStruct的使用教程
使用
发布日期:2025-04-12 00:34:43
浏览次数:10
分类:精选文章
本文共 2663 字,大约阅读时间需要 8 分钟。
MapStruct 使用指南
1. 添加依赖
在项目中首先需要添加 MapStruct 的依赖。对于 Maven 和 Gradle 项目,可以通过以下方式添加依赖:
Maven
org.mapstruct mapstruct 1.4.2.Final org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 org.mapstruct mapstruct-processor 1.4.2.Final
Gradle
dependencies { implementation 'org.mapstruct:mapstruct:1.4.2.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'}
2. 定义映射接口
创建一个接口,并使用 @Mapper
注解标注,定义源对象和目标对象之间的映射规则。例如:
import org.mapstruct.Mapper;import org.mapstruct.factory.Mappers;@Mapperpublic interface MyMapper { MyMapper INSTANCE = Mappers.getMapper(MyMapper.class); TargetObject map(SourceObject source);}
3. 使用映射方法
在业务逻辑中使用定义的映射接口来转换对象。例如:
public class MyService { public TargetObject convert(SourceObject source) { return MyMapper.INSTANCE.map(source); }}
4. 定义自定义映射方法
如果需要自定义映射逻辑,可以定义一个带有 @Mapping
注解的方法,并在其中指定映射规则。例如:
@Mapperpublic interface MyMapper { @Mapping(target = "targetProperty", source = "sourceProperty") @Mapping(target = "targetProperty2", expression = "java(String.valueOf(source.getSourceProperty2()))") TargetObject map(SourceObject source);}
5. 处理复杂映射
MapStruct 提供了多种注解来支持复杂的映射场景:
使用 @Mapping
注解
@Mapperpublic interface MyMapper { @Mapping(target = "dateOfBirth", source = "birthYear", qualifiedByName = "yearToCalendar") TargetObject map(SourceObject source); @Named("yearToCalendar") default Date yearToCalendar(int year) { Calendar calendar = Calendar.getInstance(); calendar.set(year, Calendar.JANUARY, 1); return calendar.getTime(); }}
支持的映射类型
- 自定义映射:使用
@Mapping
注解的expression
或qualifiedByName
属性。 - 集合映射:使用
@IterableMapping
和@MapMapping
注解。 - 枚举映射:直接映射枚举类型,或使用
@Mapping
注解的enumNamed
属性。 - 构造函数映射:使用
@Mapping
注解的constructor
属性。 - 嵌套属性映射:直接映射嵌套对象。
- 反向映射:定义反向映射方法,并使用
@Mapping
注解的inverse
属性。 - 使用装饰器:使用
@DecoratedWith
注解装饰 Mapper 接口。 - 映射继承:通过继承共享映射配置。
- 预处理和后处理:使用
@BeforeMapping
和@AfterMapping
注解。 - 上下文参数:使用
@Context
传递额外参数。
6. 集成 Spring
MapStruct 还可以与 Spring 集成,允许在映射器中注入 Spring 管理的 Bean。使用 @Mapper(componentModel = "spring")
注解以指定 Spring 作为组件模型:
@Mapper(componentModel = "spring")public interface MyMapper { TargetObject map(SourceObject source);}
这样,MapStruct 映射器就可以作为 Spring Bean 进行自动装配。
通过使用 MapStruct,您可以减少手动编写的样板代码,提高开发效率,并减少因手动映射而产生的错误。
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年05月20日 16时33分08秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
marquee标签
2025-04-12
Marshal类的使用
2025-04-12
Mask R-CNN
2025-04-12
MaskFormer
2025-04-12
MASM中可以定义的变量类型
2025-04-12
Mass DM 机器人工作正常,现在它不会发送消息
2025-04-12
Massive Data Mining学习记录
2025-04-12
MasterPage(母板页)的不一般用法
2025-04-12
MatchingFrontier包简介及R实现
2025-04-12
MateBook16重装攻略
2025-04-12
material-ui ant.design
2025-04-12
MaterialForm对tab页进行隐藏
2025-04-12
materialTabControl1_SelectedIndexChanged的使用
2025-04-12
Math.Atan2的基本讲解(C#版本)
2025-04-12
Math.round(),Math.ceil(),Math.floor()的区别
2025-04-12
mathlab中deepDreamImage的参数PyramidLevels的作用
2025-04-12
MathType 6.9中的字距该怎样进行调整
2025-04-12
MathType二次偏导怎么表示
2025-04-12
MathType给公式底部加箭头的教程
2025-04-12
math对象
2025-04-12