我有一个Spring Boot 2.2的项目,我使用的是MapStruct 1.3.1和Lombook 1.18.12。我使用MapStruct 1.3.1和Lombook 1.18.12。
我有这些模型。
@EntityListeners({AuditingEntityListener.class})
@MappedSuperclass
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public abstract class AbstractEntity implements Persistable<Long>, Serializable {
//stuff
}
+
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@DiscriminatorValue(value = "AuditLog")
@EntityListeners(AuditLogListener.class)
@NoArgsConstructor
@SuperBuilder
@Data
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public class AuditLog extends AbstractEntity {
//stuff
}
+
@Entity
@DiscriminatorValue(value = "AuditCdr")
@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
public class AuditCdr extends AuditLog {
@Size(max = 30)
private String from;
@Size(max = 30)
private String to;
@Enumerated(EnumType.STRING)
@Size(max = 50)
private CallType callType;
private Instant startDate;
private Instant endDate;
public AuditCdr() {
setOperationType(AuditType.CDR);
}
}
我创建了这个DTO。
@Data
@Builder
public class Cdr3CXDto {
String from;
String to;
String type;
String startDate;
String endDate;
Long contactId;
}
和这个映射器。
@Mapper(componentModel = "spring")
public interface Cdr3CXMapper {
@ValueMappings({
@ValueMapping(source = "Inbound", target = "INBOUND"),
@ValueMapping(source = "Outbound", target = "OUTBOUND"),
@ValueMapping(source = "Missed", target = "MISSED"),
@ValueMapping(source = "Notanswered", target = "NOT_ANSWERED")
})
@Mapping(source = "contactId", target = "parentId")
@Mapping(source = "type", target = "callType")
AuditCdr cdr3CXDtoToAuditCdr(Cdr3CXDto cdr3CXDto);
}
这是我在pom.xml中的相关部分。
<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>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
我还添加了依赖关系。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
<scope>provided</scope>
</dependency>
当我运行mvn安装时,mapstructs为mapper生成正确的实现。
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-04-30T14:09:56+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
@Component
public class Cdr3CXMapperImpl implements Cdr3CXMapper {
@Override
public AuditCdr cdr3CXDtoToAuditCdr(Cdr3CXDto cdr3CXDto) {
if ( cdr3CXDto == null ) {
return null;
}
AuditCdr auditCdr = new AuditCdr();
auditCdr.setFrom( cdr3CXDto.getFrom() );
auditCdr.setTo( cdr3CXDto.getTo() );
if ( cdr3CXDto.getStartDate() != null ) {
auditCdr.setStartDate( Instant.parse( cdr3CXDto.getStartDate() ) );
}
if ( cdr3CXDto.getEndDate() != null ) {
auditCdr.setEndDate( Instant.parse( cdr3CXDto.getEndDate() ) );
}
return auditCdr;
}
}
没有编译错误,这是确定的。当我像图中那样运行JunitTest时,我出现了一个编译错误,因为这次的实现是错误的。
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-04-30T15:09:29+0200",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
@Component
public class Cdr3CXMapperImpl implements Cdr3CXMapper {
@Override
public AuditCdr cdr3CXDtoToAuditCdr(Cdr3CXDto cdr3CXDto) {
if ( cdr3CXDto == null ) {
return null;
}
AuditLogBuilder<?, ?> auditCdr = AuditLog.builder();
auditCdr.parentId( cdr3CXDto.getContactId() );
return auditCdr.build();
}
}
似乎MapStruct使用了父类的构建器,而不是像下面描述的那样使用子类 疑问. 我希望有一种方法可以指导mapstruct生成正确的代码,即使我在IntelliJ IDE中运行一个测试。
Maven和IntelliJ的编译执行应该没有区别。如果有区别,可能意味着Lombok的一些问题。@SuperBuilder
.
我将尝试添加 printProcessorInfo
和 printRounds
编译器参数,这样你就可以看到发生了什么。