SpringBoot2.x集成Dozer的示例代码

目录

  • 一、引入依赖
  • 二、实体类
  • 三、编写配置文件
  • 四、创建测试类
  • 五、Dozer的基本使用
  • 代码示例
Dozer是Java Bean到Java Bean的映射器,它以递归的方式将数据从一个对象复制到另一个对象。通常,这些Java Bean将具有不同的复杂类型。它支持简单属性映射,复杂类型映射,双向映射,隐式显式映射,以及递归映射。这包括映射需要在元素层面上进行映射的集合属性。可以将Dozer用作两个对象之间属性转换的工具,使用它可以很方便地对项目中的DO、DTO、VO进行相互转换。
本文主要对SpringBoot2.x集成Dozer及其基本使用进行简单总结,其中SpringBoot使用的2.4.5版本。

一、引入依赖
com.github.dozermapperdozer-spring-boot-starter6.5.0org.springframework.bootspring-boot-starter-testtestorg.projectlomboklombok1.18.8


二、实体类 User类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; import java.util.Date; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.User * @description User * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class User {private Long id; private String name; private Integer age; private String gender; private String email; private Date birthday; }

UserDTO类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.UserDTO * @description UserDTO * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class UserDTO {private Long userId; private String userName; private Integer userAge; private String gender; private String email; private String birthday; }


三、编写配置文件 resources/dozer/目录下创建Dozer的全局配置文件global-dozer.xml
yyyy/MM/dd HH:mm:ss

resources/dozer/目录下创建Dozer的映射文件dozer.xml
com.rtxtitanv.model.Usercom.rtxtitanv.model.UserDTOiduserIdnameuserNameageuserAge

resources目录下创建application.yml配置文件:
dozer:# 指定Dozer的映射配置文件位置mapping-files:- classpath:dozer/global-dozer.xml- classpath:dozer/dozer.xml


四、创建测试类 创建单元测试类DozerTest
package com.rtxtitanv; import com.github.dozermapper.core.Mapper; import com.rtxtitanv.model.*; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.DozerTest * @description Dozer单元测试类 * @date 2021/8/18 16:44 */@Slf4j@SpringBootTestclass DozerTest {@Resourceprivate Mapper mapper; @Testvoid test1() {UserDTO userDTO = new UserDTO(); userDTO.setUserId(1L).setUserName("ZhaoYun").setGender("男").setUserAge(20).setEmail("zhaoyun@xxx.com").setBirthday("2001/8/18 18:05:32"); User user = mapper.map(userDTO, User.class); log.info(user.toString()); UserDTO userDTO2 = mapper.map(user, UserDTO.class); log.info(userDTO2.toString()); }}

执行测试方法,发现User和UserDTO相互转换成功:

SpringBoot2.x集成Dozer的示例代码
文章图片


五、Dozer的基本使用 下面对Dozer的一些基本使用进行总结。Dozer支持注解、API、XML三种映射配置方式,XML方式比较常用,前面使用的也是XML映射配置方式。XML映射配置中mapping元素的map-id属性可以设置该映射的标识,通过此标识来确定使用该映射关系。
dozer.xml中新增以下配置:
com.rtxtitanv.model.Usercom.rtxtitanv.model.UserDTOiduserIdnameuserNameageuserAge

新增以下测试方法:
@Testvoid test2() {UserDTO userDTO = new UserDTO(); userDTO.setUserId(2L).setUserName("MaChao").setGender("男").setUserAge(21).setEmail("machao@xxx.com").setBirthday("2000/6/15 08:45:20"); User user = mapper.map(userDTO, User.class, "user"); log.info(user.toString()); }

执行测试方法,发现转换成功:

SpringBoot2.x集成Dozer的示例代码
文章图片


在调用map方法时也可以直接指定要转换的目标对象。新增以下测试方法:
@Testvoid test3() {UserDTO userDTO = new UserDTO(); userDTO.setUserId(3L).setUserName("LiuBei").setGender("男").setUserAge(30).setEmail("liubei@xxx.com").setBirthday("1991/1/20 13:36:55"); User user = new User(); mapper.map(userDTO, user, "user"); log.info(user.toString()); }

执行测试方法,发现转换成功:

SpringBoot2.x集成Dozer的示例代码
文章图片


通过field-exclude标签可以设置不想进行转换的属性,这些属性在进行转换时会被自动排除。
dozer.xml中新增以下配置:
com.rtxtitanv.model.Usercom.rtxtitanv.model.UserDTOiduserIdnameuserNameageuserAgeemailemail

新增以下测试方法:
@Testvoid test4() {UserDTO userDTO = new UserDTO(); userDTO.setUserId(1L).setUserName("ZhaoYun").setGender("男").setUserAge(20).setEmail("zhaoyun@xxx.com").setBirthday("2001/8/18 18:05:32"); User user = mapper.map(userDTO, User.class, "user-exclude"); log.info(user.toString()); }

执行测试方法,发现email属性被成功排除:

SpringBoot2.x集成Dozer的示例代码
文章图片

Dozer中的映射方式默认都是双向映射,如果想让转换不可逆,即只需要单向转换,可以设置mapping元素的type属性为one-way来开启单向映射。
dozer.xml中新增以下配置:
com.rtxtitanv.model.UserDTOcom.rtxtitanv.model.UseruserIdiduserNamenameuserAgeage

新增以下测试方法:
@Testvoid test5() {UserDTO userDTO = new UserDTO(); userDTO.setUserId(1L).setUserName("ZhaoYun").setGender("男").setUserAge(20).setEmail("zhaoyun@xxx.com").setBirthday("2001/8/18 18:05:32"); User user = mapper.map(userDTO, User.class, "user-oneway"); log.info(user.toString()); UserDTO userDTO2 = mapper.map(user, UserDTO.class, "user-oneway"); log.info(userDTO2.toString()); }

执行测试方法,发现只有UserDTO转换为User成功:

SpringBoot2.x集成Dozer的示例代码
文章图片


当两个实体类中都嵌套有能够互相转换的实体类型属性时,也可以进行相互转换。
创建Order类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.Order * @description Order * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class Order {private Long id; private String number; private String description; private User user; }

创建OrderDTO类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.OrderDTO * @description OrderDTO * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class OrderDTO {private Long orderId; private String orderNumber; private String orderDescription; private UserDTO userDTO; }

dozer.xml中新增以下配置:
com.rtxtitanv.model.Ordercom.rtxtitanv.model.OrderDTOidorderIdnumberorderNumberdescriptionorderDescriptionuseruserDTO

新增以下测试方法:
@Testvoid test6() {OrderDTO orderDTO = new OrderDTO(); UserDTO userDTO = new UserDTO().setUserId(6L).setUserName("DiaoChan").setGender("女").setUserAge(18).setEmail("diaochan@xxx.com").setBirthday("2003/12/27 23:10:36"); orderDTO.setOrderId(1L).setOrderNumber("78956328").setOrderDescription("二两麻辣牛肉面").setUserDTO(userDTO); Order order = mapper.map(orderDTO, Order.class, "order"); log.info(order.toString()); OrderDTO orderDTO2 = mapper.map(order, OrderDTO.class, "order"); log.info(orderDTO2.toString()); }

执行测试方法,发现Order和OrderDTO相互转换成功:

SpringBoot2.x集成Dozer的示例代码
文章图片


Dozer还可以对深层属性进行映射,即深度映射。例如一个对象中的String类型属性可以与另一个对象中嵌套的对象的属性进行映射。
创建UserInfo类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.UserInfo * @description UserInfo * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class UserInfo {private String gender; private String email; private String birthday; }

创建UserInfoDTO类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.UserInfoDTO * @description UserInfoDTO * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class UserInfoDTO {private Long userId; private String userName; private Integer userAge; private UserInfo userInfo; }

dozer.xml中新增以下配置:
com.rtxtitanv.model.UserInfoDTOcom.rtxtitanv.model.UseruserIdiduserNamenameuserAgeageuserInfo.gendergenderuserInfo.emailemailuserInfo.birthdaybirthday

新增以下测试方法:
@Testvoid test7() {UserInfo userInfo = new UserInfo(); userInfo.setGender("男").setEmail("zhaoyun@xxx.com").setBirthday("2001/8/18 18:05:32"); UserInfoDTO userInfoDTO = new UserInfoDTO(); userInfoDTO.setUserId(1L).setUserName("ZhaoYun").setUserAge(20).setUserInfo(userInfo); User user = mapper.map(userInfoDTO, User.class, "user-deep-mapping"); log.info(user.toString()); }

执行测试方法,发现UserInfoDTO成功转换为User:

SpringBoot2.x集成Dozer的示例代码
文章图片


Dozer还支持注解方式配置映射,使用@Mapping注解可以进行一些简单的映射处理。
创建UserEntity类:
package com.rtxtitanv.model; import com.github.dozermapper.core.Mapping; import lombok.Data; import lombok.experimental.Accessors; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.UserEntity * @description UserEntity * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class UserEntity {@Mapping(value = "https://www.it610.com/article/userId")private Long id; @Mapping(value = "https://www.it610.com/article/userName")private String name; @Mapping(value = "https://www.it610.com/article/userAge")private Integer age; private String gender; private String email; private String birthday; }

@Mapping只需要在源类中指定目标类中对应的属性即可。
创建UserVO类:
package com.rtxtitanv.model; import lombok.Data; import lombok.experimental.Accessors; import java.util.Date; /** * @author rtxtitanv * @version 1.0.0 * @name com.rtxtitanv.model.UserVO * @description UserVO * @date 2021/8/18 16:45 */@Accessors(chain = true)@Datapublic class UserVO {private Long userId; private String userName; private Integer userAge; private String gender; private String email; private Date birthday; }

新增以下测试方法:
@Testvoid test8() {UserEntity userEntity = new UserEntity(); userEntity.setId(1L).setName("ZhaoYun").setGender("男").setAge(20).setEmail("zhaoyun@xxx.com").setBirthday("2001/8/18 18:05:32"); UserVO userVO = mapper.map(userEntity, UserVO.class); log.info(userVO.toString()); }

执行测试方法,发现转换成功:

SpringBoot2.x集成Dozer的示例代码
文章图片


代码示例 Github:https://github.com/RtxTitanV/springboot-learning/tree/master/springboot2.x-learning/springboot-dozer
Gitee:https://gitee.com/RtxTitanV/springboot-learning/tree/master/springboot2.x-learning/springboot-dozer
【SpringBoot2.x集成Dozer的示例代码】到此这篇关于SpringBoot2.x 集成 Dozer的文章就介绍到这了,更多相关SpringBoot2.x 集成 Dozer内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读