开发中,我们经常需要将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
<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
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;
}
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", "[email protected]", role);
}
@Test
public void toUserRoleDtorTest() {
UserRoleDto userRoleDto=userRoleMapper.toUserRoleDto(user);
System.out.println(userRoleDto);
}
}
UserRoleDto(userId=1, name=herion, roleName=admin)
@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", "[email protected]", role);
}
@Test
public void toUserRoleDto2Test() {
UserRoleDto userRoleDto=userRoleMapper.toUserRoleDto(user,role);
System.out.println(userRoleDto);
}
}
UserRoleDto(userId=1, name=herion, roleName=admin)
@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", "[email protected]", role);
}
@Test
public void useParameterTest() {
UserRoleDto userRoleDto = userRoleMapper.useParameter(user, "myUserRole");
System.out.println(userRoleDto);
}
}
UserRoleDto(userId=1, name=herion, roleName=myUserRole)
@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", "[email protected]", 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( 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)
@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)
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;
}
@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("[email protected]");
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)