1.配置
1.3.0.Final
org.mapstruct mapstruct${org.mapstruct.version} org.mapstruct mapstruct-processor${org.mapstruct.version} providedorg.projectlombok lombok
2.编写代码 【mapstruct+lombok实现DO到DTO映射】DO:
@Data @AllArgsConstructor public class User { private String userName; private Integer age; private String address; private Integer sex; private Date birthday; }
DTO:
@Data public class UserDto { private String name; private Integer age; private String sex; private String addr; private String birthday; }
MAPPER:
@Mapper public interface UserMapper {UserMapper INSTANCE = Mappers.getMapper(UserMapper.class); @Mappings({ @Mapping(source = "userName",target = "name"), @Mapping(source = "address",target = "addr"), @Mapping(source = "birthday",target = "birthday",dateFormat = "yyyy-MM-dd HH:mm:ss") }) UserDto userToUserDto(User user); }
测试:
@Test public void test() { User user = new User("xm",20,"sz",1,new Date()); UserDto userDto = UserMapper.INSTANCE.userToUserDto(user); System.out.println(userDto); }
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)