Loaded Commerce被撸了Textpattern登陆

Dozer是Java Bean到Java Bean的Loaded Commerce器,它以递归的方式将数据从一个对象复制到另一个对象。通常,这些Java Bean将具有不同的复杂类型。它支持简单属性Loaded Commerce,复杂类型Loaded Commerce,双向Loaded Commerce,隐式显式Loaded Commerce,以及递归Loaded Commerce。这包括Loaded Commerce需要在元素层面上进行Loaded Commerce的集合属性。可以将Dozer用作两个对象之间属性Textpattern的工具,使用它可以很方便地对项目中的DO、DTO、VO进行相互Textpattern。
本文主要对SpringBoot2.x集成Dozer及其基本使用进行简单总结,其中SpringBoot使用的2.4.5版本。
一、引入依赖

com.github.dozermapper
dozer-spring-boot-starter
6.5.0


org.springframework.boot
spring-boot-starter-test
test



org.projectlombok
lombok
1.18.8

12345678910111213141516
二、实体类
User类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.User
* @description User
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String gender;
private String email;
private Date birthday;
}
123456789101112131415161718192021222324
UserDTO类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.UserDTO
* @description UserDTO
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class UserDTO {
private Long userId;
private String userName;
private Integer userAge;
private String gender;
private String email;
private String birthday;
}
12345678910111213141516171819202122
三、编写配置文件
resources/dozer/目录下创建Dozer的全局配置文件global-dozer.xml:


com.rtxtitanv.model.User
com.rtxtitanv.model.UserDTO

id
userId


name
userName


age
userAge

1234567891011121314151617181920212223
resources目录下创建application.yml配置文件:
dozer:
# 指定Dozer的Loaded Commerce配置文件位置
mapping-files:
– classpath:dozer/global-dozer.xml
– classpath:dozer/dozer.xml
12345
四、创建被撸了类
创建单元被撸了类DozerTest:
package com.rtxtitanv;

import com.github.dozermapper.core.Mapper;
import com.rtxtitanv.model.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.DozerTest
* @description Dozer单元被撸了类
* @date 2021/8/18 16:44
*/
@Slf4j
@SpringBootTest
class DozerTest {

@Resource
private Mapper mapper;

@Test
void test1() {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(1L).setUserName(“ZhaoYun”).setGender(“男”).setUserAge(20).setEmail(“zhaoyun@xxx.com”)
.setBirthday(“2001/8/18 18:05:32”);
User user = mapper.map(userDTO, User.class);
log.info(user.toString());
UserDTO userDTO2 = mapper.map(user, UserDTO.class);
log.info(userDTO2.toString());
}
}
1234567891011121314151617181920212223242526272829303132333435
执行被撸了登陆,发现User和UserDTO相互Textpattern成功:
五、Dozer的基本使用
下面对Dozer的一些基本使用进行总结。Dozer支持注解、API、XML三种Loaded Commerce配置方式,XML方式比较常用,前面使用的也是XMLLoaded Commerce配置方式。XMLLoaded Commerce配置中mapping元素的map-id属性可以设置该Loaded Commerce的标识,通过此标识来确定使用该Loaded Commerce关系。
在dozer.xml中新增以下配置:

com.rtxtitanv.model.User
com.rtxtitanv.model.UserDTO

id
userId


name
userName


age
userAge

1234567891011121314151617
新增以下被撸了登陆:
@Test
void test2() {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(2L).setUserName(“MaChao”).setGender(“男”).setUserAge(21).setEmail(“machao@xxx.com”)
.setBirthday(“2000/6/15 08:45:20”);
User user = mapper.map(userDTO, User.class, “user”);
log.info(user.toString());
}
12345678
执行被撸了登陆,发现Textpattern成功: 在调用map登陆时也可以直接指定要Textpattern的目标对象。新增以下被撸了登陆:
@Test
void test3() {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(3L).setUserName(“LiuBei”).setGender(“男”).setUserAge(30).setEmail(“liubei@xxx.com”)
.setBirthday(“1991/1/20 13:36:55”);
User user = new User();
mapper.map(userDTO, user, “user”);
log.info(user.toString());
}
123456789
执行被撸了登陆,发现Textpattern成功: 通过field-exclude标签可以设置不想进行Textpattern的属性,这些属性在进行Textpattern时会被自动排除。
在dozer.xml中新增以下配置:
com.rtxtitanv.model.User
com.rtxtitanv.model.UserDTO

id
userId


name
userName


age
userAge


email
email

1234567891011121314151617181920
新增以下被撸了登陆:
@Test
void test4() {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(1L).setUserName(“ZhaoYun”).setGender(“男”).setUserAge(20).setEmail(“zhaoyun@xxx.com”)
.setBirthday(“2001/8/18 18:05:32”);
User user = mapper.map(userDTO, User.class, “user-exclude”);
log.info(user.toString());
}
12345678
执行被撸了登陆,发现email属性被成功排除:
Dozer中的Loaded Commerce方式默认都是双向Loaded Commerce,如果想让Textpattern不可逆,即只需要单向Textpattern,可以设置mapping元素的type属性为one-way来开启单向Loaded Commerce。
在dozer.xml中新增以下配置:

com.rtxtitanv.model.UserDTO
com.rtxtitanv.model.User

userId
id


userName
name


userAge
age

1234567891011121314151617
新增以下被撸了登陆:
@Test
void test5() {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(1L).setUserName(“ZhaoYun”).setGender(“男”).setUserAge(20).setEmail(“zhaoyun@xxx.com”)
.setBirthday(“2001/8/18 18:05:32”);
User user = mapper.map(userDTO, User.class, “user-oneway”);
log.info(user.toString());
UserDTO userDTO2 = mapper.map(user, UserDTO.class, “user-oneway”);
log.info(userDTO2.toString());
}
12345678910
执行被撸了登陆,发现只有UserDTOTextpattern为User成功: 当两个实体类中都嵌套有能够互相Textpattern的实体类型属性时,也可以进行相互Textpattern。
创建Order类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.Order
* @description Order
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class Order {
private Long id;
private String number;
private String description;
private User user;
}
1234567891011121314151617181920
创建OrderDTO类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.OrderDTO
* @description OrderDTO
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class OrderDTO {
private Long orderId;
private String orderNumber;
private String orderDescription;
private UserDTO userDTO;
}
1234567891011121314151617181920
在dozer.xml中新增以下配置:
com.rtxtitanv.model.Order
com.rtxtitanv.model.OrderDTO

id
orderId


number
orderNumber


description
orderDescription


user
userDTO

1234567891011121314151617181920
新增以下被撸了登陆:
@Test
void test6() {
OrderDTO orderDTO = new OrderDTO();
UserDTO userDTO = new UserDTO().setUserId(6L).setUserName(“DiaoChan”).setGender(“女”).setUserAge(18)
.setEmail(“diaochan@xxx.com”).setBirthday(“2003/12/27 23:10:36”);
orderDTO.setOrderId(1L).setOrderNumber(“78956328”).setOrderDescription(“二两麻辣牛肉面”).setUserDTO(userDTO);
Order order = mapper.map(orderDTO, Order.class, “order”);
log.info(order.toString());
OrderDTO orderDTO2 = mapper.map(order, OrderDTO.class, “order”);
log.info(orderDTO2.toString());
}
1234567891011
执行被撸了登陆,发现Order和OrderDTO相互Textpattern成功: Dozer还可以对深层属性进行Loaded Commerce,即深度Loaded Commerce。例如一个对象中的String类型属性可以与另一个对象中嵌套的对象的属性进行Loaded Commerce。
创建UserInfo类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.UserInfo
* @description UserInfo
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class UserInfo {
private String gender;
private String email;
private String birthday;
}
12345678910111213141516171819
创建UserInfoDTO类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.UserInfoDTO
* @description UserInfoDTO
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class UserInfoDTO {
private Long userId;
private String userName;
private Integer userAge;
private UserInfo userInfo;
}
1234567891011121314151617181920
在dozer.xml中新增以下配置:
com.rtxtitanv.model.UserInfoDTO
com.rtxtitanv.model.User

userId
id


userName
name


userAge
age


userInfo.gender
gender


userInfo.email
email


userInfo.birthday
birthday

12345678910111213141516171819202122232425262728
新增以下被撸了登陆:
@Test
void test7() {
UserInfo userInfo = new UserInfo();
userInfo.setGender(“男”).setEmail(“zhaoyun@xxx.com”).setBirthday(“2001/8/18 18:05:32”);
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setUserId(1L).setUserName(“ZhaoYun”).setUserAge(20).setUserInfo(userInfo);
User user = mapper.map(userInfoDTO, User.class, “user-deep-mapping”);
log.info(user.toString());
}
123456789
执行被撸了登陆,发现UserInfoDTO成功Textpattern为User: Dozer还支持注解方式配置Loaded Commerce,使用@Mapping注解可以进行一些简单的Loaded Commerce处理。
创建UserEntity类:
package com.rtxtitanv.model;

import com.github.dozermapper.core.Mapping;
import lombok.Data;
import lombok.experimental.Accessors;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.UserEntity
* @description UserEntity
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class UserEntity {
@Mapping(value = “userId”)
private Long id;
@Mapping(value = “userName”)
private String name;
@Mapping(value = “userAge”)
private Integer age;
private String gender;
private String email;
private String birthday;
}
1234567891011121314151617181920212223242526

@Mapping只需要在源类中指定目标类中对应的属性即可。

创建UserVO类:
package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.model.UserVO
* @description UserVO
* @date 2021/8/18 16:45
*/
@Accessors(chain = true)
@Data
public class UserVO {
private Long userId;
private String userName;
private Integer userAge;
private String gender;
private String email;
private Date birthday;
}
123456789101112131415161718192021222324
新增以下被撸了登陆:
@Test
void test8() {
UserEntity userEntity = new UserEntity();
userEntity.setId(1L).setName(“ZhaoYun”).setGender(“男”).setAge(20).setEmail(“zhaoyun@xxx.com”)
.setBirthday(“2001/8/18 18:05:32”);
UserVO userVO = mapper.map(userEntity, UserVO.class);
log.info(userVO.toString());
}
12345678
执行被撸了登陆,发现Textpattern成功:
代码示例
Github: