MapStruct的使用
最近在学习技术时候,发现一个特别好用的包,org.mapstruct:mapstruct,它是专门用来处理 domin 实体类与 model 类的属性映射的
它的优势:
很多项目大量映射的方式通过手动get、set,首先写法很low,没有技术含量。而且中间还可能牵涉了很多类型转换,嵌套之类的繁琐操作,非常的麻烦。
一、导包
在项目的pom.xml 文件夹中导入包
<!--mapStruct依赖--><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>1.3.1.Final</version></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.3.1.Final</version><scope>provided</scope></dependency>
如果想要在代码编写的时候有很好的提示的时候,也可以在pom.xml文件中加入相应的插件
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.5.1</version><configuration><source>1.8</source><target>1.8</target><annotationProcessorPaths><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.3.1.Final</version></path></annotationProcessorPaths></configuration></plugin></plugins></build>
二、常用注解
三、代码使用
案例详解:mapstruct 实体转换及List转换
1、封装接口
packagecom.geesun.picking.converter;importcom.geesun.picking.domain.MacPickingDetails;importcom.geesun.picking.domain.MacPickingMain;importcom.geesun.picking.domain.MacPrepareDetails;importcom.geesun.picking.domain.MacPrepareMain;importcom.geesun.picking.domain.dto.MacPickingDTO;importorg.mapstruct.Builder;importorg.mapstruct.Mapper;importorg.mapstruct.factory.Mappers;/**
* pojo转换器
*
* @author xxx
* @name ProcessConverter
* @date 2023-01-05 11:43
*/@Mapper(componentModel ="spring", builder =@Builder(disableBuilder =true))publicinterfacePojoConverter{PojoConverterINSTANCE=Mappers.getMapper(PojoConverter.class);/**
* 用户实体转换用户DTO对象
*/UserDtotoUserDto(User data);}
2、特殊案例
@Mappings({@Mapping(source ="cityId", target ="cityIds"),@Mapping(target ="groupIds", expression ="java(com.alibaba.fastjson.JSONObject.parseObject(source.getUserGroupIds(), new com.alibaba.fastjson.TypeReference<java.util.List<java.lang.Long>>() {}))"),@Mapping(target ="status", ignore =true)})ContexttemplateDto2Context(TemplateDto source);
(1) 类型不一致
@Mappings({@Mapping(target ="createTime", expression ="java(com.java.mmzsblog.util.DateTransform.strToDate(source.getCreateTime()))"),})UserVO3toConvertVO3(User source);
(2) 字段名不一致
@Mappings({@Mapping(source ="id", target ="userId"),@Mapping(source ="name", target ="userName")})UserVO4toConvertVO(User source);
(3) 忽略字段
@Mappings({@Mapping(target ="status", ignore =true)})ContexttemplateDto2Context(TemplateDto source);
(4)执行报错
报错案例:Unknown property “xxx” in result type xxx. Did you mean “null”?
代码生成器annotationProcessor标签部分,将lombok放在mapstruct之前
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><!--自动生成代码annotationProcessor--><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></path><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${mapstruct.version}</version></path></annotationProcessorPaths></configuration></plugin></plugins></build>
参考链接:Mapstruct使用时报Unknown property xxx in result type xxx. Did you mean null?
3、mapstruct禁用构建器@Bulder
参考文档:https://blog.csdn.net/lhc66666/article/details/123654325
在转换类上添加注解:@Mapper(builder = @Builder(disableBuilder = true))
版权归原作者 喝汽水的猫^ 所有, 如有侵权,请联系我们删除。