设为首页 加入收藏

TOP

彻底干掉 BeanUtils,最优雅的 Mapstruct 增强工具全新出炉(二)
2023-07-25 21:39:47 】 浏览:46
Tags:干掉 BeanUtils Mapstruct 强工具 全新出
ruct,可以直接替换相关依赖。

单注解即可实现两个对象相互转换

例如快速开始中,只在 User 类上面增加注解 @AutoMapper,Mapstruct Plus 除了会生成 User -> UserDto 的转换接口,默认还会生成 UserDto -> User 的转换接口。

编译后,可以查看生成的类,如下:

image.png

自定义对象类型的属性自动转换

当两个需要转换的对象 AADto,其中的属性 BBDto 是自定义类型,并且也定义了 @AutoMapper 注解的话,在生成转换 AADto 时,会自动依赖属性 BBDto 的转换接口,实现其相应的属性转换。

例如:
分别有两组对象模型:汽车(Car) 和座椅配置(SeatConfiguration),其中 Car 依赖 SeatConfiguration。

两组对象结构一致,只不过代表层级不一样,这里拿 dto 层的对象定义举例:

  • CarDto
@AutoMapper(target = Car.class)
public class CarDto {
    private SeatConfigurationDto seatConfiguration;
}
  • SeatConfiguration
@AutoMapper(target = SeatConfiguration.class)
public class SeatConfigurationDto {
    private int seatCount;
}

测试:

@Test
public void carConvertTest() {
    CarDto carDto = new CarDto();
    SeatConfigurationDto seatConfigurationDto = new SeatConfigurationDto();
    seatConfigurationDto.setSeatCount(4);
    carDto.setSeatConfiguration(seatConfigurationDto);

    final Car car = converter.convert(carDto, Car.class);
    System.out.println(car);    // Car(seatConfiguration=SeatConfiguration(seatCount=4))

    assert car.getSeatConfiguration() != null;
    assert car.getSeatConfiguration().getSeatCount() == 4;
}

单个对象对多个对象进行转换

Mapstruct Plus 提供了 @AutoMappers 注解,支持配置多个目标对象,进行转换。

例如:有三个对象,UserUserDtoUserVOUser 分别要和其他两个对象进行转换。则可以按如下配置:

@Data
@AutoMappers({
    @AutoMapper(target = UserDto.class),
    @AutoMapper(target = UserVO.class)
})
public class User {
    // ...
}

Map 转对象

Mapstruct Plus 提供了 @AutoMapMapper 注解,支持生成 Map<String, Object> 转换为当前类的接口。同时,还支持 map 中嵌套 Map<String, Object> 转换为自定义类嵌套自定义类的场景。

其中,map 中的 value 支持的类型如下:

  • String
  • BigDecimal
  • BigInteger
  • Integer
  • Long
  • Double
  • Number
  • Boolean
  • Date
  • LocalDateTime
  • LocalDate
  • LocalTime
  • URI
  • URL
  • Calendar
  • Currency
  • 自定义类(自定义类也需要增加 @AutoMapMapper 注解

例如:

有如下两个接口:

  • MapModelA
@Data
@AutoMapMapper
public class MapModelA {

    private String str;
    private int i1;
    private Long l2;
    private MapModelB mapModelB;

}
  • MapModelB
@Data
@AutoMapMapper
public class MapModelB {

    private Date date;
    
}

测试:

@Test
public void test() {
    Map<String, Object> mapModel1 = new HashMap<>();
    mapModel1.put("str", "1jkf1ijkj3f");
    mapModel1.put("i1", 111);
    mapModel1.put("l2", 11231);

    Map<String, Object> mapModel2 = new HashMap<>();
    mapModel2.put("date", DateUtil.parse("2023-02-23 01:03:23"));

    mapModel1.put("mapModelB", mapModel2);

    final MapModelA mapModelA = converter.convert(mapModel1, MapModelA.class);
    System.out.println(mapModelA);  // MapModelA(str=1jkf1ijkj3f, i1=111, l2=11231, mapModelB=MapModelB(date=2023-02-23 01:03:23))
}

支持自定义转换

自定义属性转换

Mapstruct Plus 提供了 @AutoMapping 注解,该注解在编译后,会变为 Mapstruct 中的 @Mapping 注解,已经实现了几个常用的注解属性。

指定不同名称属性字段映射转换

例如,Car 类中属性 wheels 属性,转换 CarDto 类型时,需要将该字段映射到 wheelList 上面,可以在 wheels 上面增加如下注解:

@AutoMapper(target = CarDto.class)
public class Car {
    @AutoMapping(target = "wheelList")
    private List<String> wheels;
}

自定义时间格式化

在将 Date 类型的属性,转换为 String 类型时,可以通过 dateFormat 来指定时间格式化:

@AutoMapper(target = Goods.class)
public class GoodsDto {

    @AutoMapping(target = "takeDownTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date takeDownTime;

}

自定义数字格式化

当数字类型(doublelongBigDecimal)转换

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring IOC官方文档学习笔记(五.. 下一篇静态类和非静态类 抽象类

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目