四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)
Posted 上善若水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)相关的知识,希望对你有一定的参考价值。
一、pd-tools-dozer
pd-tools-dozer
模块定位为对象转换,其本质就是一个Spring Boot starter,其他模块可以直接导入此模块就可以直接完成对象转换了。
1.1、dozer介绍
Dozer
是Java Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象。dozer是用来对两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的调用set和get方法了。dozer其实是对我们熟知的beanutils的封装。
dozer的maven坐标:
<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>
1.2、dozer入门案例
第一步:创建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.xbmu</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
package com.xbmu.dto;
import lombok.Data;
@Data
public class UserDTO
private String userId;
private String userName;
private int userAge;
private String address;
private String birthday;
package com.xbmu.entity;
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;
第三步:在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-mapping
http://dozermapper.github.io/schema/bean-mapping.xsd">
<!--描述两个类中属性的对应关系,对于两个类中同名的属性可以不映射-->
<mapping date-format="yyyy-MM-dd">
<class-a>com.xbmu.entity.UserEntity</class-a>
<class-b>com.xbmu.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.xbmu.entity.UserEntity</class-a>
<class-b>com.xbmu.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:
mapping-files:
- classpath:dozer/global.dozer.xml
- classpath:dozer/biz.dozer.xml
第六步:编写启动类DozerApp
package com.xbmu;
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);
第七步:编写单元测试DozerTest
package com.xbmu.test;
import com.github.dozermapper.core.Mapper;
import com.xbmu.DozerApp;
import com.xbmu.dto.UserDTO;
import com.xbmu.entity.UserEntity;
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("bob");
userDTO.setUserAge(20);
userDTO.setAddress("bj");
userDTO.setBirthday("2010-11-20");
UserEntity user = mapper.map(userDTO, UserEntity.class);
System.out.println(user);// UserEntity(id=100, name=bob, 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("bob");
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=bob, 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("bob");
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=bob, age=20, address=bj, birthday=null)
1.3、pd-tools-dozer使用
在pd-tools-dozer
模块中为了进一步简化操作,封装了一个工具类DozerUtils
,其内部使用的就是Mapper对象进行的操作。并且按照Spring Boot starter的规范编写 /resources/META-INF/spring.factories
文件,内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
com.itheima.pinda.dozer.DozerAutoConfiguration
在配置类DozerAutoConfiguration
中完成DozerUtils
对象的创建,这样其他的程序如果需要使用dozer进行对象转换,只需要引入这个模块的maven坐标并且提供对应的映射文件,就可以在程序中直接注入DozerUtils
对象进行操作了。
具体使用过程:
第一步:创建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.xbmu</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>
第二步:创建UserEntity
和UserDTO
package com.xbmu.entity;
import lombok.Data;
@Data
public class UserEntity
private Integer id;
private String name;
private int age;
package com.xbmu.dto;
import lombok.Data;
@Data
public class UserDTO
private Integer id;
private String name;
private int age;
第三步:创建UserController
package com.xbmu.controller;
import com.xbmu.dto.UserDTO;
import com.xbmu.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("itcast");
userDTO.setAge(20);
UserEntity userEntity = dozerUtils.map(userDTO, UserEntity.class);
return userEntity;
第四步:创建application.yml
server:
port: 8080
第五步:创建启动类
package com.xbmu;
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);以上是关于四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)的主要内容,如果未能解决你的问题,请参考以下文章
四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)