前言
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

本文介绍了ModelMapper这个ObjectToObject的工具,它通过反射实现对象间的映射。在实际应用中,例如处理前端传来的参数到PO实体时,ModelMapper能简化转换过程。文章还展示了如何创建ModelMapper的单例工具类以及如何在实体类中使用ModelMapper进行转换操作。
最低0.47元/天 解锁文章
3689

被折叠的 条评论
为什么被折叠?



