注解@Mapper(componentModel = "spri

2019-08-13  本文已影响0人  炒面Z

注解@Mapper(componentModel = "spring")-自动生成VO-DO的模型映射
场景: 插件可以自动生成实现类,实现DTO-DO各种模型之间的字段映射(不仅仅限制于DTO-DO)

            <!--mapStruct依赖-->
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-jdk8</artifactId>
                <version>1.2.0.CR1</version>

            </dependency>
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.2.0.CR1</version>   
                <scope>provided</scope>
            </dependency>
@Mapper(componentModel = "spring")
public interface RoleConvert {

    RoleDO convert(RoleCreateDTO roleCreateDTO);
}


    使用:
    @Autowired
    private RoleConvert roleConvert;

    // 转化
    RoleDO roleDO = roleConvert.convert(roleCreateDTO);

编译后自动生成了RoleConvert 的实现类 RoleConvertImpl.class

image.png
@Mapper(componentModel = "spring")
public interface ProductConverter {

    @Mappings({
            @Mapping(source = "id",target = "productId"),
            @Mapping(source = "title",target = "productName"),
            @Mapping(source = "price",target = "salePrice"),
            @Mapping(source = "sellPoint",target = "subTitle"),
            @Mapping(source = "image",target = "picUrl")
    })
    ProductDto item2Dto(ItemDocument item);
}
上一篇 下一篇

猜你喜欢

热点阅读