pom文件修改
修改1
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<m2e.apt.activation>jdt_apt</m2e.apt.activation>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok.version>1.18.18</lombok.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
修改2
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
修改3
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>tech.foolfish.cpc.ccpbs.CcpbsApplication</mainClass>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<generatedSourcesDirectory>${project.build.directory}/generated-sources</generatedSourcesDirectory>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
<!-- due to problem in maven-compiler-plugin, for verbose mode add showWarnings -->
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
<arg>-Amapstruct.suppressGeneratorVersionInfoComment=true</arg>
<arg>-Amapstruct.verbose=true</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
代码案例
@Data
public class StationWhitelistEntity extends Creator {
private Long id;
private String stationId;
private int itemStatus;
private int preferenceStatus;
private String stationName;
}
@Mapper
public interface WhitelistConverter {
WhitelistConverter INSTANCE = Mappers.getMapper(WhitelistConverter.class);
@Mapping(target = "id", ignore = true)
@Mapping(target = "creatorId", constant = "99999L")
@Mapping(target = "createTime", expression = "java(System.currentTimeMillis() / 1000)")
@Mapping(target = "updatorId", constant = "99999L")
@Mapping(target = "updateTime", expression = "java(System.currentTimeMillis() / 1000)")
@Mapping(target = "deleteInd", constant = "0")
StationWhitelistEntity updateStationWhitelistEntity(StationWhitelistEntity stationWhitelistEntity);
List<StationWhitelistEntity> updateStationWhitelistEntities(List<StationWhitelistEntity> stationWhitelistEntities);
}
mapstruct自动生成的代码如下
@Generated(
value = "org.mapstruct.ap.MappingProcessor"
)
public class WhitelistConverterImpl implements WhitelistConverter {
@Override
public StationWhitelistEntity updateStationWhitelistEntity(StationWhitelistEntity stationWhitelistEntity) {
if ( stationWhitelistEntity == null ) {
return null;
}
StationWhitelistEntity stationWhitelistEntity1 = new StationWhitelistEntity();
stationWhitelistEntity1.setItemStatus( stationWhitelistEntity.getItemStatus() );
stationWhitelistEntity1.setPreferenceStatus( stationWhitelistEntity.getPreferenceStatus() );
stationWhitelistEntity1.setStationId( stationWhitelistEntity.getStationId() );
stationWhitelistEntity1.setStationName( stationWhitelistEntity.getStationName() );
stationWhitelistEntity1.setCreatorId( (long) 99999L );
stationWhitelistEntity1.setCreateTime( System.currentTimeMillis() / 1000 );
stationWhitelistEntity1.setUpdatorId( (long) 99999L );
stationWhitelistEntity1.setUpdateTime( System.currentTimeMillis() / 1000 );
stationWhitelistEntity1.setDeleteInd( 0 );
return stationWhitelistEntity1;
}
@Override
public List<StationWhitelistEntity> updateStationWhitelistEntities(List<StationWhitelistEntity> stationWhitelistEntities) {
if ( stationWhitelistEntities == null ) {
return null;
}
List<StationWhitelistEntity> list = new ArrayList<StationWhitelistEntity>( stationWhitelistEntities.size() );
for ( StationWhitelistEntity stationWhitelistEntity : stationWhitelistEntities ) {
list.add( updateStationWhitelistEntity( stationWhitelistEntity ) );
}
return list;
}
}