pd-tools-dozer模块定位为对象转换,其本质就是一个Spring Boot starter,其他模块可以直接导入此模块就可以直接完成对象转换了。
Dozer是Java Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象。 dozer是用来对两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的调用set和get方法了。dozer其实是对我们熟知的beanutils的封装。
为了简化使用方式,dozer还提供了starter,其maven坐标为:
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-spring-boot-starter</artifactId>
<version>6.5.0</version>
</dependency>
1,创建maven工程dozer_demo并配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.ben</groupId>
<artifactId>dozer_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-spring-boot-starter</artifactId>
<version>6.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2,创建UserDTO和UserEntity
import lombok.Data;
@Data
public class UserDTO {
private String userId;
private String userName;
private int userAge;
private String address;
private String birthday;
}
import lombok.Data;
import java.util.Date;
@Data
public class UserEntity {
private String id;
private String name;
private int age;
private String address;
private Date birthday;
}
3,在resources/dozer/目录下创建dozer的全局配置文件global.dozer.xml
注:全局配置文件名称可以任意
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://dozermapper.github.io/schema/bean-mapping"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping
http://dozermapper.github.io/schema/bean-mapping.xsd">
<!--
全局配置:
<date-format>表示日期格式
-->
<configuration>
<date-format>yyyy-MM-dd</date-format>
</configuration>
</mappings>
4,在resources/dozer/目录下创建dozer的映射文件biz.dozer.xml
注:映射文件名称可以任意
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://dozermapper.github.io/schema/bean-mapping"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping
http://dozermapper.github.io/schema/bean-mapping.xsd">
<!--描述两个类中属性的对应关系,对于两个类中同名的属性可以不映射-->
<mapping date-format="yyyy-MM-dd">
<class-a>com.ben.entity.UserEntity</class-a>
<class-b>com.ben.dto.UserDTO</class-b>
<field>
<a>id</a>
<b>userId</b>
</field>
<field>
<a>name</a>
<b>userName</b>
</field>
<field>
<a>age</a>
<b>userAge</b>
</field>
</mapping>
<!--
也可以使用map-id指定映射的标识,在程序中通过此标识来确定使用当前这个映射关系
-->
<mapping date-format="yyyy-MM-dd" map-id="user">
<class-a>com.ben.entity.UserEntity</class-a>
<class-b>com.ben.dto.UserDTO</class-b>
<field>
<a>id</a>
<b>userId</b>
</field>
<field>
<a>name</a>
<b>userName</b>
</field>
<field>
<a>age</a>
<b>userAge</b>
</field>
</mapping>
</mappings>
5,编写application.yml文件
dozer:
mappingFiles:
- classpath:dozer/global.dozer.xml
- classpath:dozer/biz.dozer.xml
6,编写启动类DozerApp
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DozerApp {
public static void main(String[] args) {
SpringApplication.run(DozerApp.class,args);
}
}
7,编写单元测试DozerTest
import com.ben.DozerApp;
import com.ben.dto.UserDTO;
import com.ben.entity.UserEntity;
import com.github.dozermapper.core.Mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DozerApp.class)
public class DozerTest {
@Autowired
private Mapper mapper;
@Test
public void testDozer1(){
UserDTO userDTO = new UserDTO();
userDTO.setUserId("100");
userDTO.setUserName("ben");
userDTO.setUserAge(20);
userDTO.setAddress("bj");
userDTO.setBirthday("2010-11-20");
UserEntity user = mapper.map(userDTO, UserEntity.class);
//UserEntity(id=100, name=ben, age=20, address=bj, birthday=Sat Nov 20 00:00:00 CST 2010)
System.out.println(user);
}
@Test
public void testDozer2(){
UserDTO userDTO = new UserDTO();
userDTO.setUserId("100");
userDTO.setUserName("itcast");
userDTO.setUserAge(20);
userDTO.setAddress("bj");
userDTO.setBirthday("2010-11-20");
UserEntity user = new UserEntity();
user.setId("200");
//UserEntity(id=200, name=null, age=0, address=null, birthday=null)
System.out.println(user);
mapper.map(userDTO,user);
//UserEntity(id=100, name=itcast, age=20, address=bj, birthday=Sat Nov 20 00:00:00 CST 2010)
System.out.println(user);
}
@Test
public void testDozer3(){
UserDTO userDTO = new UserDTO();
userDTO.setUserId("100");
userDTO.setUserName("itcast");
userDTO.setUserAge(20);
userDTO.setAddress("bj");
UserEntity user = new UserEntity();
//UserEntity(id=null, name=null, age=0, address=null, birthday=null)
System.out.println(user);
//"user"对应到biz.dozer.xml中map-id="user"
mapper.map(userDTO,user,"user");
//UserEntity(id=100, name=itcast, age=20, address=bj, birthday=null)
System.out.println(user);
}
}
在pd-tools-dozer模块中为了进一步简化操作,封装了一个工具类DozerUtils,其内部使用的就是Mapper对象进行的操作。并且按照Spring Boot starter的规范编写/resources/META-INF/spring.factories文件,内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pinda.dozer.DozerAutoConfiguration
在配置类DozerAutoConfiguration中完成DozerUtils对象的创建,这样其他的程序如果需要使用dozer进行对象转换,只需要引入这个模块的maven坐标并且提供对应的映射文件就可以在程序中直接注入DozerUtils对象进行操作了。
具体使用过程:
1,创建maven工程myDozerApp并配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.ben</groupId>
<artifactId>myDozerApp</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--引入我们自己定义的dozer基础模块-->
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-dozer</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2,创建UserEntity和UserDTO
import lombok.Data;
@Data
public class UserEntity {
private Integer id;
private String name;
private int age;
private String addr;
}
import lombok.Data;
@Data
public class UserDTO {
private Integer id;
private String name;
private int age;
private String address;
}
3,创建UserController
import com.ben.dto.UserDTO;
import com.ben.entity.UserEntity;
import com.itheima.pinda.dozer.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private DozerUtils dozerUtils; //在pd-tools-dozer中已经完成了自动配置,可以直接注入
@GetMapping("/mapper")
public UserEntity mapper(){
UserDTO userDTO = new UserDTO();
userDTO.setId(10);
userDTO.setName("ben");
userDTO.setAge(20);
userDTO.setAddress("xian");
UserEntity userEntity = dozerUtils.map(userDTO, UserEntity.class);
return userEntity;
}
}
4,创建application.yml和映射文件
server:
port: 8080
dozer:
mappingFiles:
- classpath:dozer/biz.dozer.xml
如果这两个类中的属性存在不一致的情况,需要创建映射文件进行映射,并且还需要在application.yml中配置映射文件的位置
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://dozermapper.github.io/schema/bean-mapping"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping
http://dozermapper.github.io/schema/bean-mapping.xsd">
<!--描述两个类中属性的对应关系,对于两个类中同名的属性可以不映射-->
<mapping date-format="yyyy-MM-dd">
<class-a>com.ben.entity.UserEntity</class-a>
<class-b>com.ben.dto.UserDTO</class-b>
<field>
<a>addr</a>
<b>address</b>
</field>
</mapping>
</mappings>
5,创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyDozerApplication {
public static void main(String[] args) {
SpringApplication.run(MyDozerApplication.class,args);
}
}
6,启动项目,访问地址:http://localhost:8080/user/mapper
{"id":10,"name":"ben","age":20,"addr":"xian"}