mapstruct 对象映射详解

前言

开发中,我们经常需要将PO转DTO、DTO转PO等一些实体间的转换。

比较出名的有BeanUtil 和ModelMapper等,它们使用简单,但是在稍显复杂的业务场景下力不从心。

MapStruct这个插件可以用来处理domin实体类与model类的属性映射,可配置性强。只需要定义一个 Mapper 接口,MapStruct 就会自动实现这个映射接口,避免了复杂繁琐的映射实现。

参考文档

MapStruct官网地址: http://mapstruct.org/

IDE 支持:https://mapstruct.org/documentation/ide-support

demo地址:https://github.com/herionZhang/mapstruct-demo

maven 依赖

	<dependency>
		<groupId>org.mapstruct</groupId>
		<artifactId>mapstruct</artifactId>
		<version>1.3.1.Final</version>
	</dependency>

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

入门

不使用spring示例

测试对象准备

public class Car {
 
    private String make;
    private int numberOfSeats;
 
    //constructor, getters, setters etc.
}


public class CarDto {

private String make;
private int seatCount;

//constructor, getters, setters etc.
}

mapper定义

@Mapper
public interface CarMapper {
 	//为客户端提供对映射器实现的访问。
    CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
 
    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto carToCarDto(Car car);
}

1、@Mapper项目编译时会生产对应实现类

2、@Mapping 用来指定属性映射的,如果两个对象的属性名相同是可以省略

编译后源码

package com.herion.example.demo.mappers;

import com.herion.example.demo.dto.CarDto;
import com.herion.example.demo.entity.Car;

import javax.annotation.Generated;

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-11-27T16:03:01+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_211 (Oracle Corporation)"
)
public class CarMapperImpl implements CarMapper {

@Override
public CarDto carToCarDto(Car car) {
if ( car == null ) {
return null;
}

CarDto carDto = new CarDto();

carDto.setSeatCount( car.getNumberOfSeats() );
carDto.setMake( car.getMake() );

return carDto;
}
}

源码位置
在这里插入图片描述

测试类

package com.example.demo.mappers;

import com.example.demo.dto.CarDto;
import com.example.demo.entity.Car;
import org.junit.Test;



public class CarMapperTest {

    @Test
    public void shouldMapCarToDto() {
        //given
        Car car = new Car("Morris", 5);

        //when
        CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );

        System.out.println(carDto.toString());
    }
}

测试结果

CarDto(make=Morris, seatCount=5)

进阶

以使用springboot为示例,@Mapper(componentModel = “spring”),表示把当前Mapper类纳入spring容器。

测试对象准备

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class User {
    private Long id;
    private String username;
    private String password;
    private String phoneNum;
    private String email;
    private Role role;
}



package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Role {
    private Long id;
    private String roleName;
    private String description;
}


package com.example.demo.dto;

import lombok.Data;

@Data
public class UserRoleDto {
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 用户名
     */
    private String name;
    /**
     * 角色名
     */
    private String roleName;
}


package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
    private Long id;
    private String name;
    private Boolean isDisable;

    private String email;

    private Date birthday;
}

对象属性复制

定义mapper

package com.example.demo.mappers;


import com.example.demo.dto.UserRoleDto;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;


@Mapper(componentModel = "spring")
public interface UserRoleMapper {

    /**
     * 对象属性复制的方法
     *
     * @param user 这个参数就是源对象,也就是需要被复制的对象
     * @return 返回的是目标对象,就是最终的结果对象
     * @Mapping 用来定义属性复制规则 source 指定源对象属性 target指定目标对象属性
     */
    @Mappings({
            @Mapping(source = "id", target = "userId"),
            @Mapping(source = "username", target = "name"),
            @Mapping(source = "role.roleName", target = "roleName")
    })
    UserRoleDto toUserRoleDto(User user);
}

测试类

package com.example.demo.mappers;

import com.example.demo.DemoApplication;
import com.example.demo.dto.UserRoleDto;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class UserRoleMapperTest {

    // 注入Mapper
    @Autowired
    private UserRoleMapper userRoleMapper;

    Role role = null;
    User user = null;

    @Before
    public void before() {
        role = new Role(2L, "admin", "超级管理员");
        user = new User(1L, "herion", "123456", "1389999888", "123@qq.com", role);
    }

    @Test
    public void toUserRoleDtorTest() {
        UserRoleDto userRoleDto=userRoleMapper.toUserRoleDto(user);
        System.out.println(userRoleDto);
    }
}

测试结果

UserRoleDto(userId=1, name=herion, roleName=admin)

多个参数中的值绑定

定义mapper

@Mappings({
            // 把user中的id绑定到目标对象的userId属性中
            @Mapping(source = "user.id", target = "userId"),
            // 把user中的username绑定到目标对象的name属性中
            @Mapping(source = "user.username", target = "name"),
            // 把role对象的roleName属性值绑定到目标对象的roleName中
            @Mapping(source = "role.roleName", target = "roleName")
    })
    UserRoleDto toUserRoleDto(User user, Role role);

测试类

package com.example.demo.mappers;

import com.example.demo.DemoApplication;
import com.example.demo.dto.UserRoleDto;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class UserRoleMapperTest {

    // 注入Mapper
    @Autowired
    private UserRoleMapper userRoleMapper;

    Role role = null;
    User user = null;

    @Before
    public void before() {
        role = new Role(2L, "admin", "超级管理员");
        user = new User(1L, "herion", "123456", "1389999888", "123@qq.com", role);
    }

    @Test
    public void toUserRoleDto2Test() {
        UserRoleDto userRoleDto=userRoleMapper.toUserRoleDto(user,role);
        System.out.println(userRoleDto);
    }
}

测试结果

UserRoleDto(userId=1, name=herion, roleName=admin)

入参作为值绑定

定义mapper

@Mappings({
            // 把user中的id绑定到目标对象的userId属性中
            @Mapping(source = "user.id", target = "userId"),
            // 把user中的username绑定到目标对象的name属性中
            @Mapping(source = "user.username", target = "name"),
            // 把role对象的roleName属性值绑定到目标对象的roleName中
            @Mapping(source = "myRoleName", target = "roleName")
    })
    UserRoleDto useParameter(User user, String myRoleName);

测试类

package com.example.demo.mappers;

import com.example.demo.DemoApplication;
import com.example.demo.dto.UserRoleDto;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class UserRoleMapperTest {

    // 注入Mapper
    @Autowired
    private UserRoleMapper userRoleMapper;

    Role role = null;
    User user = null;

    @Before
    public void before() {
        role = new Role(2L, "admin", "超级管理员");
        user = new User(1L, "herion", "123456", "1389999888", "123@qq.com", role);
    }

   @Test
    public void useParameterTest() {
        UserRoleDto userRoleDto = userRoleMapper.useParameter(user, "myUserRole");
        System.out.println(userRoleDto);
    }
}

测试结果

UserRoleDto(userId=1, name=herion, roleName=myUserRole)

更新对象中的属性

定义mapper

    @Mappings({
            @Mapping(source = "userId", target = "id"),
            @Mapping(source = "name", target = "username"),
            @Mapping(source = "roleName", target = "role.roleName")
    })
    void updateDto(UserRoleDto userRoleDto, @MappingTarget User user);

测试类

package com.example.demo.mappers;

import com.example.demo.DemoApplication;
import com.example.demo.dto.UserRoleDto;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class UserRoleMapperTest {

    // 注入Mapper
    @Autowired
    private UserRoleMapper userRoleMapper;

    Role role = null;
    User user = null;

    @Before
    public void before() {
        role = new Role(2L, "admin", "超级管理员");
        user = new User(1L, "herion", "123456", "1389999888", "123@qq.com", role);
    }

      @Test
    public void updateDtoTest() {
        UserRoleDto userRoleDto=new UserRoleDto();
        userRoleDto.setName("管理员");
        userRoleDto.setRoleName("架构部");
        userRoleDto.setUserId(2L);

        System.out.println("执行前 userRoleDto:"+userRoleDto.toString());
        System.out.println("执行前 user:"+user.toString());
        userRoleMapper.updateDto(userRoleDto,user);
        System.out.println("执行后 userRoleDto:"+userRoleDto.toString());
        System.out.println("执行后 user:"+user.toString());
    }
}

测试结果

执行前 userRoleDto:UserRoleDto(userId=2, name=管理员, roleName=架构部)
执行前 user:User(id=1, username=herion, password=123456, phoneNum=1389999888, email=123@qq.com, role=Role(id=2, roleName=admin, description=超级管理员))
执行后 userRoleDto:UserRoleDto(userId=2, name=管理员, roleName=架构部)
执行后 user:User(id=2, username=管理员, password=123456, phoneNum=1389999888, email=123@qq.com, role=Role(id=2, roleName=架构部, description=超级管理员))

自定义类型转换

有时候,在对象转换的时候可能会出现这样一个问题,就是源对象中的类型是Boolean类型,而目标对象类型是String类型,这种情况可以通过@Mapper的uses属性来实现:

定义规则类

package com.example.demo.format;

import org.springframework.stereotype.Component;

@Component
public class BooleanStrFormat {
    public String toStr(Boolean isDisable) {
        if (isDisable) {
            return "Y";
        } else {
            return "N";
        }
    }
    public Boolean toBoolean(String str) {
        if (str.equals("Y")) {
            return true;
        } else {
            return false;
        }
    }
}

定义Mapper

@Mapper( uses = { BooleanStrFormat.class}),注意,这里的users属性用于引用之前定义的转换规则的类:

package com.example.demo.mappers;

import com.example.demo.dto.CustomerDto;
import com.example.demo.entity.Customer;
import com.example.demo.format.BooleanStrFormat;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

@Mapper(componentModel = "spring", uses = { BooleanStrFormat.class})
public interface CustomerListMapper {

    @Mappings({
            @Mapping(source = "name", target = "customerName"),
            @Mapping(source = "isDisable", target = "disable")
    })
    CustomerDto customersToCustomerDto(Customer customer);
}

测试类

@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class CustomerMapperTest {

    @Autowired
    private CustomerListMapper customerListMapper;


    @Test
    public void customersToCustomerDtoTest(){
        Customer customer = new Customer(1L, "herion",true,null,new Date());
        CustomerDto customerDto = customerListMapper.customersToCustomerDto(customer);
        System.out.println(customerDto.toString());
    }

}

测试结果

CustomerDto(id=1, customerName=herion, disable=Y)

list转换

定义Mapper

@Mapper(componentModel = "spring", uses = { BooleanStrFormat.class})
public interface CustomerListMapper {

    @Mappings({
            @Mapping(source = "name", target = "customerName"),
            @Mapping(source = "isDisable", target = "disable")
    })
    CustomerDto customersToCustomerDto(Customer customer);


    List<CustomerDto> customersToCustomerDtos(List<Customer> customers);

}

测试类

    @Test
    public void customersToCustomerDtosTest(){
        Customer customer1 = new Customer(1L, "herion1",true,null,new Date());
        Customer customer2 = new Customer(2L, "herion2",true,null,new Date());
        Customer customer3 = new Customer(3L, "herion3",true,null,new Date());
        List<Customer> list=new ArrayList<Customer>();
        list.add(customer1);
        list.add(customer2);
        list.add(customer3);
        List<CustomerDto> customerDtos = customerListMapper.customersToCustomerDtos(list);
        customerDtos.forEach(customer -> {
            System.out.println(customer.toString());
        });
    }

测试结果:

CustomerDto(id=1, customerName=herion1, disable=Y)
CustomerDto(id=2, customerName=herion2, disable=Y)
CustomerDto(id=3, customerName=herion3, disable=Y)

复杂混合用法

定义vo

package com.example.demo.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomerVO {
    private Long id;
    private String disable;
    private String email;
    private String birthDateFormat;
    private String username;
    private String phoneNum;
}

定义mapper

   @Mappings({
            @Mapping(source = "customer.name", target = "username"),
            @Mapping(source = "customer.id", target = "id"),
            //格式转换
            @Mapping(source = "customer.isDisable", target = "disable"),
            //日期格式转换
            @Mapping(source = "customer.birthday", target = "birthDateFormat", dateFormat = "yyyy-MM-dd HH:mm:ss"),
            //ignore 忽略映射
            @Mapping(target = "email", ignore = true)
    })
    CustomerVO userCustomerToCustomerVO(Customer customer, User user);

测试类

 @Test
 public void userCustomerToCustomerVOTest(){
     Customer customer = new Customer();
     customer.setId(111L);
     customer.setName("herion");
     customer.setIsDisable(true);
     customer.setBirthday(new Date());

     User user=new User();
     user.setEmail("11@qq.com");
     user.setId(222L);
     user.setPhoneNum("13812344321");
     CustomerVO customerVO = customerMapper.userCustomerToCustomerVO(customer,user);
     System.out.println(customerVO.toString());
 }

测试结果

CustomerVO(id=111, disable=Y, email=null, birthDateFormat=2019-11-27 15:44:47, username=herion, phoneNum=13812344321)
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页