日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MapStruct超级简单的学习笔记

發(fā)布時間:2023/12/8 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MapStruct超级简单的学习笔记 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

MapStruct

    • 使用MapStruct
      • 1.MapStruct是用來做什么的?
      • 2.使用MapStruct解決上述問題
      • 3.添加默認方法
      • 4. 可以使用abstract class來代替接口
      • 5.可以使用多個參數(shù)
      • 5.直接使用參數(shù)作為屬性值
      • 6.更新對象屬性
      • 7.沒有getter/setter也能賦值
      • 8.使用Spring依賴注入
      • 9.自定義類型轉換

使用MapStruct

首先來了解一下DTO,DTO簡單的理解就是做數(shù)據(jù)傳輸對象的,類似于VO,但是VO用于傳輸?shù)角岸恕?#xff08;~~)

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對象轉換成UserRoleDto對象

public class MainTest {User user = null;/*** 模擬從數(shù)據(jù)庫中查出user對象*/@Beforepublic void before() {Role role = new Role(2L, "administrator", "超級管理員");user = new User(1L, "zhangsan", "12345", "17677778888", "123@qq.com", role);}/*** 模擬把user對象轉換成UserRoleDto對象*/@Testpublic 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對象屬性轉換接口,在這個類里面規(guī)定轉換規(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);}

在測試類中測試:

public class MainTest {User user = null;/*** 模擬從數(shù)據(jù)庫中查出user對象*/@Beforepublic void before() {Role role = new Role(2L, "administrator", "超級管理員");user = new User(1L, "zhangsan", "12345", "17677778888", "123@qq.com", role);}/*** 模擬通過MapStruct把user對象轉換成UserRoleDto對象*/@Testpublic void test2() {UserRoleDto userRoleDto = UserRoleMapper.INSTANCES.toUserRoleDto(user);System.out.println(userRoleDto);} }

通過上面的例子可以看出,使用MapStruct方便許多。

3.添加默認方法

添加默認方法是為了這個類(接口)不只是為了做數(shù)據(jù)轉換用的,也可以做一些其他的事。

import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers;/*** @Mapper 定義這是一個MapStruct對象屬性轉換接口,在這個類里面規(guī)定轉換規(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對象屬性轉換接口,在這個類里面規(guī)定轉換規(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對象屬性轉換接口,在這個類里面規(guī)定轉換規(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對象屬性轉換接口,在這個類里面規(guī)定轉換規(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;@Beforepublic void before() {role = new Role(2L, "administrator", "超級管理員");user = new User(1L, "zhangsan", "12345", "17677778888", "123@qq.com", role);}@Testpublic 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類為源類,調用此方法,會把源類中的屬性更新到目標類中。更新規(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);@InheritInverseConfigurationCustomerDto 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 {@Overridepublic Customer toCustomer(CustomerDto customerDto) {if ( customerDto == null ) {return null;}Customer customer = new Customer();customer.setName( customerDto.customerName );customer.setId( customerDto.id );return customer;}@Overridepublic 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容器。可以在其它類中直接注入了:

@SpringBootApplication @RestController public class DemoMapstructApplication {// 注入Mapper@Autowiredprivate 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 {@Overridepublic CustomerDto toCustomerDto(Customer customer) {if ( customer == null ) {return null;}CustomerDto customerDto = new CustomerDto();customerDto.setCustomerName( customer.getName() );customerDto.setId( customer.getId() );return customerDto;} }

9.自定義類型轉換

有時候,在對象轉換的時候可能會出現(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; }

定義轉換規(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屬性用于引用之前定義的轉換規(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就會轉變成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();@Overridepublic 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;} }

要注意的是,如果使用了例如像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); }

轉換類要加入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 {// 使用自動注入的方式引入@Autowiredprivate BooleanStrFormat booleanStrFormat;@Overridepublic 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;} }

總結

以上是生活随笔為你收集整理的MapStruct超级简单的学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。