在一个成熟可维护的工程中,细分模块后,domian工程最好不要被其他工程依赖,但是实体类一般存于domain之中,这样其他工程想获取实体类数据时就需要在各自工程写model,自定义model可以根据自身业务需要而并不需要映射整个实体属性。
mapstruct这个插件就是用来处理domin实体类与model类的属性映射,定义mapper接口,mapstruct就会自动的帮我们实现这个映射接口,避免了麻烦复杂的映射实现。
MapStruct是用于生成类型安全的bean映射类的Java注解处理器。所有你需要做的是定义一个声明任何所需的映射方法的映射器接口。在编译期间,MapStruct将生成此接口的实现。此实现使用纯Java方法调用来在源对象和目标对象之间进行映射,即无反射或类似。
与手写映射代码相比,MapStruct通过生成繁琐且容易出错的代码来节省时间。遵循配置方法的约定,MapStruct使用合理的默认值,但是在配置或实现特殊行为时会采取措施。
与动态映射框架相比,MapStruct具有以下优点:
•通过使用简单方法调用而不是反射来快速执行
•编译时类型安全性:只能映射彼此的对象和属性,例如不会将订单实体意外映射到客户DTO等中
•如果实体或属性无法映射在构建会错误
注意:
有时候在运行时发现没有Mapper对应的实现类
解决关键在于:要先clean再进行build
配置:
xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>org.mapstruct.examples.fieldgroupId> <artifactId>mapstruct-examples-field-mappingsartifactId> <version>1.0.0version> <properties> <org.mapstruct.version>1.2.0.CR1org.mapstruct.version> properties> <dependencies> <dependency> <groupId>org.mapstructgroupId> <artifactId>mapstruct-jdk8artifactId> <version>${org.mapstruct.version}version> dependency> <dependency> <groupId>org.mapstructgroupId> <artifactId>mapstruct-processorartifactId> <version>${org.mapstruct.version}version> <scope>providedscope> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>org.assertjgroupId> <artifactId>assertj-coreartifactId> <version>3.6.2version> <scope>testscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>3.5.1version> <configuration> <source>1.8source> <target>1.8target> <annotationProcessorPaths> <path> <groupId>org.mapstructgroupId> <artifactId>mapstruct-processorartifactId> <version>${org.mapstruct.version}version> path> annotationProcessorPaths> configuration> plugin> plugins> build> project>
例子:
entity:
public class Customer { private Long id; private String name; private Collection<OrderItem> orderItems; //getter setter省略
}
public class OrderItem { private String name; private Long quantity; //getter setter省略
}
model:
public class CustomerDto { public Long id; public String customerName; public List<OrderItemDto> orders; }
public class OrderItemDto { public String name; public Long quantity; }
mapper:
@Mapper(uses = { OrderItemMapper.class })
public interface CustomerMapper { CustomerMapper MAPPER = Mappers.getMapper( CustomerMapper.class ); @Mappings({ @Mapping(source = "orders", target = "orderItems"), @Mapping(source = "customerName", target = "name") }) Customer toCustomer(CustomerDto customerDto); @InheritInverseConfiguration CustomerDto fromCustomer(Customer customer); }
@Mapper public interface OrderItemMapper { OrderItemMapper MAPPER = Mappers.getMapper(OrderItemMapper.class); OrderItem toOrder(OrderItemDto orderItemDto); @InheritInverseConfiguration OrderItemDto fromOrder(OrderItem orderItem); }