• 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时会报错提示参数类型不匹配。

    使用以上的方式就不存在这个问题。

  • 相关阅读:
    【C++】关于new分配空间
    【嵌入式】keil不识别野火高速dap的问题
    【Java】质数判断
    【学习方法】大学背知识点方法
    【算法】浮点数多次运算精确值下降
    【算法】main函数的堆栈溢出
    【算法】三值选中法
    【CC++笔记】register寄存器关键字
    JSON.parse()和JSON.stringify()
    webStorm使用和安装
  • 原文地址:https://www.cnblogs.com/umgsai/p/8428440.html
Copyright © 2020-2023  润新知