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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值