Mapstruct使用笔记
Mapstruct 可以用来处理java实体间的转换,比如DTO转DO,DO转DTO。
Pom依赖如下
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
<scope>provided</scope>
</dependency>
public class BooleanIntegerMapper {
public int asInt(boolean success) {
return success ? 1 : 0;
}
public boolean asBoolean(int success) {
return success == 1;
}
}
@Mapper(uses = { BooleanIntegerMapper.class })
public interface FaultyMachineMapper {
FaultyMachine doToDTO(FaultyMachineDO FaultyMachineDO);
CloudboxFaultyMachineDO dtoToDO(FaultyMachine faultyMachine);
List<FaultyMachine> dosToDTOs(Iterable<FaultyMachineDO> faultyMachineDOS);
}
Mapstruct会为FaultyMachineMapper生成实现类FaultyMachineMapperImpl,在工程的target目录下可以看到。Spring初始化时会实例化FaultyMachineMapperImpl对象,并且注入到需要使用的地方。
public class FaultyMachineMapperImpl implements FaultyMachineMapper {
private final BooleanIntegerMapper booleanIntegerMapper = new BooleanIntegerMapper();
public FaultyMachineMapperImpl() {
}
public FaultyMachine doToDTO(CloudboxFaultyMachineDO cloudboxFaultyMachineDO) {
if (cloudboxFaultyMachineDO == null) {
return null;
} else {
FaultyMachine faultyMachine = new FaultyMachine();
faultyMachine.setId(cloudboxFaultyMachineDO.getId());
faultyMachine.setAppName(cloudboxFaultyMachineDO.getAppName());
faultyMachine.setZone(cloudboxFaultyMachineDO.getZone());
faultyMachine.setHardwareTemplate(cloudboxFaultyMachineDO.getHardwareTemplate());
faultyMachine.setContainerIp(cloudboxFaultyMachineDO.getContainerIp());
faultyMachine.setContainerSn(cloudboxFaultyMachineDO.getContainerSn());
faultyMachine.setHostname(cloudboxFaultyMachineDO.getHostname());
faultyMachine.setEventCode(cloudboxFaultyMachineDO.getEventCode());
faultyMachine.setEventId(cloudboxFaultyMachineDO.getEventId());
faultyMachine.setEventType(cloudboxFaultyMachineDO.getEventType());
faultyMachine.setLoseData(this.booleanIntegerMapper.asBoolean(cloudboxFaultyMachineDO.getLoseData()));
faultyMachine.setNcIp(cloudboxFaultyMachineDO.getNcIp());
faultyMachine.setNcSn(cloudboxFaultyMachineDO.getNcSn());
faultyMachine.setStatus(cloudboxFaultyMachineDO.getStatus());
faultyMachine.setGmtCreate(cloudboxFaultyMachineDO.getGmtCreate());
faultyMachine.setGmtModified(cloudboxFaultyMachineDO.getGmtModified());
return faultyMachine;
}
}
public CloudboxFaultyMachineDO dtoToDO(FaultyMachine faultyMachine) {
if (faultyMachine == null) {
return null;
} else {
CloudboxFaultyMachineDO cloudboxFaultyMachineDO = new CloudboxFaultyMachineDO();
cloudboxFaultyMachineDO.setId(faultyMachine.getId());
cloudboxFaultyMachineDO.setAppName(faultyMachine.getAppName());
cloudboxFaultyMachineDO.setZone(faultyMachine.getZone());
cloudboxFaultyMachineDO.setHardwareTemplate(faultyMachine.getHardwareTemplate());
cloudboxFaultyMachineDO.setContainerIp(faultyMachine.getContainerIp());
cloudboxFaultyMachineDO.setContainerSn(faultyMachine.getContainerSn());
cloudboxFaultyMachineDO.setHostname(faultyMachine.getHostname());
cloudboxFaultyMachineDO.setEventCode(faultyMachine.getEventCode());
cloudboxFaultyMachineDO.setEventId(faultyMachine.getEventId());
cloudboxFaultyMachineDO.setEventType(faultyMachine.getEventType());
cloudboxFaultyMachineDO.setLoseData(this.booleanIntegerMapper.asInt(faultyMachine.isLoseData()));
cloudboxFaultyMachineDO.setNcIp(faultyMachine.getNcIp());
cloudboxFaultyMachineDO.setNcSn(faultyMachine.getNcSn());
cloudboxFaultyMachineDO.setGmtCreate(faultyMachine.getGmtCreate());
cloudboxFaultyMachineDO.setGmtModified(faultyMachine.getGmtModified());
cloudboxFaultyMachineDO.setStatus(faultyMachine.getStatus());
return cloudboxFaultyMachineDO;
}
}
public List<FaultyMachine> dosToDTOs(Iterable<CloudboxFaultyMachineDO> cloudboxFaultyMachineDOS) {
if (cloudboxFaultyMachineDOS == null) {
return null;
} else {
List<FaultyMachine> list = new ArrayList();
Iterator var3 = cloudboxFaultyMachineDOS.iterator();
while(var3.hasNext()) {
CloudboxFaultyMachineDO cloudboxFaultyMachineDO = (CloudboxFaultyMachineDO)var3.next();
list.add(this.doToDTO(cloudboxFaultyMachineDO));
}
return list;
}
}
}
代码中的使用
public class FaultyMachineRepositoryImpl implements FaultyMachineRepository {
private FaultyMachineMapper faultyMachineMapper = Mappers.getMapper(FaultyMachineMapper.class);
@Setter
private FaultyMachineDAO faultyMachineDAO;
@Override
public FaultyMachine getById(int id) {
FaultyMachineDO faultyMachineDO = faultyMachineDAO.getById(id);
FaultyMachine faultyMachine = faultyMachineMapper.doToDTO(faultyMachineDO);
return faultyMachine;
}
@Override
public int insertFaultyMachine(FaultyMachine faultyMachine) {
FaultyMachineDO faultyMachineDO = faultyMachineMapper.dtoToDO(faultyMachine);
return (int)faultyMachineDAO.insertFaultyMachine(faultyMachineDO);
}
}
如果Java类中有一个字段是boolean类型的,对应DB中类型是tinyint,此时使用BeanUtil在DTO和DO之间copy时会报错提示参数类型不匹配。
使用以上的方式就不存在这个问题。
![](https://img2020.cnblogs.com/blog/479975/202108/479975-20210812223620577-372116739.jpg)
积跬步以致千里,积小流以成江海。
2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
2016年5月之后博文发布与cnblogs上。
Github地址 https://github.com/umgsai
Keep moving~!!!
2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
2016年5月之后博文发布与cnblogs上。
Github地址 https://github.com/umgsai
Keep moving~!!!