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
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
</dependency>
定義轉換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目錄中會自動生成該接口的實現(xiàn)類(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;
}
}
}
定義主函數(shù)測試:
public class Convert {
public static void main(String[] args) {
PersonDo2DtoMapping instance = PersonDo2DtoMapping.INSTANCE;
PersonDo personDo =
new PersonDo(1L, "zhige", "zhige.me@gmail.com", new Date(), new User(19, 2, 65.4));
PersonDto personDTO = instance.do2dto(personDo);
System.out.println(personDTO.toString());
}
//打印結果
//PersonDto(firstName=null, age=19, email=zhige.me@gmail.com, 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<PersonDto> listToList(List<PersonDo> 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<PersonDto> listToList(List<PersonDo> people) {
if (people == null) {
return null;
} else {
List<PersonDto> 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", "zhige.me@gmail.com", 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=zhige.me@gmail.com, birth=null, high=185, weight=65.5, adult=null)
}
自定義轉換規(guī)則
有時我們需要int轉boolean(帶有語義的轉換,比如age轉換為是否成年)暑椰。那么我們就可以定義轉換規(guī)則,或定義默認的轉換方法拟淮。先說下定義轉換規(guī)則方式干茉。
PersonTransRule.class
public class PersonTransRule {
public boolean intToBoolean(int age) {
return age > 18;
}
}
在mapping中引入規(guī)則:
使用mapper注解的uses屬性,參數(shù)類型為class數(shù)組很泊,可以指定多個轉換規(guī)則類角虫。
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 {
// 引入轉換規(guī)則類的對象
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) {
//這里使用轉換規(guī)則進行轉換
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", "zhige.me@gmail.com", new Date(), new User(12, 185, 65.5));
PersonDto personDTO = instance.do2dto(personDo);
System.out.println(personDTO.toString());
//打印結果
//PersonDto(firstName=null, age=12, email=zhige.me@gmail.com, birth=2019-06-11 17:07:53, high=null, weight=null, adult=false)
//因為age為12所以adult為false未成年
}
與spring整合:
依賴依然是上面說的兩個包。
只需要在映射接口的mapper注解中添加參數(shù)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);
}
編譯后生成的實現(xiàn)類:
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委造。