實體映射最強工具類:MapStruct 真香!
回復架構師獲取資源
大家好,我是你們的朋友架構君,一個會寫代碼吟詩的架構師。
'javajgs.com';
來源:blog.csdn.net/qq122516902
1.MapStruct是用來做什么的? 2.使用MapStruct解決上述問題 3.添加默認方法 4.可以使用abstract class來代替接口
5.可以使用多個參數(shù) 5.直接使用參數(shù)作為屬性值 6.更新對象屬性 7.沒有getter/setter也能賦值 8.使用Spring依賴注入 9.自定義類型轉(zhuǎn)換
首先來了解一下DTO,DTO簡單的理解就是做數(shù)據(jù)傳輸對象的,類似于VO,但是VO用于傳輸?shù)角岸?。(~~)
1.MapStruct是用來做什么的?
現(xiàn)在有這么個場景,從數(shù)據(jù)庫查詢出來了一個user對象(包含id,用戶名,密碼,手機號,郵箱,角色這些字段)和一個對應的角色對象role(包含id,角色名,角色描述這些字段),現(xiàn)在在controller需要用到user對象的id,用戶名,和角色對象的角色名三個屬性。
一種方式是直接把兩個對象傳遞到controller層,但是這樣會多出很多沒用的屬性。更通用的方式是需要用到的屬性封裝成一個類(DTO),通過傳輸這個類的實例來完成數(shù)據(jù)傳輸。
User.java
@AllArgsConstructor
@Data
public class User {
private Long id;
private String username;
private String password;
private String phoneNum;
private String email;
private Role role;
}
Role.java
@AllArgsConstructor
@Data
public class Role {
private Long id;
private String roleName;
private String description;
}
UserRoleDto.java,這個類就是封裝的類
@Data
public class UserRoleDto {
/**
* 用戶id
*/
private Long userId;
/**
* 用戶名
*/
private String name;
/**
* 角色名
*/
private String roleName;
}
測試類,模擬將user對象轉(zhuǎn)換成UserRoleDto對象
public class MainTest {
User user = null;
/**
* 模擬從數(shù)據(jù)庫中查出user對象
*/
@Before
public void before() {
Role role = new Role(2L, "administrator", "超級管理員");
user = new User(1L, "zhangsan", "12345", "17677778888", "[email protected]", role);
}
/**
* 模擬把user對象轉(zhuǎn)換成UserRoleDto對象
*/
@Test
public void test1() {
UserRoleDto userRoleDto = new UserRoleDto();
userRoleDto.setUserId(user.getId());
userRoleDto.setName(user.getUsername());
userRoleDto.setRoleName(user.getRole().getRoleName());
System.out.println(userRoleDto);
}
}
從上面代碼可以看出,通過getter、setter的方式把一個對象屬性值復制到另一個對象中去還是很麻煩的,尤其是當屬性過多的時候。而MapStruct就是用于解決這種問題的。
2.使用MapStruct解決上述問題
這里我們沿用User.java、Role.java、UserRoleDto.java。
新建一個UserRoleMapper.java,這個來用來定義User.java、Role.java和UserRoleDto.java之間屬性對應規(guī)則:
UserRoleMapper.java
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @Mapper 定義這是一個MapStruct對象屬性轉(zhuǎn)換接口,在這個類里面規(guī)定轉(zhuǎn)換規(guī)則
* 在項目構建時,會自動生成改接口的實現(xiàn)類,這個實現(xiàn)類將實現(xiàn)對象屬性值復制
*/
@Mapper
public interface UserRoleMapper {
/**
* 獲取該類自動生成的實現(xiàn)類的實例
* 接口中的屬性都是 public static final 的 方法都是public abstract的
*/
UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);
/**
* 這個方法就是用于實現(xiàn)對象屬性復制的方法
*
* @Mapping 用來定義屬性復制規(guī)則 source 指定源對象屬性 target指定目標對象屬性
*
* @param user 這個參數(shù)就是源對象,也就是需要被復制的對象
* @return 返回的是目標對象,就是最終的結果對象
*/
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
}
在測試類中測試:
通過上面的例子可以看出,使用MapStruct方便許多。
3.添加默認方法
添加默認方法是為了這個類(接口)不只是為了做數(shù)據(jù)轉(zhuǎn)換用的,也可以做一些其他的事。
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @Mapper 定義這是一個MapStruct對象屬性轉(zhuǎn)換接口,在這個類里面規(guī)定轉(zhuǎn)換規(guī)則
* 在項目構建時,會自動生成改接口的實現(xiàn)類,這個實現(xiàn)類將實現(xiàn)對象屬性值復制
*/
@Mapper
public interface UserRoleMapper {
/**
* 獲取該類自動生成的實現(xiàn)類的實例
* 接口中的屬性都是 public static final 的 方法都是public abstract的
*/
UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);
/**
* 這個方法就是用于實現(xiàn)對象屬性復制的方法
*
* @Mapping 用來定義屬性復制規(guī)則 source 指定源對象屬性 target指定目標對象屬性
*
* @param user 這個參數(shù)就是源對象,也就是需要被復制的對象
* @return 返回的是目標對象,就是最終的結果對象
*/
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
/**
* 提供默認方法,方法自己定義,這個方法是我隨便寫的,不是要按照這個格式來的
* @return
*/
default UserRoleDto defaultConvert() {
UserRoleDto userRoleDto = new UserRoleDto();
userRoleDto.setUserId(0L);
userRoleDto.setName("None");
userRoleDto.setRoleName("None");
return userRoleDto;
}
}
測試代碼:
@Test
public void test3() {
UserRoleMapper userRoleMapperInstances = UserRoleMapper.INSTANCES;
UserRoleDto userRoleDto = userRoleMapperInstances.defaultConvert();
System.out.println(userRoleDto);
}
4. 可以使用abstract class來代替接口
mapper可以用接口來實現(xiàn),也可以完全由抽象來完全代替
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @Mapper 定義這是一個MapStruct對象屬性轉(zhuǎn)換接口,在這個類里面規(guī)定轉(zhuǎn)換規(guī)則
* 在項目構建時,會自動生成改接口的實現(xiàn)類,這個實現(xiàn)類將實現(xiàn)對象屬性值復制
*/
@Mapper
public abstract class UserRoleMapper {
/**
* 獲取該類自動生成的實現(xiàn)類的實例
* 接口中的屬性都是 public static final 的 方法都是public abstract的
*/
public static final UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);
/**
* 這個方法就是用于實現(xiàn)對象屬性復制的方法
*
* @Mapping 用來定義屬性復制規(guī)則 source 指定源對象屬性 target指定目標對象屬性
*
* @param user 這個參數(shù)就是源對象,也就是需要被復制的對象
* @return 返回的是目標對象,就是最終的結果對象
*/
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
public abstract UserRoleDto toUserRoleDto(User user);
/**
* 提供默認方法,方法自己定義,這個方法是我隨便寫的,不是要按照這個格式來的
* @return
*/
UserRoleDto defaultConvert() {
UserRoleDto userRoleDto = new UserRoleDto();
userRoleDto.setUserId(0L);
userRoleDto.setName("None");
userRoleDto.setRoleName("None");
return userRoleDto;
}
}
5.可以使用多個參數(shù)
可以綁定多個對象的屬性值到目標對象中:
package com.mapstruct.demo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @Mapper 定義這是一個MapStruct對象屬性轉(zhuǎn)換接口,在這個類里面規(guī)定轉(zhuǎn)換規(guī)則
* 在項目構建時,會自動生成改接口的實現(xiàn)類,這個實現(xiàn)類將實現(xiàn)對象屬性值復制
*/
@Mapper
public interface UserRoleMapper {
/**
* 獲取該類自動生成的實現(xiàn)類的實例
* 接口中的屬性都是 public static final 的 方法都是public abstract的
*/
UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);
/**
* 這個方法就是用于實現(xiàn)對象屬性復制的方法
*
* @Mapping 用來定義屬性復制規(guī)則 source 指定源對象屬性 target指定目標對象屬性
*
* @param user 這個參數(shù)就是源對象,也就是需要被復制的對象
* @return 返回的是目標對象,就是最終的結果對象
*/
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
UserRoleDto toUserRoleDto(User user);
/**
* 多個參數(shù)中的值綁定
* @param user 源1
* @param role 源2
* @return 從源1、2中提取出的結果
*/
@Mappings({
@Mapping(source = "user.id", target = "userId"), // 把user中的id綁定到目標對象的userId屬性中
@Mapping(source = "user.username", target = "name"), // 把user中的username綁定到目標對象的name屬性中
@Mapping(source = "role.roleName", target = "roleName") // 把role對象的roleName屬性值綁定到目標對象的roleName中
})
UserRoleDto toUserRoleDto(User user, Role role);
對比兩個方法~
5.直接使用參數(shù)作為屬性值
package com.mapstruct.demo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @Mapper 定義這是一個MapStruct對象屬性轉(zhuǎn)換接口,在這個類里面規(guī)定轉(zhuǎn)換規(guī)則
* 在項目構建時,會自動生成改接口的實現(xiàn)類,這個實現(xiàn)類將實現(xiàn)對象屬性值復制
*/
@Mapper
public interface UserRoleMapper {
/**
* 獲取該類自動生成的實現(xiàn)類的實例
* 接口中的屬性都是 public static final 的 方法都是public abstract的
*/
UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);
/**
* 直接使用參數(shù)作為值
* @param user
* @param myRoleName
* @return
*/
@Mappings({
@Mapping(source = "user.id", target = "userId"), // 把user中的id綁定到目標對象的userId屬性中
@Mapping(source = "user.username", target = "name"), // 把user中的username綁定到目標對象的name屬性中
@Mapping(source = "myRoleName", target = "roleName") // 把role對象的roleName屬性值綁定到目標對象的roleName中
})
UserRoleDto useParameter(User user, String myRoleName);
}
測試類:
public class Test1 {
Role role = null;
User user = null;
@Before
public void before() {
role = new Role(2L, "administrator", "超級管理員");
user = new User(1L, "zhangsan", "12345", "17677778888", "[email protected]", role);
}
@Test
public void test1() {
UserRoleMapper instances = UserRoleMapper.INSTANCES;
UserRoleDto userRoleDto = instances.useParameter(user, "myUserRole");
System.out.println(userRoleDto);
}
}
6.更新對象屬性
在之前的例子中UserRoleDto useParameter(User user, String myRoleName);都是通過類似上面的方法來生成一個對象。而MapStruct提供了另外一種方式來更新一個對象中的屬性。@MappingTarget
public interface UserRoleMapper1 {
UserRoleMapper1 INSTANCES = Mappers.getMapper(UserRoleMapper1.class);
@Mappings({
@Mapping(source = "userId", target = "id"),
@Mapping(source = "name", target = "username"),
@Mapping(source = "roleName", target = "role.roleName")
})
void updateDto(UserRoleDto userRoleDto, @MappingTarget User user);
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
void update(User user, @MappingTarget UserRoleDto userRoleDto);
}
通過@MappingTarget來指定目標類是誰(誰的屬性需要被更新)。@Mapping還是用來定義屬性對應規(guī)則。
以此為例說明:
@Mappings({
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
})
void update(User user, @MappingTarget UserRoleDto userRoleDto);
@MappingTarget標注的類UserRoleDto 為目標類,user類為源類,調(diào)用此方法,會把源類中的屬性更新到目標類中。更新規(guī)則還是由@Mapping指定。
7.沒有getter/setter也能賦值
對于沒有getter/setter的屬性也能實現(xiàn)賦值操作
public class Customer {
private Long id;
private String name;
//getters and setter omitted for brevity
}
public class CustomerDto {
public Long id;
public String customerName;
}
@Mapper
public interface CustomerMapper {
CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
@Mapping(source = "customerName", target = "name")
Customer toCustomer(CustomerDto customerDto);
@InheritInverseConfiguration
CustomerDto fromCustomer(Customer customer);
}
@Mapping(source = “customerName”, target = “name”)不是用來指定屬性映射的,如果兩個對象的屬性名相同是可以省略@Mapping的。
MapStruct生成的實現(xiàn)類:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-02-14T15:41:21+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
public class CustomerMapperImpl implements CustomerMapper {
@Override
public Customer toCustomer(CustomerDto customerDto) {
if ( customerDto == null ) {
return null;
}
Customer customer = new Customer();
customer.setName( customerDto.customerName );
customer.setId( customerDto.id );
return customer;
}
@Override
public CustomerDto toCustomerDto(Customer customer) {
if ( customer == null ) {
return null;
}
CustomerDto customerDto = new CustomerDto();
customerDto.customerName = customer.getName();
customerDto.id = customer.getId();
return customerDto;
}
}
@InheritInverseConfiguration在這里的作用就是實現(xiàn)customerDto.customerName = customer.getName();功能的。如果沒有這個注解,toCustomerDto這個方法則不會有customerName 和name兩個屬性的對應關系的。
8.使用Spring依賴注入
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
private Long id;
private String name;
}
@Data
public class CustomerDto {
private Long id;
private String customerName;
}
// 這里主要是這個componentModel 屬性,它的值就是當前要使用的依賴注入的環(huán)境
@Mapper(componentModel = "spring")
public interface CustomerMapper {
@Mapping(source = "name", target = "customerName")
CustomerDto toCustomerDto(Customer customer);
}
@Mapper(componentModel = “spring”),表示把當前Mapper類納入spring容器??梢栽谄渌愔兄苯幼⑷肓耍?/p>
@SpringBootApplication
@RestController
public class DemoMapstructApplication {
// 注入Mapper
@Autowired
private CustomerMapper mapper;
public static void main(String[] args) {
SpringApplication.run(DemoMapstructApplication.class, args);
}
@GetMapping("/test")
public String test() {
Customer customer = new Customer(1L, "zhangsan");
CustomerDto customerDto = mapper.toCustomerDto(customer);
return customerDto.toString();
}
}
看一下由mapstruct自動生成的類文件,會發(fā)現(xiàn)標記了@Component注解。
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-02-14T15:54:17+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
@Component
public class CustomerMapperImpl implements CustomerMapper {
@Override
public CustomerDto toCustomerDto(Customer customer) {
if ( customer == null ) {
return null;
}
CustomerDto customerDto = new CustomerDto();
customerDto.setCustomerName( customer.getName() );
customerDto.setId( customer.getId() );
return customerDto;
}
}
9.自定義類型轉(zhuǎn)換
有時候,在對象轉(zhuǎn)換的時候可能會出現(xiàn)這樣一個問題,就是源對象中的類型是Boolean類型,而目標對象類型是String類型,這種情況可以通過@Mapper的uses屬性來實現(xiàn):
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
private Long id;
private String name;
private Boolean isDisable;
}
@Data
public class CustomerDto {
private Long id;
private String customerName;
private String disable;
}
定義轉(zhuǎn)換規(guī)則的類:
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屬性用于引用之前定義的轉(zhuǎn)換規(guī)則的類:
@Mapper( uses = { BooleanStrFormat.class})
public interface CustomerMapper {
CustomerMapper INSTANCES = Mappers.getMapper(CustomerMapper.class);
@Mappings({
@Mapping(source = "name", target = "customerName"),
@Mapping(source = "isDisable", target = "disable")
})
CustomerDto toCustomerDto(Customer customer);
}
這樣子,Customer類中的isDisable屬性的true就會轉(zhuǎn)變成CustomerDto中的disable屬性的yes。
MapStruct自動生成的類中的代碼:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-02-14T16:49:18+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
public class CustomerMapperImpl implements CustomerMapper {
// 引用 uses 中指定的類
private final BooleanStrFormat booleanStrFormat = new BooleanStrFormat();
@Override
public CustomerDto toCustomerDto(Customer customer) {
if ( customer == null ) {
return null;
}
CustomerDto customerDto = new CustomerDto();
// 轉(zhuǎn)換方式的使用
customerDto.setDisable( booleanStrFormat.toStr( customer.getIsDisable() ) );
customerDto.setCustomerName( customer.getName() );
customerDto.setId( customer.getId() );
return customerDto;
}
}
要注意的是,如果使用了例如像spring這樣的環(huán)境,Mapper引入uses類實例的方式將是自動注入,那么這個類也應該納入Spring容器:
CustomerMapper.java指定使用spring
@Mapper(componentModel = "spring", uses = { BooleanStrFormat.class})
public interface CustomerMapper {
CustomerMapper INSTANCES = Mappers.getMapper(CustomerMapper.class);
@Mappings({
@Mapping(source = "name", target = "customerName"),
@Mapping(source = "isDisable", target = "disable")
})
CustomerDto toCustomerDto(Customer customer);
}
轉(zhuǎn)換類要加入Spring容器:
@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;
}
}
}
MapStruct自動生成的類:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-02-14T16:55:35+0800",
comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
@Component
public class CustomerMapperImpl implements CustomerMapper {
// 使用自動注入的方式引入
@Autowired
private BooleanStrFormat booleanStrFormat;
@Override
public CustomerDto toCustomerDto(Customer customer) {
if ( customer == null ) {
return null;
}
CustomerDto customerDto = new CustomerDto();
customerDto.setDisable( booleanStrFormat.toStr( customer.getIsDisable() ) );
customerDto.setCustomerName( customer.getName() );
customerDto.setId( customer.getId() );
return customerDto;
}
}
這些年小編給你分享過的干貨
2.優(yōu)質(zhì)ERP系統(tǒng)帶進銷存財務生產(chǎn)功能(附源碼)
3.優(yōu)質(zhì)SpringBoot帶工作流管理項目(附源碼)
5.SBoot+Vue外賣系統(tǒng)前后端都有(附源碼)

轉(zhuǎn)發(fā)在看就是最大的支持??
