推荐一款 Java 对象映射神器
程序员的成长之路
共 5618字,需浏览 12分钟
·
2020-12-31 00:37
阅读本文大概需要 5 分钟。
来自:网络
调用getter/setter方法进行属性赋值 调用BeanUtil.copyPropertie进行反射属性赋值
准备工作
@Data
@Accessors(chain = true)
public class User {
private Long id;
private String username;
private String password;
private Integer sex;
private LocalDate birthday;
private LocalDateTime createTime;
private String config;
private String test;
}
@Data
@Accessors(chain = true)
public class UserVo {
private Long id;
private String username;
private String password;
private Integer gender;
private LocalDate birthday;
private String createTime;
private Listconfig;
private String test;
@Data
public static class UserConfig {
private String field1;
private Integer field2;
}
}
一、MapStruct 配置以及基础使用
<dependency>
<groupId>org.mapstructgroupId>
<artifactId>mapstructartifactId>
<version>1.3.1.Finalversion>
dependency>
<dependency>
<groupId>org.mapstructgroupId>
<artifactId>mapstruct-processorartifactId>
<version>1.3.1.Finalversion>
dependency>
@MapperConfig
public interface BaseMapping{
/**
* 映射同名属性
*/
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
TARGET sourceToTarget(SOURCE var1);
/**
* 反向,映射同名属性
*/
@InheritInverseConfiguration(name = "sourceToTarget")
SOURCE targetToSource(TARGET var1);
/**
* 映射同名属性,集合形式
*/
@InheritConfiguration(name = "sourceToTarget")
ListsourceToTarget(List var1);
/**
* 反向,映射同名属性,集合形式
*/
@InheritConfiguration(name = "targetToSource")
ListtargetToSource(List var1);
/**
* 映射同名属性,集合流形式
*/
List<TARGET> sourceToTarget(Stream<SOURCE> stream);
/**
* 反向,映射同名属性,集合流形式
*/
List<SOURCE> targetToSource(Stream<TARGET> stream);
}
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(componentModel = "spring")
public interface UserMapping extends BaseMapping<User, UserVo> {
@Mapping(target = "gender", source = "sex")
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Override
UserVo sourceToTarget(User var1);
@Mapping(target = "sex", source = "gender")
@Mapping(target = "password", ignore = true)
@Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Override
User targetToSource(UserVo var1);
default ListstrConfigToListUserConfig(String config) {
return JSON.parseArray(config, UserConfig.class);
}
default String listUserConfigToStrConfig(Listlist) {
return JSON.toJSONString(list);
}
}
二、自定义属性类型转换方法
default List strConfigToListUserConfig(String config) {
return JSON.parseArray(config, UserConfig.class);
}
default String listUserConfigToStrConfig(Listlist) {
return JSON.toJSONString(list);
}
三、单元测试
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class MapStructTest {
@Resource
private UserMapping userMapping;
@Test
public void tetDomain2DTO() {
User user = new User()
.setId(1L)
.setUsername("zhangsan")
.setSex(1)
.setPassword("abc123")
.setCreateTime(LocalDateTime.now())
.setBirthday(LocalDate.of(1999, 9, 27))
.setConfig("[{\"field1\":\"Test Field1\",\"field2\":500}]");
UserVo userVo = userMapping.sourceToTarget(user);
log.info("User: {}", user);
log.info("UserVo: {}", userVo);
}
@Test
public void testDTO2Domain() {
UserConfig userConfig = new UserConfig();
userConfig.setField1("Test Field1");
userConfig.setField2(500);
UserVo userVo = new UserVo()
.setId(1L)
.setUsername("zhangsan")
.setGender(2)
.setCreateTime("2020-01-18 15:32:54")
.setBirthday(LocalDate.of(1999, 9, 27))
.setConfig(Collections.singletonList(userConfig));
User user = userMapping.targetToSource(userVo);
log.info("UserVo: {}", userVo);
log.info("User: {}", user);
}
四、常见问题
代码下载
微信扫描二维码,关注我的公众号
朕已阅
评论