mapstruct对象转换工具
基本使用:
mapstruct是一个基于java注解生成类型安全bean的转换工机;
ps:最好可以和lombok
一起使用
一般情况下,将do转换为dto,需要进行一下操作:
类PersonDo:
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class PersonDo {
private Long id;
private String name;
private String email;
private Date birthday;
private User user;
}
类PersonDto:
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class PersonDto {
private String firstName;
private Integer age;
private String email;
private Date birth;
private Integer high;
private boolean adult;
}
方法进行转换:
PersonDto do2Dto(PersonDo personDo) {
PersonDto personDto = new PersonDto();
personDto.setFirstName(personDo.getName());
personDto.setEmail(personDo.getEmail());
Integer age = personDo.getUser().getAge();
personDto.setAge(age);
personDto.setAdult(age > 18);
Date birthday = personDo.getBirthday();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(birthday);
personDto.setBirth(format);
return personDto;
}
基本功能引入:
使用mapstruct:
引入依赖的包:
pom.xml
org.mapstruct
mapstruct-jdk8
1.2.0.Final
org.mapstruct
mapstruct-processor
1.2.0.Final
定义转换mapping接口:
PersonDo2DtoMapping.class
import com.lee.boot.mapstruct.bean.PersonDo;
import com.lee.boot.mapstruct.bean.User;
import com.lee.boot.mapstruct.dto.PersonDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import java.util.List;
import java.util.Set;
@Mapper()
public interface PersonDo2DtoMapping {
PersonDo2DtoMapping INSTANCE = Mappers.getMapper(PersonDo2DtoMapping.class);
/**
* dateformat 在Date对象转为字符串时指定格式
*
* @param personDo
* @return
*/
@Mapping(source = "birthday", target = "birth", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Mapping(source = "user.age", target = "age")
@Mapping(source = "email", target = "email")
PersonDto do2dto(PersonDo personDo);
}
在编译后class目录中会自动生成该接口的实现类(idea中为target目录):
PersonDo2DtoMappingImpl.class
import com.lee.boot.mapstruct.bean.PersonDo;
import com.lee.boot.mapstruct.bean.User;
import com.lee.boot.mapstruct.dto.PersonDto;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class PersonDo2DtoMappingImpl implements PersonDo2DtoMapping {
public PersonDo2DtoMappingImpl() {
}
public PersonDto do2dto(PersonDo personDo) {
if (personDo == null) {
return null;
} else {
PersonDto personDto = new PersonDto();
if (personDo.getBirthday() != null) {
personDto.setBirth((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(personDo.getBirthday()));
}
Integer age = this.personDoUserAge(personDo);
if (age != null) {
personDto.setAge(age);
}
personDto.setEmail(personDo.getEmail());
return personDto;
}
}
}
定义主函数测试:
public class Convert {
public static void main(String[] args) {
PersonDo2DtoMapping instance = PersonDo2DtoMapping.INSTANCE;
PersonDo personDo =
new PersonDo(1L, "zhige", "[email protected]", new Date(), new User(19, 2, 65.4));
PersonDto personDTO = instance.do2dto(personDo);
System.out.println(personDTO.toString());
}
//打印结果
//PersonDto(firstName=null, age=19, [email protected], birth=2019-06-11 14:19:53, high=null, weight=null, adult=null)
}
以上是mapstruct的基本转换功能。
其它功能:
还有一些可配置的功能:
do对象集合转换成dto对象集合:
PersonDo2DtoMapping.class
public interface PersonDo2DtoMapping {
/**
* 定义一个常量给调用者使用
*/
PersonDo2DtoMapping INSTANCE = Mappers.getMapper(PersonDo2DtoMapping.class);
@Mapping(source = "birthday", target = "birth", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Mapping(source = "user.age", target = "age")
@Mapping(source = "email", target = "email")
PersonDto do2dto(PersonDo personDo);
/**
* 会使用domain2dto方法进行对象list转换
**/
List listToList(List people);
}
编译后:
PersonDo2DtoMappingImpl.class
public class PersonDo2DtoMappingImpl implements PersonDo2DtoMapping {
public PersonDo2DtoMappingImpl() {
}
public PersonDto do2dto(PersonDo personDo) {
if (personDo == null) {
return null;
} else {
PersonDto personDto = new PersonDto();
if (personDo.getBirthday() != null) {
personDto.setBirth((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(personDo.getBirthday()));
}
Integer age = this.personDoUserAge(personDo);
if (age != null) {
personDto.setAge(age);
}
personDto.setEmail(personDo.getEmail());
return personDto;
}
}
// 可以看到这里转换的时候调用了do2dto方法,也就是说mapsstruct能自动的使用合适的转换方法
public List listToList(List people) {
if (people == null) {
return null;
} else {
List list = new ArrayList(people.size());
Iterator var3 = people.iterator();
while(var3.hasNext()) {
PersonDo personDo = (PersonDo)var3.next();
list.add(this.do2dto(personDo));
}
return list;
}
}
}
两个对象转换为一个对象:
PersonDo2DtoMapping.class
@Mapper()
public interface PersonDo2DtoMapping {
PersonDo2DtoMapping INSTANCE = Mappers.getMapper(PersonDo2DtoMapping.class);
@Mapping(source = "personDo.name", target = "firstName")
@Mapping(source = "personDo.email", target = "email")
@Mapping(source = "user.age", target = "age")
@Mapping(source = "user.high", target = "high")
@Mapping(source = "user.weight", target = "weight")
PersonDto twoPar2dto(PersonDo personDo, User user);
}
PersonDo2DtoMappingImpl.class
public class PersonDo2DtoMappingImpl implements PersonDo2DtoMapping {
public PersonDo2DtoMappingImpl() {
}
public PersonDto twoPar2dto(PersonDo personDo, User user) {
if (personDo == null && user == null) {
return null;
} else {
PersonDto personDto = new PersonDto();
if (personDo != null) {
personDto.setFirstName(personDo.getName());
personDto.setEmail(personDo.getEmail());
}
if (user != null) {
personDto.setWeight(user.getWeight());
personDto.setHigh(user.getHigh());
personDto.setAge(user.getAge());
}
return personDto;
}
}
}
测试:
public class Convert {
public static void main(String[] args) {
PersonDo2DtoMapping instance = PersonDo2DtoMapping.INSTANCE;
PersonDo personDo =
new PersonDo(1L, "zhige", "[email protected]", new Date(), new User());
User user = new User(12, 185, 65.5);
PersonDto personDTO = instance.twoPar2dto(personDo, user);
System.out.println(personDTO.toString());
}
//打印结果
//PersonDto(firstName=zhige, age=12, [email protected], birth=null, high=185, weight=65.5, adult=null)
}
自定义转换规则
有时我们需要int转boolean(带有语义的转换,比如age转换为是否成年)。那么我们就可以定义转换规则,或定义默认的转换方法。先说下定义转换规则方式。
PersonTransRule.class
public class PersonTransRule {
public boolean intToBoolean(int age) {
return age > 18;
}
}
在mapping中引入规则:
使用mapper注解的uses属性,参数类型为class数组,可以指定多个转换规则类。
PersonDo2DtoMapping.classs
@Mapper(uses = PersonTransRule.class)
public interface PersonDo2DtoMapping {
PersonDo2DtoMapping INSTANCE = Mappers.getMapper(PersonDo2DtoMapping.class);
@Mapping(source = "birthday", target = "birth", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Mapping(source = "user.age", target = "age")
@Mapping(source = "email", target = "email")
@Mapping(source = "user.age",target = "adult")
PersonDto do2dto(PersonDo personDo);
}
编译后的文件:
PersonDo2DtoMappingImpl.class
public class PersonDo2DtoMappingImpl implements PersonDo2DtoMapping {
// 引入转换规则类的对象
private final PersonTransRule personTransRule = new PersonTransRule();
public PersonDo2DtoMappingImpl() {
}
public PersonDto do2dto(PersonDo personDo) {
if (personDo == null) {
return null;
} else {
PersonDto personDto = new PersonDto();
if (personDo.getBirthday() != null) {
personDto.setBirth((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(personDo.getBirthday()));
}
Integer age = this.personDoUserAge(personDo);
if (age != null) {
//这里使用转换规则进行转换
personDto.setAdult(this.personTransRule.intToBoolean(age));
}
Integer age1 = this.personDoUserAge(personDo);
if (age1 != null) {
personDto.setAge(age1);
}
personDto.setEmail(personDo.getEmail());
return personDto;
}
}
}
测试:
public class Convert {
public static void main(String[] args) {
PersonDo2DtoMapping instance = PersonDo2DtoMapping.INSTANCE;
PersonDo personDo =
new PersonDo(1L, "zhige", "[email protected]", new Date(), new User(12, 185, 65.5));
PersonDto personDTO = instance.do2dto(personDo);
System.out.println(personDTO.toString());
//打印结果
//PersonDto(firstName=null, age=12, [email protected], birth=2019-06-11 17:07:53, high=null, weight=null, adult=false)
//因为age为12所以adult为false未成年
}
与spring整合:
依赖依然是上面说的两个包。
只需要在映射接口的mapper注解中添加参数componentModel = "spring"
PersonDo2DtoMapping.classs
@Mapper(uses = PersonTransRule.class,componentModel = "spring")
public interface PersonDo2DtoMapping {
@Mapping(source = "birthday", target = "birth", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Mapping(source = "user.age", target = "age")
@Mapping(source = "email", target = "email")
@Mapping(source = "user.age",target = "adult")
PersonDto do2dto(PersonDo personDo);
}
编译后生成的实现类:
PersonDo2DtoMappingImpl.class
@Component
public class PersonDo2DtoMappingImpl implements PersonDo2DtoMapping {
@Autowired
private PersonTransRule personTransRule;
@Override
public PersonDto do2dto(PersonDo personDo) {
if ( personDo == null ) {
return null;
}
PersonDto personDto = new PersonDto();
if ( personDo.getBirthday() != null ) {
personDto.setBirth( new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( personDo.getBirthday() ) );
}
Integer age = personDoUserAge( personDo );
if ( age != null ) {
personDto.setAdult( personTransRule.intToBoolean( age.intValue() ) );
}
Integer age1 = personDoUserAge( personDo );
if ( age1 != null ) {
personDto.setAge( age1 );
}
personDto.setEmail( personDo.getEmail() );
return personDto;
}
}
在使用该映射转换的时候只要使用@Autowired注解注入就ok。