mapstruct实体类转换工具

悠悠 2023-09-24 20:57 108阅读 0赞

1、mapstruct环境搭建

1、导入依赖文件

  1. <properties>
  2. <org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
  3. </properties>
  4. <!--mapStruct依赖-->
  5. <dependency>
  6. <groupId>org.mapstruct</groupId>
  7. <artifactId>mapstruct</artifactId>
  8. <version>${org.mapstruct.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.mapstruct</groupId>
  12. <artifactId>mapstruct-processor</artifactId>
  13. <scope>provided</scope>
  14. <version>1.2.0.Final</version>
  15. </dependency>
  16. <!--mapStruct相关插件-->
  17. <build>
  18. <plugins>
  19. <plugin>
  20. <groupId>org.apache.maven.plugins</groupId>
  21. <artifactId>maven-compiler-plugin</artifactId>
  22. <version>3.8.1</version>
  23. <configuration>
  24. <source>1.8</source>
  25. <!-- depending on your project -->
  26. <target>1.8</target>
  27. <!-- depending on your project -->
  28. <annotationProcessorPaths>
  29. <path>
  30. <groupId>org.mapstruct</groupId>
  31. <artifactId>mapstruct-processor</artifactId>
  32. <version>${org.mapstruct.version}</version>
  33. </path>
  34. <!-- other annotation processors -->
  35. </annotationProcessorPaths>
  36. </configuration></plugin></plugins>
  37. </build>

2、创建car类

  1. package cn.itedus.lottery.test.mapstruct;
  2. import cn.itedus.lottery.test.mapstruct.entity.Brand;
  3. /**
  4. * @author cl
  5. */
  6. public class Car {
  7. private String make;
  8. private int numberOfSeats;
  9. private Brand brand;
  10. public Car(String make, int numberOfSeats, Brand brand) {
  11. this.make = make;
  12. this.numberOfSeats = numberOfSeats;
  13. this.brand = brand;
  14. }
  15. public Car() {
  16. }
  17. public Car(String make, int numberOfSeats) {
  18. this.make = make;
  19. this.numberOfSeats = numberOfSeats;
  20. }
  21. public String getMake() {
  22. return make;
  23. }
  24. public int getNumberOfSeats() {
  25. return numberOfSeats;
  26. }
  27. public void setMake(String make) {
  28. this.make = make;
  29. }
  30. public void setNumberOfSeats(int numberOfSeats) {
  31. this.numberOfSeats = numberOfSeats;
  32. }
  33. public Brand getBrand() {
  34. return brand;
  35. }
  36. public void setBrand(Brand brand) {
  37. this.brand = brand;
  38. }
  39. @Override
  40. public String toString() {
  41. return "Car{" +
  42. "make='" + make + '\'' +
  43. ", numberOfSeats=" + numberOfSeats +
  44. ", brand=" + brand +
  45. '}';
  46. }
  47. }

3、创建carDto类

  1. package cn.itedus.lottery.test.mapstruct;
  2. import cn.itedus.lottery.test.mapstruct.entity.Brand;
  3. /**
  4. * @author cl
  5. */
  6. public class CarDto {
  7. private String make;
  8. private int seatCount;
  9. private String type;
  10. private Brand carBrand;
  11. public CarDto() {
  12. }
  13. public CarDto(String make, int seatCount, String type) {
  14. this.make = make;
  15. this.seatCount = seatCount;
  16. this.type = type;
  17. }
  18. public String getMake() {
  19. return make;
  20. }
  21. public int getSeatCount() {
  22. return seatCount;
  23. }
  24. public String getType() {
  25. return type;
  26. }
  27. public void setMake(String make) {
  28. this.make = make;
  29. }
  30. public void setSeatCount(int seatCount) {
  31. this.seatCount = seatCount;
  32. }
  33. public void setType(String type) {
  34. this.type = type;
  35. }
  36. public CarDto(String make, int seatCount, String type, Brand carBrand) {
  37. this.make = make;
  38. this.seatCount = seatCount;
  39. this.type = type;
  40. this.carBrand = carBrand;
  41. }
  42. public Brand getCarBrand() {
  43. return carBrand;
  44. }
  45. public void setCarBrand(Brand carBrand) {
  46. this.carBrand = carBrand;
  47. }
  48. @Override
  49. public String toString() {
  50. return "CarDto{" +
  51. "make='" + make + '\'' +
  52. ", seatCount=" + seatCount +
  53. ", type='" + type + '\'' +
  54. ", carBrand=" + carBrand +
  55. '}';
  56. }
  57. }

4、创建CarMapper类

  1. package cn.itedus.lottery.test.mapstruct;
  2. import org.mapstruct.Mapper;
  3. import org.mapstruct.Mapping;
  4. import org.mapstruct.Mappings;
  5. import org.mapstruct.factory.Mappers;
  6. /**
  7. * @author cl
  8. */
  9. @Mapper
  10. public interface CarMapper {
  11. CarMapper instantce = Mappers.getMapper(CarMapper.class);
  12. /**
  13. * Car -> CarDto转换
  14. *
  15. * 将源类Car中的字段numberOfSeats转换到CarDto中的seatCount字段中
  16. *
  17. * 表达式如果想自动提示的话,则需要安装mapstruct support插件
  18. *
  19. * @param car
  20. * @return
  21. */
  22. @Mapping(source = "numberOfSeats", target = "seatCount")
  23. @Mapping(target = "type",constant = "bbb")
  24. CarDto carToCarDto(Car car);
  25. @Mappings({
  26. @Mapping(source = "numberOfSeats", target = "seatCount"),
  27. @Mapping(target = "type",constant = "bbb"),
  28. @Mapping(source = "brand",target = "carBrand")
  29. })
  30. CarDto carToCarDtoV2(Car car);
  31. }

5、测试类

  1. public class MsTest {
  2. @Test
  3. public void test_ms(){
  4. CarMapper instantce = CarMapper.instantce;
  5. Car car = new Car();
  6. car.setMake("from");
  7. car.setNumberOfSeats(100);
  8. car.setBrand(new Brand("宝马"));
  9. CarDto carDto = instantce.carToCarDtoV2(car);
  10. System.out.println(car);
  11. System.out.println(carDto);
  12. }
  13. }

测试结果:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,108人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MapStruct实体转换

    摘要: 在实际项目中,我们经常需要将PO转DTO、DTO转PO等一些实体间的转换。比较出名的有BeanUtil 和ModelMapper等,它们使用简单,但是在稍显复杂的业务场