MapStruct使用实例
package com.chinaunicom.base.server.user.converter; import com.chinaunicom.base.server.user.entity.UserSpace; import com.chinaunicom.base.server.user.converter.worker.UserSpaceConvert; import com.chinaunicom.base.server.user.entity.UserSpaceEntity; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; import java.util.List; @Mapper(componentModel = "spring", uses = {UserSpaceConvert.class}) public interface UserSpaceBaseConverter { UserSpaceBaseConverter INSTANCES = Mappers.getMapper(UserSpaceBaseConverter.class); @Mappings({ @Mapping(source = "id", target = "id"), @Mapping(source = "employeeName", target = "employeeName"), @Mapping(source = "rootSpace", target = "rootSpace"), @Mapping(source = "createTime", target = "createTime"), @Mapping(source = "deleted", target = "deleted") }) UserSpaceEntity to(UserSpace userSpace); @Mappings({ @Mapping(source = "id", target = "id"), @Mapping(source = "employeeName", target = "employeeName"), @Mapping(source = "rootSpace", target = "rootSpace"), @Mapping(source = "createTime", target = "createTime"), @Mapping(source = "deleted", target = "deleted"), @Mapping(target = "age", expression = "java(com.chinaunicom.base.server.user.converter.worker.UserSpaceConvert.getAgeFromFeatures(userSpaceEntity.getFeatures()))") }) UserSpace back(UserSpaceEntity userSpaceEntity); List<UserSpaceEntity> toList(List<UserSpace> source); List<UserSpace> backList(List<UserSpaceEntity> source); }
UserSpaceEntity 有字段为string类型,保存的是json格式。对应的模型层有属性age是Integer类型的
public class UserSpaceConvert { public static Integer getAgeFromFeatures(String json) { Map<String,Object> valueMap = JSON.parseObject(json, Map.class); if (valueMap.containsKey("age")) { return Integer.valueOf(valueMap.get("age").toString()); } return null; } }
package com.chinaunicom.asset.server.designtype.model; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; import java.util.List; import java.util.Objects; @Data @NoArgsConstructor @AllArgsConstructor @Builder public class DesignType { /** * 主键 */ private Integer id; /** * 类型:1- 图标,2-插画 */ private Integer type; /** * 设计类型名称 */ private String designTypeName; /** * 标签 */ private List<DesignTag> designTags; /** * 创建时间 */ private LocalDateTime createTime; /** * 修改时间 */ private LocalDateTime updateTime; /** * 是否删除:0-未删除 ,1-删除 */ private Integer deleted; public boolean isDeleted() { return Objects.equals(deleted, 1); } public static DesignType copy(DesignType source) { if (source == null) { return null; } return DesignType.builder() .id(source.getId()) .type(source.getType()) .designTypeName(source.getDesignTypeName()) .designTags(source.getDesignTags()) .createTime(source.getCreateTime()) .updateTime(source.getUpdateTime()) .deleted(source.getDeleted()) .build(); } }
package com.chinaunicom.asset.server.designpublish.entity; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor @Builder public class DesignPublishEntity { private Integer id; private Long designId; private String name; private String description; private Integer typeId; /** * 资产标签ids(以英文逗号分隔) */ private String tagIds; private String publisherId; private String publisherName; private Date createTime; private Date updateTime; private Integer deleted; }
package com.chinaunicom.asset.server.designpublish.convertor; import com.chinaunicom.asset.common.constants.CommonConstants; import com.chinaunicom.asset.server.designpublish.entity.DesignPublishEntity; import com.chinaunicom.asset.server.designpublish.model.DesignPublish; import com.chinaunicom.asset.server.designtype.model.DesignType; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.Named; import org.mapstruct.factory.Mappers; import org.springframework.util.CollectionUtils; import java.util.List; import java.util.stream.Collectors; @Mapper(componentModel = "spring") public interface DesignPublishConverter { DesignPublishConverter INSTANCES = Mappers.getMapper(DesignPublishConverter.class); @Mapping(source = "id", target = "id") @Mapping(source = "designId", target = "designId") @Mapping(source = "designType", target = "typeId", qualifiedByName = "toTypeId") @Mapping(source = "designType", target = "tagIds", qualifiedByName = "toTagIdStrings") @Mapping(source = "publisherId", target = "publisherId") @Mapping(source = "publisherName", target = "publisherName") @Mapping(source = "name", target = "name") @Mapping(source = "description", target = "description") @Mapping(source = "deleted", target = "deleted") @Mapping(source = "createTime", target = "createTime") @Mapping(source = "updateTime", target = "updateTime") DesignPublishEntity to(DesignPublish designType); @Mappings({ @Mapping(source = "id", target = "id"), @Mapping(source = "designId", target = "designId"), @Mapping(source = "typeId", target = "designType.type"), @Mapping(source = "publisherId", target = "publisherId"), @Mapping(source = "name", target = "name"), @Mapping(source = "description", target = "description"), @Mapping(source = "deleted", target = "deleted"), @Mapping(source = "createTime", target = "createTime"), @Mapping(source = "updateTime", target = "updateTime") }) DesignPublish back(DesignPublishEntity designTypeEntity); List<DesignPublishEntity> toList(List<DesignPublish> designTypeList); List<DesignPublish> backList(List<DesignPublishEntity> source); @Named("toTypeId") default Integer toTypeId(DesignType designType) { return designType == null ? null : designType.getType(); } @Named("toTagIdStrings") default String toTagIdStrings(DesignType designType) { if (designType == null || CollectionUtils.isEmpty(designType.getDesignTags())) { return null; } return designType.getDesignTags().stream() .map(designTag -> String.valueOf(designTag.getId())) .collect(Collectors.joining(CommonConstants.COMMA)); } }