本文主要是介绍dozer的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
dozer简介
Dozer是Java Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象。 dozer是用来对两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的调用set和get方法了。dozer其实是对我们熟知的beanutils的封装。
<?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>cn.dozer</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>
第二步:创建UserDTO和UserEntity
@Data
public class UserDTO {private String userId;private String userName;private int userAge;private String address;private String birthday;
}
@Data
public class UserEntity {private String id;private String name;private int age;private String address;private Date birthday;
}
第三步:在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>
第四步:在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-mappinghttp://dozermapper.github.io/schema/bean-mapping.xsd"><!--描述两个类中属性的对应关系,对于两个类中同名的属性可以不映射--><mapping date-format="yyyy-MM-dd"><class-a>com.dozer.entity.UserEntity</class-a><class-b>com.dozer.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.dozer.entity.UserEntity</class-a><class-b>com.dozer.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>
第五步:编写application.yml文件
dozer:mappingFiles:- classpath:dozer/global.dozer.xml- classpath:dozer/biz.dozer.xml
这篇关于dozer的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!