Entity到Vo的转换

本文介绍了如何在Java项目中使用MapStruct库来实现对象之间的映射转换。通过在`pom.xml`中引入MapStruct依赖,创建自定义接口并使用注解定义转换规则,以及在测试类中验证转换功能,展示了从`Product`到`ProductVO`,`Category`到`OrderVO`的转换过程。
摘要由CSDN通过智能技术生成

entity —》 VO

1、pom.xml 导入依赖

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>1.2.0.CR1</version>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.2.0.CR1</version>
    <scope>provided</scope>
</dependency>

2、自定义接口

package com.southwind.mapper;

import com.southwind.entity.Category;
import com.southwind.entity.Product;
import com.southwind.vo.OrderVO;
import com.southwind.vo.ProductVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface ProductMapper {

    ProductMapper mapper = Mappers.getMapper(ProductMapper.class);

    @Mappings({
            @Mapping(source = "productId",target = "id"),
            @Mapping(source = "productName",target = "name"),
            @Mapping(source = "productPrice",target = "price"),
            @Mapping(source = "productStatus",target = "status",qualifiedByName = "convertStatus")
    })
    ProductVO pojo2vo(Product product);

    @Named("convertStatus")
    default Boolean convertStatus(Integer status){
        if(status == 1)return true;
        return false;
    }

    List<ProductVO> list2vo(List<Product> list);

    @Mappings({
            @Mapping(source = "product.productName",target = "product"),
            @Mapping(source = "category.categoryName",target = "category")
    })
    OrderVO createOrder(Product product, Category category);
}

3、测试类

package com.southwind.mapper;

import com.southwind.entity.Category;
import com.southwind.entity.Product;
import com.southwind.vo.OrderVO;
import com.southwind.vo.ProductVO;
import org.junit.Test;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class VOTest {

    @Test
    public void test() {
        Product product = new Product(1,"电脑", new BigDecimal(6000),1);
        ProductVO productVO = ProductMapper.mapper.pojo2vo(product);
        System.out.println(productVO);
    }

    @Test
    public void list(){
        List<Product> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list.add(new Product(i,"电脑"+i,new BigDecimal(6000),1));
        }
        List<ProductVO> productVOS = ProductMapper.mapper.list2vo(list);
        for (ProductVO productVO : productVOS) {
            System.out.println(productVO);
        }
    }

    @Test
    public void order(){
        Product product = new Product(1, "电脑", new BigDecimal(6000),1);
        Category category = new Category("电子产品");
        OrderVO order = ProductMapper.mapper.createOrder(product, category);
        System.out.println(order);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值