一.先导入相关的依赖
<!--引入dozer依赖-->
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>
<version>6.5.2</version>
</dependency>
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-spring-boot-starter</artifactId>
<version>6.5.2</version>
</dependency>
二.创建对应的工具类
@Component
public class DozerUtils {
private static Mapper dozerMapper;
@Autowired
public void setDozerMapper(Mapper dozerMapper) {
DozerUtils.dozerMapper = dozerMapper;
}
/**
* 构造新的destinationClass实例对象,通过source对象中的字段内容
* 映射到destinationClass实例对象中,并返回新的destinationClass实例对象。
*
* @param source 源数据对象
* @param destinationClass 要构造新的实例对象Class
*/
public static <T> T map(Object source, Class<T> destinationClass) {
return dozerMapper.map(source, destinationClass);
}
/**
* 将对象source的所有属性值拷贝到对象destination中.
*
* @param source 对象source
* @param destinationObject 对象destination
*/
public static void map(Object source, Object destinationObject) {
dozerMapper.map(source, destinationObject);
}
/**
* List数据源映射
*
* @param sourceList
* @param destinationClass
* @param <T>
* @return List<T>
*/
public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
List destinationList = new ArrayList<>();
for (Iterator iterator = sourceList.iterator(); iterator.hasNext(); ) {
Object sourceObject = iterator.next();
Object destinationObject = dozerMapper.map(sourceObject, destinationClass);
destinationList.add(destinationObject);
}
return destinationList;
}
}
别忘了添加相应的注解
三.可以在控制层controller中进行方法的调用了
我这里就举个例子:
1.这是直接将AdminUser实体类转换为AdminUserDTO类
AdminUser adminUser = DozerUtils.map(adminUserDTO, AdminUser.class);
2. 这是将简化了遍历过程,直接利用dozer将ProductDTO里面获取的字段传输给AdminProductVO所需要的字段
List<ProductDTO> list = productService.productSelect();
List<AdminProductVO> voList = DozerUtils.mapList(list, AdminProductVO.class);
(为了方便大家理解,我将ProductDTO和AdminProductDTO的内容做一下展示)
@Data
public class ProductDTO {
private Integer id;
private String name;
private String description;
private BigDecimal price;
private Integer count;
private String category;
/**
* 用于区别顾客和管理员身份的字段 顾客默认为 0 查看所有上架商品 管理员可选填0 1 , 0为上架 1为下架 null全看
*/
private Integer status;
/**
* 页码
*/
private Integer pageNum;
/**
* 每页的条数
*/
private Integer pageSize;
private Date createdDate;
private String createdUser;
private String modifiedUser;
private Date modifiedDate;
private Integer isDelete;
private Integer type;
/**
*图片信息
*/
private Integer imgId;
private Integer midId;
private String imgName;
private String domain;
private ImgInformationDTO productImginfo;
private List<ImgInformationDTO> productImginfos;
}
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AdminProductVO {
private Integer id;
private String name;
private String description;
private BigDecimal price;
private Integer count;
private Integer status;
private String category;
private List<ImgInformationDTO> productImginfos;
private String isDelete0;
public void setIsDelete(Integer isDelete){
if (isDelete==1){
this.isDelete0="已删除";
}else if (isDelete==0){
this.isDelete0 = "未删除";
}
}
}
注: 我这里的这段代码是为了实现在后端的一个类型转换
public void setIsDelete(Integer isDelete){
if (isDelete==1){
this.isDelete0="已删除";
}else if (isDelete==0){
this.isDelete0 = "未删除";
}
}
要注意区分isDelete和isDelete0
目的是将DTO获取的类型为Interget的isDelete字段获取后,展示在控制台为String类型的isDelete0