当前位置: 首页 > news >正文

日本r影片网站做我的奴隶/晚上看b站

日本r影片网站做我的奴隶,晚上看b站,自己开网站做职称论文可以吗,深圳市招投标交易中心网站概述 针对模型转换,我们常用的方式是 方式一:BeanUtils.copyProperties(source, target); 方式二:自己写converter方法。 下面讲一个目前主流推荐的做法:使用mapstruct。 mapstruct本质就是自动帮我们生成转换代码。我们只需要配…

概述

针对模型转换,我们常用的方式是
方式一:BeanUtils.copyProperties(source, target);
方式二:自己写converter方法。

下面讲一个目前主流推荐的做法:使用mapstruct
mapstruct本质就是自动帮我们生成转换代码。我们只需要配置好策略即可。

依赖

...
<properties><org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
</properties>
...
<dependencies><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>${org.mapstruct.version}</version></dependency>
</dependencies>
...
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><annotationProcessorPaths><!-- 注意顺序,lombok在前,mapstruct在后 --><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></path><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${org.mapstruct.version}</version></path></annotationProcessorPaths></configuration></plugin></plugins>
</build>

定义mapper

为了告诉mapstruct怎么生成转换代码,我们需要先定义个接口。官方把这个操作称为:定义mapper。(创建映射器)

@Mapper
public interface CarMapper {CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);// 模型转换UnityDTO modelToDTO(UnityModel unityModel);
}

两个模型如下:

@Data
public class UnityDTO {/*** 纠纷单Id*/private String disputeOrderId;/*** 订单Id*/private Long orderId;
}@Data
public class UnityModel {/*** 纠纷单Id*/private String disputeOrderId;/*** 订单Id*/private Long orderId;
}

这个时候只需要,编辑下项目,mapstruct就会帮我们把转换代码生成出来。生成的代码在target/classes文件夹里。

如果是简单使用,理论上来说,其实已经结束了。不过只是简单的使用肯定满足不了我们的需求。

字段名不一致的情况

简单情况

如果两个字段名不一致,如下,一个orderId,一个是oId。

@Data
public class UnityDTO {/*** 纠纷单Id*/private String disputeOrderId;/*** 订单Id*/private Long orderId;
}@Data
public class UnityModel {/*** 纠纷单Id*/private String disputeOrderId;/*** 订单Id*/private Long oId;
}

指定映射字段:

@Mapper
public interface CarMapper {CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);// 模型转换@Mapping(target = "orderId", source = "oId")UnityDTO modelToDTO(UnityModel unityModel);
}

复杂情况

expression

下面情况是:model里面平铺的字段,转换到DTO中时,希望能放到extInfo的map中:

public class UnityModel {private String reverseBizType;private String reverseOrderBizType;
}public class UnityDTO {/*** 纠纷单Id*/private Map<String, String> extInfo;
}
/*** 字段不同时的指定方式,多个字段不同,写多个@Mapping即可*/
@Mappings({@Mapping(target = "extInfo", expression = "java(INSTANCE.fillExtInfo(unityModel))")
})
UnityDTO modelToDTO(UnityModel unityModel);default Map<String, String> fillExtInfo(JudgementUnityModel unityModel) {Map<String, String> map = new HashMap<>();Map<String, String> extInfo = unityModel.getExtInfo();if (extInfo != null) {map.putAll(extInfo);}map.put("reverseBizType", unityModel.getReverseBizType());map.put("reverseOrderBizType", unityModel.getReverseOrderBizType());return map;
}

这里是利用expression的能力,单独调用fillExtInfo方法。
具体步骤:

  1. 使用@Mapping(target = "目标字段名", expression = "表达式")
  2. 编写表达式中的方法,入参为source模型。

afterMapping

除了使用expression的方式之外,还可以使用@AfterMapping

@AfterMapping
default void fillExtInfo(@MappingTarget JudgementUnityMqModel unityMqModel, JudgementUnityModel unityModel){Map<String, String> map = fillExtInfo(unityModel);unityMqModel.setExtInfo(map);
}

mapstruct生成的方法后,fillExtInfo会放在方法的最后。

说明:

  1. @MappingTarget用来指定需要更新的对象。或者说,假设我们不想new一个对象,就可以使用@MappingTarget进行指定。
  2. @AfterMapping作用于生成方法的尾部,并且作用于相同映射关系的全部方法。

具有多个源参数的映射方法

@Mapper
public interface AddressMapper {@Mapping(target = "description", source = "person.description")@Mapping(target = "houseNumber", source = "address.houseNo")DeliveryAddressDto personAndAddressToDeliveryAddressDto(Person person, Address address);
}

没生效的情况

对象模型继承父类字段

父类字段没有生成 setXXX代码。

多半是因为lombokmapstruct 先后顺序导致的:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><annotationProcessorPaths><!-- 注意顺序,lombok在前,mapstruct在后 --><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></path><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${org.mapstruct.version}</version></path></annotationProcessorPaths></configuration>
</plugin>

总结

目前常用的:

  1. @Mapping 对字段一一指定
  2. 使用express指定转换方法
  3. 使用@afterMapping是放到方法的尾部。当然也有@beforeMapping,它有点特点,如果没有@MappingTarget,那么会在创建bean之前调用,如果有,就是创建bean之后调用。具体参考官方文档。

参考地址:

mapstruct官方文档

http://www.lbrq.cn/news/1598293.html

相关文章:

  • 折扣券网站怎么做/山东最新消息今天
  • 怎样做营销型网站推广/流量平台有哪些
  • 做网站如何防止被坑/哈尔滨seo优化软件
  • dreamweaver设计网页步骤/自己做seo网站推广
  • 北京网站建设 降龙网/2022最新小学生新闻
  • 北京网站开发建设/软文营销文章
  • 自己做网站的图片/中国十大搜索引擎排名最新
  • 中国容桂品牌网站建设/百度app内打开
  • 牡丹江做网站的公司/电商网站建设公司哪家好
  • 建筑工程网登/网站seo优化技巧
  • 服务器不是自己的做违法网站/青岛的seo服务公司
  • discuz修改网站底部/百度网站名称和网址
  • 广州宝盈网络科技有限公司网站/郑州网站优化排名
  • wordpress php 5.2/seo搜索优化是什么
  • 设计一个网站的步骤/沈阳优化网站公司
  • 给赌博网站做设计/18岁以上站长统计
  • 电子商务网站建设的基本要素/aso优化分析
  • 阿里云做网站要几天/销售网络平台推广
  • 买下云服务器怎么做网站/如何推广自己产品
  • 深圳市住房城乡建设局网站/做一个公司网站需要多少钱
  • 浙江网站建设推广公司哪家权威/网站免费高清素材软件
  • 企业做网站需要哪些人员/免费下载百度
  • 四川省住房与城乡建设 厅网站/谁有恶意点击软件
  • 宝塔怎么做两个网站/网站推广平台有哪些
  • 网站搭建吧/盘多多搜索引擎入口
  • 北京市住房和建设委员会网站/重庆seo技术博客
  • 专门做资产负债表结构分析的网站/交换友链平台
  • 建湖哪家专业做网站/模板建站
  • 莱阳网站定制/网站检测
  • 新建网站做优化/西地那非片的功能主治和副作用
  • 【C++】Stack and Queue and Functor
  • Java学习第一百零六部分——Lucene
  • linux_https,udp,tcp协议(更新中)
  • 【Linux】System V - 基于建造者模式的信号量
  • 多级表头的导出
  • C++入门自学Day5-- C/C++内存管理(续)