类之间转换框架 mapStruct
maven 中引用 jar
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
<version>${org.mapstruct.version}</version>
</dependency>
配置 maven-compiler-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>${java.version}</source> <!-- or higher, depending on your project -->
<target>${java.version}</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<!-- 如果使用lombok, 需要将lombok放在 mapstruct之前 -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<!-- 不生成impl生成的时间 -->
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
<!-- 生成的impl类将带有 @Component spring注解 -->
<arg>-Amapstruct.defaultComponentModel=spring</arg>
</compilerArgs>
</configuration>
</plugin>
使用
编写需要转换的类
转换 CustomerDo -> CustomerDto -> CustomerVo
@Data
@AllArgsConstructor
public class CustomerDo {
private String firstName;
private String lastName;
}
@Data
public class CustomerDto {
private String forename;
private String surname;
}
@Data
public class CustomerVo {
private String firstName;
private String lastName;
}
编写转换 Mapper (非Mybatis中的Mapper)
@org.mapstruct.Mapper
public interface CustomerMapper {
/** 其实就是 CustomerMapper 实例对象 */
CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);
/**
* CustomerDo 转换成 CustomerDto
*
* \@org.mapstruct.Mapping
* source: 源类的属性
* target: 目标类的属性
* 如果两个类中的属性名不相同, 则需要进行 source -> target 进行映射;
* 如果两个类属性名完全相同, 则无需添加 \@org.mapstruct.Mapping, MapStruct 自动相同名称进行映射
*
* @param customerDo 源数据
* @return 转换之后数据
*/
@Mapping(source = "firstName", target = "forename")
@Mapping(source = "lastName", target = "surname")
CustomerDto customerToDto(CustomerDo customerDo);
/** CustomerDto 转换成 CustomerVo */
@Mapping(source = "forename", target = "firstName")
@Mapping(source = "surname", target = "lastName")
CustomerVo customerToVo(CustomerDto dto);
}
编译结果
执行 mvn clean compile -Dmaven.test.skip=true, 结果如下:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
comments = "version: 1.4.2.Final, compiler: javac, environment: Java 1.8.0_271 (Oracle Corporation)"
)
// <arg>-Amapstruct.defaultComponentModel=spring</arg> 作用的结果, 如果不想用spring, 可以去掉
@Component
public class CustomerMapperImpl implements CustomerMapper {
@Override
public CustomerDto customerToDto(CustomerDo customerDo) {
if ( customerDo == null ) {
return null;
}
CustomerDto customerDto = new CustomerDto();
customerDto.setForename( customerDo.getFirstName() );
customerDto.setSurname( customerDo.getLastName() );
return customerDto;
}
@Override
public CustomerVo customerToVo(CustomerDto dto) {
if ( dto == null ) {
return null;
}
CustomerVo customerVo = new CustomerVo();
customerVo.setFirstName( dto.getForename() );
customerVo.setLastName( dto.getSurname() );
return customerVo;
}
}
测试
/**
* @author Chris
* @version 1.0.0
* @date 2021/02/02
*/
class CustomerMapperTest {
@Test
public void test1() {
System.out.println("===== test1 =====");
CustomerDo customerDo = new CustomerDo("Jacky", "Brown");
final CustomerDto customerDto = CustomerMapper.INSTANCE.customerToDto(customerDo);
System.out.println(customerDto);
final CustomerVo customerVo = CustomerMapper.INSTANCE.customerToVo(customerDto);
System.out.println(customerVo);
}
}
结果正常, 测试结束.
因为 MapStrcut 编译时生成的类中直接使用get/set做转换处理的.所以理论上性能应该是所有映射框架中最好的. 其他映射框架如 orika,beanutils. 如果做类之间转换, 推荐使用 MapStruct 及 orika