mapstruct對象轉換的幾種較特殊用法

說到對象轉換,以前都是自己new對象,然後逐個屬性去轉換,並set到對應的屬性中。但這種方式純手寫代碼,實在過於麻煩,尤其是屬性繁多時,後來自己用反射去寫,相比較純手工轉換要好很多,但是還是較爲麻煩,後來在開發的過程中無意發現了mapstruct這個工具,是真的好用啊,屬性名相同的,字段類型相的的直接不用管,屬性名不同,字段類型不同的也只需要用註解去轉換就可以,還可以抽取公共的轉換方法,直接調用,真可謂全能。接下來就具體說一下使用方法,做爲自己的掌握知識的總結,也爲想使用此工具的朋友提供參考。

導入jar包:

我所使用的版本:

<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>

模擬前端傳入的添加對象UserAddDto:

public class UserAddDto {
    private String name;
    private String age;
    private String address;
    private String birthday;
}

 要轉換成的對象User(一般爲數據庫表對應的對象):

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "主鍵")
    private String id;
    @ApiModelProperty(value = "姓名")
    private String name;
    @ApiModelProperty(value = "年齡")
    private Integer age;
    @ApiModelProperty(value = "家庭住址")
    private String address;
    @ApiModelProperty(value = "出生日期")
    private LocalDateTime birthday;
    @ApiModelProperty(value = "用戶狀態:1正常,0不正常")
    private String status;
    @ApiModelProperty(value = "創建時間")
    private LocalDateTime createdTime;
}

以上兩個對象的字段轉換關係如下:

id:調用方法自動生成,在轉換中實現,此處採用的是UUID()

name:String到String,直接轉換

age:String到Integer, 會自動進行操作,不需要特殊處理

address:String到String,直接轉換

birthday:String到LocalDateTime,調用自定義方法進行轉換

status:添加時爲固定值:1正常

createdTime:默認當前系統時間,調用java方法實現

爲演示兩種轉換方式,此處特定義兩個屬性轉換公共類,通常情況下只定義一個即可實現

屬性轉換公共類1TypeConversionMapper:

@Component
@Named("TypeConversionMapper")
public class TypeConversionMapper {
    @Named("strDateToLocalDateTime")
    public LocalDateTime strDateToLocalDateTime(String strDate){
        if (StringUtils.isBlank(strDate))
            return null;
        return DateUtils.strDateToLocalDateTime(strDate);
    }
}

屬性轉換公共類2BaseMapper

public interface BaseMapper {
    default String getUUID(){
        return CommonUtils.getUUID();
    }
    default String getUserName(){
        return ThreadLocalContextHolder.getContext().getUserInfo().getUserName();
    }
    default String getUserId(){
        return ThreadLocalContextHolder.getContext().getUserInfo().getUserId();
    }
    default LocalDateTime parseLocalDateToTime(String date){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (StringUtils.isEmpty(date)) return null;
        return LocalDateTime.of(LocalDate.parse(date,dateTimeFormatter), LocalTime.of(0,0));
    }
}

轉換接口UserMapTran:

@Mapper(
        componentModel = "spring",
        uses = {TypeConversionMapper.class}
)
public interface UserMapTran extends BaseMapper{
    @Mappings({
            @Mapping(
                    target = "birthday",
                    source = "birthday",
                    qualifiedByName = "strDateToLocalDateTime"
            ),
            @Mapping(
                    target = "name",source = "name"
            ),
            @Mapping(target = "age",source = "age"),
            @Mapping(target = "address",source = "address"),
            @Mapping(target = "status",constant = "1"),
            @Mapping(target = "id",expression = "java(getUUID())"),
            @Mapping(target = "createdTime",expression = "java(java.time.LocalDateTime.now())")
    })
    User userAddDtoToUser(UserAddDto addDto);
}

@Mapper componentModel 指定管理組件,此處交由spring管理

users指定屬性轉換類 TypeConversionMapper.class

承認屬性轉換接口BaseMapper

birthday屬性的轉換是通過TypeConversionMapper來實現的,通過名字調用具體的方法(由@Name屬性指定,@Mapping裏的qualifiedByName屬性指定具體方法)。

id屬性的生成則是調用TypeConversionMapper中的方法,通過expresssion屬性指定具體方法,需要說明的這種方式要指定編譯語言,如java(BaseMapper接口裏的方法名),如果不繼承BaseMapper接口,也可以通過指定方法全路徑的方式來實現,如createdTime屬性的轉換方式。

貼一下編譯後的實現類UserMapTranImpl:

public class UserMapTranImpl implements UserMapTran {
    @Autowired
    private TypeConversionMapper typeConversionMapper;

    public UserMapTranImpl() {
    }

    public User userAddDtoToUser(UserAddDto addDto) {
        if (addDto == null) {
            return null;
        } else {
            User user = new User();
            user.setBirthday(this.typeConversionMapper.strDateToLocalDateTime(addDto.getBirthday()));
            user.setAddress(addDto.getAddress());
            user.setName(addDto.getName());
            if (addDto.getAge() != null) {
                user.setAge(Integer.parseInt(addDto.getAge()));
            }

            user.setCreatedTime(LocalDateTime.now());
            user.setId(this.getUUID());
            user.setStatus("1");
            return user;
        }
    }
}

從上面的編譯結果可以看出,正確調用了我們所指定的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章