引入編譯依賴
<properties>
<project-war-name>com-test</project-war-name>
<project.build.complier>1.8</project.build.complier>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
<maven-resources-plugin.version>3.0.2</maven-resources-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-war-plugin.version>2.2</maven-war-plugin.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<lombok.version>1.16.20</lombok.version>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${project.build.complier}</source>
<target>${project.build.complier}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<configuration>
<skipSource>true</skipSource>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project-war-name}</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
java
@Mapper
public interface UserCopier {
UserCopier INSTANCE = Mappers.getMapper(UserCopier.class);
@Mappings({
})
DtoPlfUserQueryInfo plfUserToDtoPlfUserQueryInfo(DoPlfUser user);
List<DtoPlfUserQueryInfo> plfUserToDtoPlfUserQueryInfo(List<DoPlfUser> addrList);
@Mappings({
})
DoPlfUser plfUserToDtoUserInsert(PlfUserInsertRequest user);
//bean copy
@Mappings({
})
DoPlfUser plfUserToDtoUserStop(PlfUserStopRequest req);
//bean copy
@Mappings({
@Mapping(source = "userName", target = "userName"),
@Mapping(source = "userMobile", target = "userMobile"),
@Mapping(source = "userPwd", target = "userPwd"),
@Mapping(source = "userCode", target = "userCode")
})
DtoMyBaseInfo plfUserToDtoMyBaseInfo(DoPlfUser user);
@Mappings({
@Mapping(source = "plfOrgId", target = "plfOrgId"),
@Mapping(source = "plfOrgName", target = "plfOrgName"),
@Mapping(source = "noDept", target = "noDept"),
@Mapping(source = "creatorName", target = "creatorName"),
@Mapping(source = "createTime", target = "createTime"),
@Mapping(source = "stopUserName", target = "stopUserName"),
@Mapping(source = "stopTime", target = "stopTime"),
@Mapping(source = "status", target = "status")
})
DtoOrgInfo plfOperOrgToDtoOrgInfo(DoPlfOperOrg doPlfOperOrg);
List<DtoOrgInfo> plfOperOrgToDtoOrgInfo(List<DoPlfOperOrg> doPlfOperOrgList);
}
新版無需配置到插件中
直接引入jar即可
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.1.Final</version>
</dependency>
實現類
/**
* description: 映射基礎類
*
* @author: 倔強的老鐵
* @date: 2021/12/24 10:31
* @version: 1.0
*/
@MapperConfig
public interface BaseMapping<SOURCE, TARGET> {
/**
* 映射同名屬性
*/
//@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
TARGET sourceToTarget(SOURCE var1);
/**
* 反向,映射同名屬性
*/
@InheritInverseConfiguration(name = "sourceToTarget")
SOURCE targetToSource(TARGET var1);
/**
* 映射同名屬性,集合形式
*/
@InheritConfiguration(name = "sourceToTarget")
List<TARGET> sourceToTarget(List<SOURCE> var1);
/**
* 反向,映射同名屬性,集合形式
*/
@InheritConfiguration(name = "targetToSource")
List<SOURCE> targetToSource(List<TARGET> var1);
/**
* 映射同名屬性,集合流形式
*/
List<TARGET> sourceToTarget(Stream<SOURCE> stream);
/**
* 反向,映射同名屬性,集合流形式
*/
List<SOURCE> targetToSource(Stream<TARGET> stream);
}
實體Copier
@Mapper
public interface PopBillImportCopier extends BaseMapping<DoPopBillImport, DtoPopBillImport>{
PopBillImportCopier INSTANCE = Mappers.getMapper(PopBillImportCopier.class);
}
用戶類轉換例子
示例使用的是 Spring 的方式,@Mapper 注解的 componentModel 屬性值為 spring,不過應該大多數都用的此模式進行開發。
@Mapping用於配置對象的映射關系,示例中 User 對象性別屬性名為 sex,而UserVo對象性別屬性名為gender,因此需要配置 target 與 source 屬性。
password 字段不應該返回到前台,可以采取兩種方式不進行轉換,第一種就是在vo對象中不出現password字段,第二種就是在@Mapping中設置該字段 ignore = true。
MapStruct 提供了時間格式化的屬性 dataFormat,支持Date、LocalDate、LocalDateTime等時間類型與String的轉換。示例中birthday 屬性為 LocalDate 類型,可以無需指定dataFormat自動完成轉換,而LocalDateTime類型默認使用的是ISO格式時間,在國內往往不符合需求,因此需要手動指定一下 dataFormat。
實現 User 與 UserVo 對象的轉換器
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(componentModel = "spring")
public interface UserMapping extends BaseMapping<User, UserVo> {
@Mapping(target = "gender", source = "sex")
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Override
UserVo sourceToTarget(User var1);
@Mapping(target = "sex", source = "gender")
@Mapping(target = "password", ignore = true)
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Override
User targetToSource(UserVo var1);
default List<UserConfig> strConfigToListUserConfig(String config) {
return JSON.parseArray(config, UserConfig.class);
}
default String listUserConfigToStrConfig(List<UserConfig> list) {
return JSON.toJSONString(list);
}
}
如果項目中也同時使用到了 Lombok,一定要注意 Lombok的版本要等於或者高於1.18.10,否則會有編譯不通過的情況發生,筆者掉進這個坑很久才爬了出來,希望各位不要重復踩坑。
嵌套,mapstruct 會發現,只要 變量名相同
對象或者數組都可以自動映射,如下圖