项目地址:https://github.com/mapstruct/mapstruct
说明:mapstruct通过直接生成Getter/Setter方法,所以与原生调用效率基本一致
生成的代码maven是在 target\generated-sources\annotations 目录。

依赖

同时用到了lombok

  1. <lombok.version>1.18.12</lombok.version>
  2. <org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
  3. <m2e.apt.activation>jdt_apt</m2e.apt.activation>
  4. <dependency>
  5. <groupId>org.projectlombok</groupId>
  6. <artifactId>lombok</artifactId>
  7. <version>${lombok.version}</version>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.mapstruct</groupId>
  11. <artifactId>mapstruct</artifactId>
  12. <version>${org.mapstruct.version}</version>
  13. <scope>compile</scope>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.mapstruct</groupId>
  17. <artifactId>mapstruct-jdk8</artifactId>
  18. <version>${org.mapstruct.version}</version>
  19. <scope>compile</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.mapstruct</groupId>
  23. <artifactId>mapstruct-processor</artifactId>
  24. <version>${org.mapstruct.version}</version>
  25. <scope>compile</scope>
  26. <optional>true</optional>
  27. </dependency>
  28. <plugin>
  29. <groupId>org.apache.maven.plugins</groupId>
  30. <artifactId>maven-compiler-plugin</artifactId>
  31. <configuration>
  32. <annotationProcessorPaths>
  33. <path>
  34. <groupId>org.mapstruct</groupId>
  35. <artifactId>mapstruct-processor</artifactId>
  36. <version>${org.mapstruct.version}</version>
  37. </path>
  38. <path>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. <version>${lombok.version}</version>
  42. </path>
  43. </annotationProcessorPaths>
  44. </configuration>
  45. </plugin>

示例

  1. //class MpsVOMp
  2. import org.mapstruct.Mapper;
  3. import org.mapstruct.Mapping;
  4. import org.mapstruct.factory.Mappers;
  5. @Mapper(componentModel = "spring")
  6. //@Mapper
  7. public interface MpsVOMp {
  8. /**
  9. * 用来调用实例 实际开发中可使用注入Spring 不写
  10. */
  11. MpsVOMp CAR_MAPPING = Mappers.getMapper(MpsVOMp.class);
  12. /**
  13. * 源类型 目标类型 成员变量相同类型 相同变量名 不用写{@link org.mapstruct.Mapping}来映射
  14. */
  15. @Mapping(source = "string1", target = "string2")
  16. @Mapping(source = "fromVo", target = "toVo")
  17. MpsToVO mapper(MpsFromVO fromVO);
  18. }
  19. //class MpsFromVO
  20. import java.math.BigDecimal;
  21. import java.util.Date;
  22. import lombok.Data;
  23. @Data
  24. public class MpsFromVO {
  25. private int int1;
  26. private Integer int2;
  27. private long long1;
  28. private Long long2;
  29. private double double1;
  30. private BigDecimal double2;
  31. private String string1;
  32. private Date date1;
  33. private boolean bool1;
  34. private Boolean bool2;
  35. private MpsFromVO fromVo;
  36. }
  37. //class MpsToVO
  38. import java.math.BigDecimal;
  39. import java.util.Date;
  40. import lombok.Data;
  41. @Data
  42. public class MpsToVO {
  43. private int int1;
  44. private Integer int2;
  45. private long long1;
  46. private Long long2;
  47. private double double1;
  48. private BigDecimal double2;
  49. private String string1;
  50. private Date date1;
  51. private boolean bool1;
  52. private Boolean bool2;
  53. //
  54. private String string2;
  55. private MpsToVO toVo;
  56. }