API
依赖引入
<mapstruct.version>1.3.1.Final</mapstruct.version>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
基础使用
- 接口创建
@Mapper
注解将该接口注册为转换器
- 创建
INSTENCE
- 创建需要转换的接口方法
@Mapper
public interface MapStructsConverter {
MapStructsConverter INSTENCE = Mappers.getMapper(MapStructsConverter.class);
NameCopy name2Copy(Name name);
}
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-03-30T21:23:01+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_311 (Oracle Corporation)"
)
public class MapStructsConverterImpl implements MapStructsConverter {
@Override
public NameCopy name2Copy(Name name) {
if ( name == null ) {
return null;
}
NameCopy nameCopy = new NameCopy();
nameCopy.setLast( name.getLast() );
nameCopy.setFullName( name.getFullName() );
nameCopy.setFirstName( name.getFirst() + "name" );
return nameCopy;
}
}
以上操作可以将A类对象a,将类型相同变量名相同的变量进行映射,获取b对象。
注解使用
@Mapping
同类型不同名
@Mapping(target = "firstName", source = "first")
NameCopy name2Copy(Name name);
忽略
@Mapping(target = "firstName", ignore = true)
NameCopy name2Copy(Name name);
类型不同
不能直接映射,需要自定义处理之后再映射
expression
@Mapping(target = "firstName", expression = "java(name.getFirst() + \"name\")")
NameCopy name2Copy(Name name);
qulifiedByName
@Mapping(target = "last", source = "last", qualifiedByName = "formatDoubleDef")
NameCopy name2Copy(Name name);
@Named("formatDoubleDef")
default Name formatDouble(String source) {
return new Name().setLast(source);
}
当两个类型不同的变量不能直接进行映射时,mapStucts
会进行默认的映射,比如有String
与Integer
之间的转换。当默认的转换不足以将两个变量进行映射时,可以通过创建自定义方法来进行处理并加注解@Named
,通过qualifiedByName
指定对应的方法进行处理。
不同类型的默认转换
NameCopy name2Copy(Name name);
@Mapping(target = "last", expression = "java(last)")
Name sting2Name(String last);
接口中定义的default
方法需要通过qualifiedByName
进行指定;
而接口中定义的接口方法则可直接进行转换,不需要进行指定。但是当,存在多个方法的时候必须指定使用哪一个方法。
@Mapping(target = "last", expression = "java(this.sting2Name(name.getLast()))")
NameCopy name2Copy(Name name);
@Mapping(target = "last", expression = "java(last)")
Name sting2Name(String last);
@Mapping(target = "last", expression = "java(last + \"123\")")
Name sting2Name1(String last);
List之间的转换
在List转换时,直接通过通过泛型,将源list进行遍历,调用target泛型和destination泛型的转换方法。将源list一个一个转换成target list。因此在进行list之间的转换的基础是泛型指定的类型之间的转换。
假如直接将原本应该写在泛型所指的方法上,则这些注解可能会因为版本的问题失效。
@Mapping(target = "firstName", expression = "java(name.getFirst() + \"name\")")
@Mapping(target = "last", expression = "java(this.sting2Name(name.getLast()))")
NameCopy name2Copy(Name name);
NameCopy name2Copy1(Name name);
List<NameCopy> names2Copy(List<Name> names);
@Override
public NameCopy name2Copy(Name name) {
if ( name == null ) {
return null;
}
NameCopy nameCopy = new NameCopy();
nameCopy.setFullName( name.getFullName() );
nameCopy.setLast( this.sting2Name(name.getLast()) );
nameCopy.setFirstName( name.getFirst() + "name" );
return nameCopy;
}
@Override
public List<NameCopy> names2Copy(List<Name> names) {
if ( names == null ) {
return null;
}
List<NameCopy> list = new ArrayList<NameCopy>( names.size() );
for ( Name name : names ) {
list.add( name2Copy( name ) );
}
return list;
}
为List转换指定方法进行
@Named("123")
@Mapping(target = "firstName", expression = "java(name.getFirst() + \"name\")")
@Mapping(target = "last1", source = "last1", qualifiedByName = "formatDoubleDef", defaultValue = "123")
@Mapping(target = "last", expression = "java(this.sting2Name(name.getLast()))")
NameCopy name2Copy(Name name);
@IterableMapping(qualifiedByName = "123")
List<NameCopy> names2Copy(List<Name> names);
default value
source 默认值
@Mapping(target = "firstName", expression = "java(name.getFirst() + \"name\")")
@Mapping(target = "last1", source = "last1", qualifiedByName = "formatDoubleDef", defaultValue = "123")
@Mapping(target = "last", expression = "java(this.sting2Name(name.getLast()))")
NameCopy name2Copy(Name name);
@Override
public NameCopy name2Copy(Name name) {
if ( name == null ) {
return null;
}
NameCopy nameCopy = new NameCopy();
if ( name.getLast1() != null ) {
nameCopy.setLast1( formatDouble( name.getLast1() ) );
}
else {
nameCopy.setLast1( formatDouble( "123" ) );
}
nameCopy.setFullName( name.getFullName() );
nameCopy.setFirstName( name.getFirst() + "name" );
nameCopy.setLast( this.sting2Name(name.getLast()) );
return nameCopy;
}
@Override
public List<NameCopy> names2Copy(List<Name> names) {
if ( names == null ) {
return null;
}
List<NameCopy> list = new ArrayList<NameCopy>( names.size() );
for ( Name name : names ) {
list.add( name2Copy( name ) );
}
return list;
}
@Override
public NameCopy name2Copy(Name name) {
if ( name == null ) {
return null;
}
NameCopy nameCopy = new NameCopy();
if ( name.getLast1() != null ) {
nameCopy.setLast1( formatDouble( name.getLast1() ) );
}
else {
nameCopy.setLast1( formatDouble( "123" ) );
}
nameCopy.setFullName( name.getFullName() );
nameCopy.setFirstName( name.getFirst() + "name" );
nameCopy.setLast( this.sting2Name(name.getLast()) );
return nameCopy;
}
constant
对指定的变量进行常量赋值。
@Mapping(target = "last1", constant = "last")
@Mapping(target = "firstName", source = "first", defaultValue = "undefined")
@Mapping(target = "last", expression = "java(this.sting2Name(name.getLast()))")
NameCopy name2Copy2(Name name);
@Override
public NameCopy name2Copy2(Name name) {
if ( name == null ) {
return null;
}
NameCopy nameCopy = new NameCopy();
if ( name.getFirst() != null ) {
nameCopy.setFirstName( name.getFirst() );
}
else {
nameCopy.setFirstName( "undefined" );
}
nameCopy.setFullName( name.getFullName() );
nameCopy.setLast1( "last" );
nameCopy.setLast( this.sting2Name(name.getLast()) );
return nameCopy;
}
多对一的转换
多转一的时候,当变量类型与变量名相同的变量同时存在源对象中,需要进行指定。
@Mapping(target = "fullName", source = "name1.fullName")
@Mapping(target = "last1", source = "name1.last1")
@Mapping(target = "last", expression = "java(this.sting2Name(name1.getLast()))")
@Mapping(target = "firstName", source = "name2.first")
NameCopy name2Copy(Name name1, Name name2);
@Override
public NameCopy name2Copy(Name name1, Name name2) {
if ( name1 == null && name2 == null ) {
return null;
}
NameCopy nameCopy = new NameCopy();
if ( name1 != null ) {
nameCopy.setFullName( name1.getFullName() );
nameCopy.setLast1( name1.getLast1() );
}
if ( name2 != null ) {
nameCopy.setFirstName( name2.getFirst() );
}
nameCopy.setLast( this.sting2Name(name1.getLast()) );
return nameCopy;
}
@MappingTarget
当目标对象为主动传入,而不是返回新的对象。
@Mapping(target = "last", expression = "java(this.sting2Name(name.getLast()))")
void name2Copy(Name name, @MappingTarget NameCopy nameCopy);
@Override
public void name2Copy(Name name, NameCopy nameCopy) {
if ( name == null ) {
return;
}
nameCopy.setLast1( name.getLast1() );
nameCopy.setFullName( name.getFullName() );
nameCopy.setLast( this.sting2Name(name.getLast()) );
}