最近项目中有使用Mapstruct实体类转换工具,非常好用,不过也是浅拷贝之类的工具,在这篇文章分享给大家。
1、引入maven依赖
org.mapstruct
mapstruct-jdk8
1.2.0.Final
org.mapstruct
mapstruct-processor
1.2.0.Final
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.8
1.8
org.projectlombok
lombok
1.18.4
org.mapstruct
mapstruct-processor
1.2.0.Final
-Amapstruct.defaultComponentModel=spring
-Amapstruct.unmappedTargetPolicy=WARN
2、定义BaseConvert接口(源对象属性与目标对象属性相同)
/**
* 1、SOURCE与TARGET中属性名相同的默认映射(如这两个都有name属性)
* 2、SOURCE与TARGET中属性名不同的,需要通过@Mappings({@Mapping()})明确关系来形成映射(如sex对应gender)
* 3、形成映射关系的属性类型不同的,需要通过表达式转换数据类型(如Date对应String)
* 4、无映射关系属性被忽略(如UserEntity的password)
*
* @author zhangzhixiang
* @since v1.0.0
*/
@Mapper
public interface BaseConvert {
/**
* 映射同名属性
* @param source 源对象
*/
TARGET source2Target(SORCE source);
/**
* 反向,映射同名属性
*/
@InheritInverseConfiguration(name = "source2Target")
SOURCE target2Source(TARGET target);
/**
* 映射同名属性,集合形式
* @param sources 源对象集合
*/
@InheritConfiguration(name = "source2Target")
List sources2Targets(List sources);
/**
* 反向,映射同名属性,集合形式
* @param targets 目标对象集合
*/
@InheritConfiguration(name = "target2Source")
List targets2Sources(List targets);
}
3、自定义实体转换接口(源对象属性与目标对象属性相同)
/**
* @description:自定义实体转换接口
* @author:zhangzhixiang
* @date:2019/03/07 13:19:23
*/
@Mapper
public interface ContactInfoConverter extends BaseConvert {
ContactInfoConverter INSTANCE = Mappers.getMapper(ContactInfoConverter.class);
}
4、DO转为BO(源对象属性与目标对象属性相同)
@Override
public void contactBo2Do(ContactInfoBO contactInfoBO) {
ContactInfoDO contactInfodo = ContactInfoConverter.INSTANCE.source2Target(contactInfoBO);
}
@Override
public void contactDo2Bo(ContactInfoDO contactInfoDO) {
ContactInfoBO contactInfobo = ContactInfoConverter.INSTANCE.target2Source(contactInfoDO);
}
@Override
public void contactBo2DoList(List contactInfoBOList) {
List contactInfoDOList = ContactInfoConverter.INSTANCE.sources2Targets(contactInfoBOList);
}
@Override
public void contactDo2BoList(List contactInfoDOList) {
List contactInfoBOList = ContactInfoConverter.INSTANCE.targets2Sources(contactInfoDOList);
}
5、定义InstitutionConverter接口(源对象属性与目标对象属性不同)
/**
* @author zhangzhixiang
* @date 2019/03/20 15:42:36
*/
@Mapper
public interface InstitutionConverter {
InstitutionConverter INSTANCE = getMapper(InstitutionConverter.class);
/**
* 映射不同名属性
*
* @param source 源对象
*/
@Mappings({
@Mapping(source = "institutionCode", target = "institutionCode"),
@Mapping(source = "institutionName", target = "institutionName"),
@Mapping(source = "areaCode", target = "areaCode"),
@Mapping(source = "areaName", target = "areaName"),
@Mapping(source = "invoiceTitle", target = "invoiceInstitutionTitle"),
@Mapping(source = "recipient", target = "invoiceIvoiceRecipient"),
@Mapping(source = "telephone", target = "invoiceInstitutionTel"),
@Mapping(source = "taxNo", target = "invoiceTaxNo"),
@Mapping(source = "bank", target = "invoiceBankOfDeposit"),
@Mapping(source = "bankAccount", target = "invoiceBankOfAccount")
})
InstitutionDO toDataObject(InstitutionEntity source);
/**
* 映射不同名属性,集合形式
* @param source 源对象集合
*/
@InheritConfiguration(name = "source2Target")
List toDataObject(List source);
}
4、Entity转为DO(源对象属性与目标对象属性不同)
@Override
public void entityToDO(InstitutionEntity institutionEntity) {
InstitutionDO institutionDO = institutionConverter.INSTANCE.toDataObject(institutionEntity);
}
@Override
public void entityToDOList(List institutionEntityList) {
List institutionDOList = institutionConverter.INSTANCE.toDataObject(institutionEntityList);
}
注:如果在实体转换的时候有报ClassNotFoundException,找不到自定义转换接口,请执行以下命令
mvn clean compile
Mapstruct的简易运用介绍到此结束。