Java dozer对象转换
小钟要学习!!! 人气:0Java中的dozer对象转换
1、dozer介绍
Dozer是Java Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象。 dozer是用来对两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的调用set和get方法了。dozer其实是对我们熟知的beanutils的封装。
2、依赖坐标
<dependency> <groupId>com.github.dozermapper</groupId> <artifactId>dozer-core</artifactId> <version>6.5.0</version> </dependency>
为了简化使用方式,dozer还提供了starter,其maven坐标为:
<dependency> <groupId>com.github.dozermapper</groupId> <artifactId>dozer-spring-boot-starter</artifactId> <version>6.5.0</version> </dependency>
3、创建测试工厂【dozer_demo】
3.1、引入对应的依赖
<?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>org.example</groupId> <artifactId>dozer_demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <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>
3.2、创建UserDTO和UserEntity
dto:用户后端与前端之间的数据交互或后端之间的数据交互
entity:对应的是数据库列字段属性
package com.zcl.dto; import lombok.Data; /** * 项目名称:dozer_demo * 描述:用户数据传输对象 * * @author zhong * @date 2022-08-29 10:41 */ @Data public class UserDTO { private String userId; private String userName; private int userAge; private String address; private String birthday; }
实际项目中的属性命名可能是一致的,而下面的使用属性故意设置为个别属性不一致,测试属性名不一致的时候如何进行对象的转换
package com.zcl.UserEntity; import lombok.Data; import java.util.Date; /** * 项目名称:dozer_demo * 描述:用户数据实体 * * @author zhong * @date 2022-08-29 10:42 */ @Data public class UserEntity { private String id; private String name; private int age; private String address; private Date birthday; }
3.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>
3.4、在resources/dozer/目录下创建dozer的映射文件biz.dozer.xml
该映射文件的主要作用是:将两个JavaBean不同的属性字段进行描述(谁对应谁)的映射关系
文件名称可以自定义date-format="yyyy-MM-dd"
是上一个配置文件所定义的全局格式
下面的配置文件中通过定义配置文件的全路径名称来指定,如<class-a>com.itheima.entity.UserEntity</class-a>
通过field
来指定两个对象之间的映射关系
<?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.zcl.entity.UserEntity</class-a> <class-b>com.zcl.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.zcl.entity.UserEntity</class-a> <class-b>com.zcl.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>
3.5、编写application.yml文件
如果不在配置文件中指定的化,上面编写的两个映射关系配置文件都不会生效
dozer: mappingFiles: - classpath:dozer/global.dozer.xml - classpath:dozer/biz.dozer.xml
3.6、编写项目启动类
package com.zcl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 项目名称:dozer_demo * 描述:启动类 * * @author zhong * @date 2022-08-29 11:01 */ @SpringBootApplication public class DozerApp { public static void main(String[] args) { SpringApplication.run(DozerApp.class, args); } }
3.7、编写单元测试
package com.zcl.test; import com.github.dozermapper.core.Mapper; import com.zcl.DozerApp; import com.zcl.dto.UserDTO; import com.zcl.entity.UserEntity; import org.junit.jupiter.api.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; /** * 项目名称:dozer_demo * 描述:测试类 * * @author zhong * @date 2022-08-29 11:03 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = DozerApp.class) public class DozerTest { /** * 注入dozer通过的对象,进行两个对象之间的复制 */ @Autowired private Mapper mapper; @Test public void testDozer1(){ UserDTO userDTO = new UserDTO(); userDTO.setUserId("100"); userDTO.setUserName("itcast"); userDTO.setUserAge(20); userDTO.setAddress("bj"); userDTO.setBirthday("2010-11-20"); // 调用map方法进行复制(原数据,复制的目标对象) UserEntity user = mapper.map(userDTO, UserEntity.class); System.out.println(user); // UserEntity(id=100, name=itcast, age=20, address=bj, birthday=Sat Nov 20 00:00:00 CST 2010) } @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"); System.out.println(user); // UserEntity(id=200, name=null, age=0, address=null, birthday=null) mapper.map(userDTO,user); System.out.println(user); // UserEntity(id=100, name=itcast, age=20, address=bj, birthday=Sat Nov 20 00:00:00 CST 2010) } @Test public void testDozer3(){ UserDTO userDTO = new UserDTO(); userDTO.setUserId("100"); userDTO.setUserName("itcast"); userDTO.setUserAge(20); userDTO.setAddress("bj"); UserEntity user = new UserEntity(); System.out.println(user); // UserEntity(id=null, name=null, age=0, address=null, birthday=null) mapper.map(userDTO,user,"user"); System.out.println(user); // UserEntity(id=100, name=itcast, age=20, address=bj, birthday=null) } }
加载全部内容