MapStruct 是一个属性映射工具,只需要定义一个Mapper 接口,MapStruct 就会自动实现这个映射接口,避免了复杂繁琐的映射实现。MapStruct官网地址: http://mapstruct.org/
一、pom依赖:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
</dependency>
二、接口工具:
Vo2PoBaseConvert:
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import java.util.List;
/**
* @Author: MHP
* @Description:
* @Date: create in 2022/1/11 19:26
*/
public interface Vo2PoBaseConvert<Vo, Po> {
@InheritConfiguration
Po vo2Po(Vo vo);
@InheritConfiguration
List<Po> vo2Po(List<Vo> sourceList);
@InheritInverseConfiguration
Vo po2Vo(Po po);
@InheritInverseConfiguration
List<Vo> po2Vo(List<Po> poList);
}
Dto2VoBaseConvert:
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import java.util.List;
/**
* @Author: MHP
* @Description:
* @Date: create in 2022/1/11 19:23
*/
public interface Dto2VoBaseConvert<Dto, Vo> {
@InheritConfiguration
Vo dto2Vo(Dto dto);
@InheritConfiguration
List<Vo> dto2Vo(List<Dto> sourceList);
@InheritInverseConfiguration
Dto vo2Dto(Vo vo);
@InheritInverseConfiguration
List<Dto> vo2Dto(List<Vo> voList);
}
Dto2PoBaseConvert:
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import java.util.List;
/**
* @Author: MHP
* @Description:
* @Date: create in 2022/1/11 19:23
*/
public interface Dto2PoBaseConvert<Dto, Po> {
@InheritConfiguration
Po dto2Po(Dto dto);
@InheritConfiguration
List<Po> dto2Po(List<Dto> sourceList);
@InheritInverseConfiguration
Dto po2Dto(Po po);
@InheritInverseConfiguration
List<Dto> po2Dto(List<Po> poList);
}
**
三、Mapper接口:
**
AdmCardConvert:
import com.msun.ascsoldhis.admcard.entity.po.AdmCard;
import com.msun.ascsoldhis.admcard.entity.po.AdmCardDTO;
import com.msun.ascsoldhis.admcard.entity.po.AdmCardVO;
import com.msun.ascsoldhis.common.convert.Dto2PoBaseConvert;
import com.msun.ascsoldhis.common.convert.Dto2VoBaseConvert;
import com.msun.ascsoldhis.common.convert.Vo2PoBaseConvert;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface AdmCardConvert extends Dto2PoBaseConvert<AdmCardDTO, AdmCard>, Dto2VoBaseConvert<AdmCardDTO, AdmCardVO>, Vo2PoBaseConvert<AdmCardVO, AdmCard> {
}