Java对象拷贝MapStruct

介绍

编译期即可生成对象复制代码。简单理解,功能定位org.springframework.beans.BeanUtils
官网,GitHub-MapStruct。

入门

maven项目引入依赖:

  1. mapstruct:包含必要注解,如@Mapping
  2. mapstruct-processor:注解处理器,根据注解自动生成mapper实现
<dependency>
    <groupId>org.mapstructgroupId>
    <artifactId>mapstructartifactId>
dependency>
<dependency>
    <groupId>org.mapstructgroupId>
    <artifactId>mapstruct-processorartifactId>
dependency>

实战:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

@Mapper
// 如果增加componentModel 增生成的实现类有注解@Component
@Mapper(componentModel = "spring")
public interface AccountConvert {

    AccountConvert INSTANCE = Mappers.getMapper(AccountConvert.class);

    @Mappings({
            @Mapping(target = "username", source = "userName")
            @Mapping(source = "userTypeEnum", target = "type")
			@Mapping(target = "createTime", expression = "java(com.java.mmzsblog.util.DateTransform.strToDate(source.getCreateTime()))"),

    })
    Account dto2Entity(AccountDTO dto);

	@Data
	private static class Account {
		private string username;
	}
	
	@Data   
  	private static class AccountDTO {
		private string userName;
	}

	@Getter
	@AllArgsConstructor
	public enum UserTypeEnum {
	    JAVA("000", "Java开发工程师"),
	    DB("001", "数据库管理员");	    
	    private String value;
	    private String title;
	}

生成class文件:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-06-10T12:52:02+0800",
    comments = "version: 1.5.5.Final, compiler: javac, environment: Java 11.0.18 (Azul Systems, Inc.)"
)
public class AccountConvertImpl implements AccountConvert {
	@Override
    public Account dto2Entity(AccountDTO dto) {
        if (dto == null) {
            return null;
        }
        Account account = new Account();
        account.setUsername(dto.getUserName());
        return account;
    }
}

原理

当字段类型不一致时,MapStruct能够自动实现类型转换的类型:

  1. 基本类型及其对应的包装类型
  2. 基本类型的包装类型和String类型之间

除此之外的类型转换,可以通过定义表达式来进行指定转换。

注解

实战

记录使用遇到的问题

Internal error in the mapping processor: java.lang.NullPointerException

IDEA 2022.1.4 (Ultimate Edition)版本,以Debug模式启动应用,失败:

java: Internal error in the mapping processor: java.lang.NullPointerException
at org.mapstruct.ap.internal.processor.DefaultVersionInformation.createManifestUrl(DefaultVersionInformation.java:182)      at org.mapstruct.ap.internal.processor.DefaultVersionInformation.openManifest(DefaultVersionInformation.java:153)      at org.mapstruct.ap.internal.processor.DefaultVersionInformation.getLibraryName(DefaultVersionInformation.java:129)      at org.mapstruct.ap.internal.processor.DefaultVersionInformation.getCompiler(DefaultVersionInformation.java:122)
at org.mapstruct.ap.internal.processor.DefaultVersionInformation.fromProcessingEnvironment(DefaultVersionInformation.java:95)
at org.mapstruct.ap.internal.processor.DefaultModelElementProcessorContext.(DefaultModelElementProcessorContext.java:54)
at org.mapstruct.ap.MappingProcessor.processMapperElements(MappingProcessor.java:264)
at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:166)
at org.jetbrains.jps.javac.APIWrappers$ProcessorWrapper.process(APIWrappers.java:157)

参考,解决方法:升级MapStruct pom依赖,1.4.0.Final -> 当前最新版1.5.5.Final

No target bean properties found: can’t map Collection element, Consider to declare/implement a mapping method

应用以Debug模式启动报错:

java: No target bean properties found: can't map Collection element "DrugEsEntity drugEsEntity" to "DrugListVo drugListVo". Consider to declare/implement a mapping method: "DrugListVo map(DrugEsEntity value)".

现有接口定义:

@Mapper
public interface DrugConverter {
    DrugConverter INSTANCE = Mappers.getMapper(DrugConverter.class);
    
    List<DrugListVo> esToVos(List<DrugEsEntity> entities);
}

解决方法,增加接口:

DrugListVo esToVo(DrugEsEntity entity);

这在生成的DrugConverterImpl代码中可见一斑:
Java对象拷贝MapStruct_第1张图片

参考

MapStruct 1.5发布支持Map转为Bean

你可能感兴趣的:(Java,java)