前言

ModelMapper 是一个 Object To Object 的工具,类似于 MapStruct又不同于 MapStruct。主要原因是 ModelMapper 是利用反射的原理实现的 Object To Object。

ModelMapper 官方API : http://modelmapper.org/user-manual/property-mapping/

使用场景

        前端传递的参数分属于多个po实体,用ModelMapper来处理前端传递过来的实体到对应的po实体中。

ModelMapperSingle工具类

import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;

public class ModelMapperSingle {

    protected final static ModelMapper modelMapper = new ModelMapper();
    private final static ModelMapperSingle modelMapperSingle = new ModelMapperSingle();

    static {
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    }

    public static ModelMapper Instance() {
        return modelMapperSingle.modelMapper;
    }
}

参数接收实体

@Data
public class ProductRequestVM extends BaseVM {

    //商品ID
    private Long productId;

    //商品名称
    @NotBlank
    private String productName;

    //商品分类ID
    @NotNull
    private Long categoryId;

    //一级商品分类ID
    private Long rootCategoryId;

    //销量
    private Integer soldNum;

    //商品内容
    private String content;

    //商品参数ID
    private Long paramId;

    //产地
    private String productPlace;

    //保质期
    private String footPeriod;

    //品牌名
    private String brand;

    //生产厂名
    private String factoryName;

    //生产厂址
    private String factoryAddress;

    //包装方式
    private String packagingMethod;

    //规格重量
    private String weight;

    //存储方式
    private String storageMethod;

    //食用方式
    private String eatMethod;
}
import com.bw.note.util.ModelMapperSingle;
import org.modelmapper.ModelMapper;


public class BaseVM {

    protected static ModelMapper modelMapper = ModelMapperSingle.Instance();


    public static ModelMapper getModelMapper() {
        return modelMapper;
    }

    public static void setModelMapper(ModelMapper modelMapper) {
        BaseVM.modelMapper = modelMapper;
    }
}

po实体

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

@Data
@TableName("PRODUCT")
public class Product extends Model<Product> {

    @ApiModelProperty(value = "序号")
    @TableId(value = "PRODUCT_ID", type = IdType.AUTO)
    private Long productId;

    @NotBlank(message = "商品名称不能为空")
    @ApiModelProperty(value = "商品名称")
    @TableId("PRODUCT_NAME")
    private String productName;

    @NotNull(message = "商品分类ID不能为空")
    @ApiModelProperty(value = "商品分类ID")
    @TableId("CATEGORY_ID")
    private Long categoryId;

    @NotNull(message = "一级分类外键ID不能为空")
    @ApiModelProperty(value = "一级分类外键id 一级分类id,用于优化查询")
    @TableId("ROOT_CATEGORY_ID")
    private Long rootCategoryId;

    @ApiModelProperty(value = "销量 累计销售")
    @TableId("SOLD_NUM")
    private Integer soldNum;

    @ApiModelProperty(value = "默认是1,表示正常状态, -1表示删除, 0下架 默认是1,表示正常状态, -1表示删除, 0下架")
    @TableId("PRODUCT_STATUS")
    private Integer productStatus;

    @ApiModelProperty(value = "商品内容")
    @TableId("CONTENT")
    private String content;

    @ApiModelProperty(value = "创建时间",hidden = true)
    @TableField(value = "CREATE_TIME",fill = FieldFill.INSERT)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @ApiModelProperty(value = "更新时间",hidden = true)
    @TableField(value = "UPDATE_TIME",fill = FieldFill.UPDATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;


    @Override
    protected Serializable pkVal() {
        return null;
    }
}
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;


@Data
@TableName("PRODUCT_PARAMS")
public class ProductParams extends Model<ProductParams> {

    @ApiModelProperty(value = "商品参数id")
    @TableId(value = "PARAM_ID", type = IdType.AUTO)
    private Long paramId;

    @NotNull(message = "商品id不能为空")
    @ApiModelProperty(value = "商品id")
    @TableId(value = "PRODUCT_ID")
    private Long productId;

    @ApiModelProperty(value = "产地")
    @TableId(value = "PRODUCT_PLACE")
    private String productPlace;

    @ApiModelProperty(value = "保质期,例:180天")
    @TableId(value = "FOOT_PERIOD")
    private String footPeriod;

    @ApiModelProperty(value = "品牌名")
    @TableId(value = "BRAND")
    private String brand;

    @ApiModelProperty(value = "生产厂名")
    @TableId(value = "FACTORY_NAME")
    private String factoryName;

    @ApiModelProperty(value = "生产厂址")
    @TableId(value = "FACTORY_ADDRESS")
    private String factoryAddress;

    @ApiModelProperty(value = "包装方式,例:袋装")
    @TableId(value = "PACKAGING_METHOD")
    private String packagingMethod;

    @ApiModelProperty(value = "规格重量,例:35g")
    @TableId(value = "WEIGHT")
    private String weight;

    @ApiModelProperty(value = "存储方法,例:常温5~25°")
    @TableId(value = "STORAGE_METHOD")
    private String storageMethod;

    @ApiModelProperty(value = "食用方式,例:开袋即食")
    @TableId(value = "EAT_METHOD")
    private String eatMethod;

    @ApiModelProperty(value = "创建时间",hidden = true)
    @TableField(value = "CREATE_TIME",fill = FieldFill.INSERT)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @ApiModelProperty(value = "更新时间",hidden = true)
    @TableField(value = "UPDATE_TIME",fill = FieldFill.UPDATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    @Override
    protected Serializable pkVal() {
        return null;
    }
}

实际应用

protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();

public Product saveOrUpdateProduct(ProductRequestVM model) {
    Product product = modelMapper.map(model, Product.class);
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐