推荐一款 Java 对象映射神器!
JAVA公众号
共 5798字,需浏览 12分钟
·
2020-11-27 03:52
点击上方“JAVA”,星标公众号
前言
调用getter/setter方法进行属性赋值 调用BeanUtil.copyPropertie进行反射属性赋值
第一种方式不必说,属性多了就需要写一大坨getter/setter代码。第二种方式比第一种方式要简便很多,但是坑巨多,比如sources与target写反,难以定位某个字段在哪里进行的赋值,同时因为用到反射,导致性能也不佳。
准备工作
@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 List config;
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
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 List strConfigToListUserConfig(String config) {
return JSON.parseArray(config, UserConfig.class);
}
default String listUserConfigToStrConfig(List list) {
return JSON.toJSONString(list);
}
}
二、自定义属性类型转换方法
default List strConfigToListUserConfig(String config) {
return JSON.parseArray(config, UserConfig.class);
}
default String listUserConfigToStrConfig(List list) {
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);
}
四、常见问题
代码下载
参考
https://mapstruct.org/documentation/stable/reference/html/
https://mapstruct.org/faq/
https://github.com/mapstruct/mapstruct-examples
http://www.kailing.pub/MapStruct1.3/index.html
https://www.cnblogs.com/javaguide/p/11861749.html
最近熬夜给大家准备了515套Java代码,有一些是业务类的小项目,比如Java博客项目,也有脚手架、也有平时用一些的工具类、21套小程序代码,也有一些游戏类的项目。
扫以下二维码并回复“828”即可获取
或者在本公众号对话框回复【828】马上获取
评论