- 测试入口:
package com.c2f.hbos.mcc;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
public class MapStructTest {
/**
* 1.单个对象测试
*/
@Test
public void po2DtoTest() {
UserPO po = UserPO.builder()
.id(1L)
.name("yang")
.gender(Gender.MAN)
.birthday(LocalDateTime.now())
.build();
UserDTO dto = UserStructMapper.INSTANCE.po2Dto(po);
System.err.println(JSON.toJSON(dto));
}
/**
* 2.集合测试
*/
@Test
public void po2DtoListTest() {
UserPO po = UserPO.builder()
.id(1L)
.name("yang")
.gender(Gender.MAN)
.birthday(LocalDateTime.now())
.build();
List<UserPO> poList = Arrays.asList(po);
List<UserDTO> dtoList = UserStructMapper.INSTANCE.po2DtoList(poList);
dtoList.forEach(it -> {
System.err.println(JSON.toJSON(it));
});
}
/**
* 3.多合一测试
*/
@Test
public void personAndAnimal2DtoTest() {
UserDTO dto = UserStructMapper.INSTANCE.personAndAnimal2Dto(new Person("杨"), new Animal(11L));
System.err.println(JSON.toJSON(dto));
}
}
- Mapstruct工具类:
@Mapper
interface UserStructMapper {
UserStructMapper INSTANCE = Mappers.getMapper(UserStructMapper.class);
/**
* 1.单个对象转换
*
* @param po
* @return
*/
@Mappings({
@Mapping(constant = "cn_zh", target = "locale"),
@Mapping(source = "gender", target = "genderCode", qualifiedByName = "toCode"),
@Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
UserDTO po2Dto(UserPO po);
/**
* 2.集合转换(自动引用单个对象转换)
*
* @param poList
* @return
*/
List<UserDTO> po2DtoList(List<UserPO> poList);
/**
* 3.多对象合并为一个对象
*
* @param person
* @param animal
* @return
*/
@Mappings({
@Mapping(source = "animal.id", target = "id"),
@Mapping(source = "person.name", target = "name")
})
UserDTO personAndAnimal2Dto(Person person, Animal animal);
/**
* 声明从编号到枚举对象的转换
*
* @param code
* @return
*/
@Named("fromCode")
default Gender fromCode(String code) {
return Gender.valueOf(code);
}
/**
* 声明从枚举对象到编号的转换
*
* @param gender
* @return
*/
@Named("toCode")
default String toCode(Gender gender) {
return gender.getCode();
}
}
- Pojo类:
@Getter
enum Gender {
/**
* 男
*/
MAN("0", "男"),
/**
* 女
*/
WOMAN("1", "女");
private String code;
private String name;
Gender(String code, String name) {
this.code = code;
this.name = name;
}
}
@Data
@Builder
class UserPO {
private Long id;
private String name;
private LocalDateTime birthday;
private Gender gender;
}
@Data
class UserDTO {
private Long id;
private String name;
private LocalDateTime birthday;
private String genderCode;
private String locale;
}
@Data
@AllArgsConstructor
class Person {
private String name;
}
@Data
@AllArgsConstructor
class Animal {
private Long id;
}