您现在的位置是:首页 > 文章详情

MapStructPlus 1.2.2 版本发布,增加枚举自动转换

日期:2023-03-27点击:937

当需要进行枚举转换时(例如枚举转换为编码值,或者又编码转换为枚举),可以在目标枚举添加 @AutoEnumMapper 注解, 增加该注解后,在任意类型中需要转换该枚举时都可以自动转换。

使用该注解需要注意:当前注解必须有一个可以保证唯一的字段,并在使用当前注解时,将该字段名,添加到注解提供的 value 属性中。

还有就是注解和使用注解的类,要在同一个模块中

例如:

  • 商品状态枚举(Goods
 @Getter @AllArgsConstructor @AutoEnumMapper("state") public enum GoodsStateEnum {  ENABLED(1, "启用"),  DISABLED(0, "禁用");  private final Integer state;  private final String desc; }
在当前枚举中添加注解   @AutoEnumMapper,且指定唯一字段为   state
  • 商品类(Goods
 @Data @AutoMapper(target = GoodsVo.class, reverseConvertGenerate = false) public class Goods {  private GoodsStateEnum state; }
  • 商品VO对象(GoodsVo
 @Data public class GoodsVo {  private Integer state; }
  • 测试类
 @Test public void enumMapTest() {  final GoodsVo goodsVo = converter.convert(goods, GoodsVo.class);  System.out.println(goodsVo);  Assert.equals(goodsVo.getState(), goods.getState().getState());  final Goods goods2 = converter.convert(goodsVo, Goods.class);  System.out.println(goods2);  Assert.equals(goods2.getState(), GoodsStateEnum.ENABLED); }
原文链接:https://www.oschina.net/news/234332/mapstructplus-1-2-2-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章