java-使用mapstruct复制两bean

前情回顾

在业务中会经常遇到两个bean会复制引用的,通常使用BeanUtils这个类。

	package com.example.demo.mapstruct;

import lombok.Data;

@Data
public class Order {

    /**
     *订单id
     */
    private Long id;

    /**
     * 订单编号
     */
    private String orderSn;

    /**
     * 收货人姓名/号码
     */
    private String receiverKeyword;

    /**
     * 订单状态:0->待付款;1->待发货;2->已发货;3->已完成;4->已关闭;5->无效订单
     */
    private Integer status;

    /**
     * 订单类型:0->正常订单;1->秒杀订单
     */
    private Integer orderType;

    /**
     * 订单来源:0->PC订单;1->app订单
     */
    private Integer sourceType;
    private Map mapList;
}



package com.example.demo.mapstruct;

import lombok.Data;

@Data
public class OrderParam {
    /**
     * 订单编号
     */
    private String orderSn;

    /**
     * 收货人姓名/号码
     */
    private String receiverKeyword;

    /**
     * 订单状态:0->待付款;1->待发货;2->已发货;3->已完成;4->已关闭;5->无效订单
     */
    private Integer status;

    /**
     * 订单类型:0->正常订单;1->秒杀订单
     */
    private Integer orderType;

    /**
     * 订单来源:0->PC订单;1->app订单
     */
    private Integer sourceType;

    private Integer orderId;
    private Map mapList;
}


@SpringBootTest
class DemoApplicationTests {

   

    @Test
    public void testBeanUtils(){
        Order order = new Order();
        order.setId(12345L);
        order.setOrderSn("orderSn");
        order.setOrderType(0);
        order.setReceiverKeyword("keyword");
        order.setSourceType(1);
        order.setStatus(2);
        System.out.println("使用Beanutils");
        OrderParam orderParam1 = new OrderParam();
        BeanUtils.copyProperties(order,orderParam1);
        System.out.println(orderParam1);
		/*
		*使用Beanutils
OrderParam(orderSn=orderSn, receiverKeyword=keyword, status=2, orderType=0, sourceType=1, orderId=null)

		*/
    }

  
}


当两bean属性名不一致时就不会进行赋值,如orderId

而这个MapSuruct可以搞定这些.
首先要引用:
poml文件:

<dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.3.0.Final</version>
        </dependency>

接下来是mapper映射类

package com.example.demo.mapstruct;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public interface OrderMapper {

    @Mapping(source = "order.id",target = "orderId")
    OrderParam entity2queryParam(Order order);
}



test类:


@Test
    public void testMapstruct(){
        Order order = new Order();
        order.setId(12345L);
        order.setOrderSn("orderSn");
        order.setOrderType(0);
        order.setReceiverKeyword("keyword");
        order.setSourceType(1);
        order.setStatus(2);
        Map map=new HashMap();
        Map map1=new HashMap();
        map1.put("test","我是test1");
        map.put("test","我是test");
        map.put("map",map1);
        order.setMapList(map);
        System.out.println("使用Beanutils");
        OrderParam orderParam1 = new OrderParam();
        BeanUtils.copyProperties(order,orderParam1);
        orderParam1.setStatus(44);
//        orderParam1.setMapList(map1);
        System.out.println(orderParam1);
        System.out.println(order);
        System.out.println("使用mapstruct");
        OrderMapper mapper = Mappers.getMapper(OrderMapper.class);
        OrderParam orderParam = mapper.entity2queryParam(order);
        System.out.println(orderParam);
    }

你可能感兴趣的:(java)