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、封装接口
package com.geesun.picking.converter;
import com.geesun.picking.domain.MacPickingDetails;
import com.geesun.picking.domain.MacPickingMain;
import com.geesun.picking.domain.MacPrepareDetails;
import com.geesun.picking.domain.MacPrepareMain;
import com.geesun.picking.domain.dto.MacPickingDTO;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* pojo转换器
*
* @author xxx
* @name ProcessConverter
* @date 2023-01-05 11:43
*/
@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
public interface PojoConverter {
PojoConverter INSTANCE = Mappers.getMapper(PojoConverter .class);
/**
* 用户实体转换用户DTO对象
*/
UserDto toUserDto(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)
})
Context templateDto2Context(TemplateDto source);
(1) 类型不一致
@Mappings({
@Mapping(target = "createTime", expression = "java(com.java.mmzsblog.util.DateTransform.strToDate(source.getCreateTime()))"),
})
UserVO3 toConvertVO3(User source);
(2) 字段名不一致
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "name", target = "userName")
})
UserVO4 toConvertVO(User source);
(3) 忽略字段
@Mappings({
@Mapping(target = "status", ignore = true)
})
Context templateDto2Context(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文章来源:https://www.toymoban.com/news/detail-621657.html
在转换类上添加注解:@Mapper(builder = @Builder(disableBuilder = true))文章来源地址https://www.toymoban.com/news/detail-621657.html
到了这里,关于Java 对象拷贝与转换-org.mapstruct:mapstruct 包(@Mapper、@Mapping)的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!