1、简介
Dozer(http://dozer.sourceforge.net/) 是 Java Bean 到 Java Bean 的映射器,他以递归的方式将数据从一个对象复制到另一个对象。Dozer 支持简单属性映射、双向映射、隐式映射以及递归映射。使用该映射器可以很方便的在项目中进行 pojo、do、vo 之间的转换。
2、入门
2.1 搭建环境
创建一个maven项目,导入下列依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/com.github.dozermapper/dozer-core -->
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>6.5.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
2.2 创建实体类
创建两个实体类User
和UserVO
@Data
public class User {
private String name;
private Integer age;
}
@Data
public class UserVO {
private String name;
private Integer age;
private String loginId;
private String pwd;
}
编写测试类
public static void main(String[] args) {
//使用构造器创建映射器
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
//创建User对象
User user = new User();
user.setName("张三");
user.setAge(23);
//第一种方式
//UserVO userVO = mapper.map(user, UserVO.class);
//第二种方式
UserVO userVO = new UserVO();
mapper.map(user,userVO);
System.out.println(userVO);//UserVO(name=张三, age=23, loginId=null, pwd=null)
}
可以看到,user的name和age都被复制到了userVO中,但是,需要注意的是,二者对应的属性名必须一样,否则无法正常映射。
那么,如果属性名相同,但是类型不同,可以进行映射吗?我们将UserVO的age改为String
类型的,再次进行测试:
private String age;
可以发现,是可以进行映射的,那我们再来看一下Dozer在遇到属性名的大小写问题是否依旧可以正常转换呢?我们将UserVo的name属性名改为首字母大写看看(不推荐这样搞得,只是玩玩的):
private String Name;
2.3 通过xml进行映射
有的时候,两个实体的属性名不一样,这个时候就不能使用Dozer提供的隐式映射了,我们需要使用Dozer提供的显式映射–通过xml配置两个实体的映射关系。
首先,我们先对两个实体类修改一下:
@Data
public class User {
private String name;
private String pwd;
private Date birth;
}
@Data
public class UserVO {
private String username;
private String password;
private Date birthday;
}
接着,在resources目录下创建userMapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
<!--全局配置-->
<configuration>
<date-format>yyyy-MM-dd</date-format>
</configuration>
<!--配置映射关系-->
<mapping>
<class-a>com.hzp.entity.User</class-a>
<class-b>com.hzp.entity.UserVO</class-b>
<field>
<a>name</a>
<b>username</b>
</field>
<field>
<a>pwd</a>
<b>password</b>
</field>
<field>
<a>birth</a>
<b>birthday</b>
</field>
</mapping>
</mappings>
最后,编写测试类:
public static void main(String[] args) {
Mapper mapper = DozerBeanMapperBuilder.create().withMappingFiles("userMapping.xml").build();
User user = new User();
user.setName("张三");
user.setPwd("123456");
user.setBirth(new Date());
UserVO userVO = mapper.map(user, UserVO.class);
System.out.println(userVO);//UserVO(username=张三, password=123456, birthday=Thu Nov 12 20:44:50 CST 2020)
}
2.4 使用注解进行映射
从版本 5.3.2 开始,Dozer 也开始提供注解支持,使用注释的明显原因是避免在映射代码中复制字段和方法名称,注释可以放在映射类的属性上,从而减少代码量。下面来看具体的使用:
@Data
public class UserVO {
@Mapping("name")
private String username;
@Mapping("pwd")
private String password;
@Mapping("birth")
private Date birthday;
}
注意:Dozer是双向映射的,所以我们只需要在一方配置即可
测试一波:
public static void main(String[] args) {
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
User user = new User();
user.setName("张三");
user.setPwd("123456");
user.setBirth(new Date());
UserVO userVO = mapper.map(user, UserVO.class);
System.out.println(userVO);//UserVO(username=张三, password=123456, birthday=Thu Nov 12 20:44:50 CST 2020)
}
3、与SpringBoot集成
3.1 搭建环境
创建一个SpringBoot项目,导入下列依赖:
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-spring-boot-starter</artifactId>
<version>6.5.0</version>
</dependency>
接着创建DozerMapperConfig
配置类
@Configuration
public class DozerMapperConfig {
@Bean
public DozerBeanMapperFactoryBean dozerMapper(@Value("classpath:mappings/*.xml") Resource[] resources) throws IOException {
DozerBeanMapperFactoryBean dozerBeanMapperFactoryBean = new DozerBeanMapperFactoryBean();
dozerBeanMapperFactoryBean.setMappingFiles(resources);
return dozerBeanMapperFactoryBean;
}
}
接着,在resources目录下新建mappings目录,并创建userMapping.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
<!--配置映射关系-->
<mapping>
<class-a>com.hzp.dozerboot.entity.User</class-a>
<class-b>com.hzp.dozerboot.entity.UserVO</class-b>
<field>
<a>name</a>
<b>username</b>
</field>
<field>
<a>pwd</a>
<b>password</b>
</field>
</mapping>
</mappings>
测试
@SpringBootTest
@RunWith(SpringRunner.class)
class DozerBootApplicationTests {
@Autowired
private Mapper mapper;
@Test
public void dozerTest() {
User user = new User();
user.setName("张三");
user.setPwd("123456");
UserVO userVO = mapper.map(user, UserVO.class);
System.out.println(userVO);//UserVO(username=张三, password=123456)
}
}