1、添加配置文件
<!-- mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.3.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.5.3.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.3.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>
2、添加转换抽象接口类
入参为被复制的对象,不同对象间的转换只需要在这里配置一次即可。
import com.siact.api.common.api.vo.common.DypropInsVO;
import com.siact.api.common.api.vo.common.StpropInsVO;
import com.siact.api.common.api.vo.common.TMInsSimpleVo;
import com.siact.api.common.api.vo.eq.EqDypropInsVO;
import com.siact.api.common.api.vo.eq.EqStpropInsVO;
import com.siact.ins.server.common.vo.common.InsTreeVo;
import com.siact.module.sec.dto.EqDypropInsDTO;
import com.siact.module.sec.dto.EqStpropInsDTO;
import com.siact.module.sec.dto.InsTreeDTO;
import com.siact.module.sec.dto.TMInsSimpleDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ClassConvertor {
ClassConvertor INSTANCE = Mappers.getMapper(ClassConvertor.class);
List<DTO> vos2DTOs (List<VO> vos);
DTO vo2DTOs VO vo;
........
}
3、实际应用
List<InsTreeVo> devInsList = SiactSecApiFeignUtil.obj(r);
List<InsTreeDTO> insTreeDTOS = ClassConvertor.INSTANCE.insTreeVos2DTOs(devInsList);