介绍 MapStruct在一个成熟可维护的工程中,细分模块后,domian工程最好不要被其他工程依赖,但是实体类一般存于domain之中,这样其他工程想获取实体类数据时就需要在各自工程写model,自定义model可以根据自身业务需要而并不需要映射整个实体属性。 mapstruct这个插件就是用来处理domin实体类与model类的属性映射,定义mapper接口,mapstruct就会自动的帮我们实现这个映射接口,避免了麻烦复杂的映射实现。
如何使用 Mapper基本类:BasicObjectMapper,BasicObjectMapper包含了4个基本方法,单个和集合以及反转的单个和集合。
开发中如需要对象转换操作可直接新建interface并继承BasicObjectMapper,并在新建的接口上加上 @Mapper(componentModel = “spring”), 如果是属性中包含其它类以及该类已经存在Mapper则注解中加上 users = {类名.class},具体如何使用以及其他各种用法在此不再赘述(本文的重点是看标题,看标题,看标题),google不行可以找度娘, componentModel = “spring”该配置表示生成的实现类默认加上spring @Component注解,使用时可直接通过@Autowire进行注入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.ampmind.framework.map;import java.util.List;import org.mapstruct.InheritConfiguration;import org.mapstruct.InheritInverseConfiguration;import org.mapstruct.Mappings;public interface BasicObjectMapper <SOURCE , TARGET > { @Mappings({}) @InheritConfiguration TARGET to (SOURCE var1) ; @InheritConfiguration List<TARGET> to (List<SOURCE> var1) ; @InheritInverseConfiguration SOURCE from (TARGET var1) ; @InheritInverseConfiguration List<SOURCE> from (List<TARGET> var1) ; }
下面是两个不同的例子: 先贴一下两个Model类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.ampmind.service.skumng.domain;public class ProductCategory { private String categoryCode; private String categoryName; public String getCategoryCode () { return categoryCode; } public void setCategoryCode (String categoryCode) { this .categoryCode = categoryCode; } public String getCategoryName () { return categoryName; } public void setCategoryName (String categoryName) { this .categoryName = categoryName; } }
CategoryVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.ampmind.service.api.protocol.vo;public class ProductCategory { private String code; private String name; public Integer getParentId () { return parentId; } public void setParentId (Integer parentId) { this .parentId = parentId; } public String getCode () { return code; } public void setCode (String code) { this .code = code; } public String getName () { return name; } public void setName (String name) { this .name = name; } }
1.如何与Spring配合 CategoryMapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.ampmind.service.skumng.api.mapper;import com.ampmind.framework.api.base.BasicObjectMapper;import com.ampmind.service.api.protocol.vo.CategoryVo;import com.ampmind.service.domain.ProductCategory;import org.mapstruct.Mapper;import org.mapstruct.Mapping;import org.mapstruct.Mappings;@Mapper(componentModel = "spring") public interface CategoryMapper extends BasicObjectMapper <CategoryVo , ProductCategory > { @Mappings({ @Mapping(source = "code", target = "categoryCode"), @Mapping(source = "name", target = "categoryName") }) ProductCategory to (CategoryVo source) ; }
上面重写了to方法,注意如果属性名一样可以不用重写。保持接口空的就行,有不一样的需要重写to方法,并在方法上加上 @Mappings注解和子注解 spring注入并使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Component public class Test { @Autowired private CategoryMapper categoryMapper; public void test () { CategoryVo vo = new CategoryVo; vo.setCategoryCode("0000" ); vo.setCategoryName("属性名称" ); ProductCategory pc = categoryMapper.to(vo); CategoryVo vo1 = categoryMapper.form(pc); List<ProductCategory> pcList = categoryMapper.to(Arrays.asList(vo, vo1)); List<CategoryVo> voList = categoryMapper.from(pcList); } }
2.如何直接使用 CategoryMapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.ampmind.service.skumng.api.mapper;import com.ampmind.framework.api.base.BasicObjectMapper;import com.ampmind.service.skumng.api.protocol.vo.CategoryVo;import com.ampmind.service.skumng.domain.ProductCategory;import org.mapstruct.Mapper;import org.mapstruct.Mapping;import org.mapstruct.Mappings;import org.mapstruct.factory.Mappers;@Mapper public interface CategoryMapper extends BasicObjectMapper <CategoryVo , ProductCategory > { CategoryMapper MAPPER = Mappers.getMapper(CategoryMapper.class); @Mappings({ @Mapping(source = "code", target = "categoryCode"), @Mapping(source = "name", target = "categoryName") }) ProductCategory to (CategoryVo source) ; }
直接可以通过main方法进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Test { public static void main (String[] args) { CategoryVo vo = new CategoryVo; vo.setCategoryCode("0000" ); vo.setCategoryName("属性名称" ); ProductCategory pc = CategoryMapper.MAPPER.to(vo); CategoryVo vo1 = CategoryMapper.MAPPER.form(pc); List<ProductCategory> pcList = CategoryMapper.MAPPER.to(Arrays.asList(vo, vo1)); List<CategoryVo> voList = CategoryMapper.MAPPER.from(pcList); } }