利用Dozer工具类进行类的转换

一.先导入相关的依赖

<!--引入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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值