当前位置:网站首页>Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!
Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!
2022-06-21 11:38:00 【Java confidant_】
The 3rd of this month ,MapStruct 1.5.0 Final Release , This official version is almost past the release of the last official version 7 year ( The last official version was released in 2015 year 11 month ), This release fixes 110 Multiple bug Outside , There are also some new features to watch out for :
Support
Map<String,?>TobeanTransformationSupport more complete conditional conversion (Conditional mapping)
Support the conversion between subclasses (Support for subclass mapping)
1. newly added Map To Java bean Transformation (Mapping from Map to Bean)
If we have the following Java Bean
public class Customer {
private Long id;
private String name;
//getters and setter omitted for brevity
} Corresponding MapStruct The code is as follows :
@Mapper
public interface CustomerMapper {
@Mapping(target = "name", source = "customerName")
Customer toCustomer(Map<String, String> map);
}That will eventually generate conversion code similar to the following :
// GENERATED CODE
public class CustomerMapperImpl implements CustomerMapper {
@Override
public Customer toCustomer(Map<String, String> map) {
// ...
if ( map.containsKey( "id" ) ) {
customer.setId( Integer.parseInt( map.get( "id" ) ) );
}
if ( map.containsKey( "customerName" ) ) {
customer.setName( source.get( "customerName" ) );
}
// ...
}
} But be careful , To be converted Map Of key Must be String Type of , otherwise , The conversion code will skip this key
2. More complete conditional transformation ( Conditional Mapping)
from 1.5.0 Final Before the release , If Java bean contains hasXXX perhaps isXXX Such methods (XXX yes bean Attribute name in ), be MapStruct In the generated code, this kind of method will be called to determine whether it is in the converted bean Whether the original attribute , But unfortunately , Most of the time , We cannot directly modify the original bean Code for . Based on this ,1.5.0 Final Version introduced org.mapstruct.Condition Annotation to implement conditional transformation . For example, we have the following conversion code :
@Mapper
public interface CarMapper {
CarDto carToCarDto(Car car);
@Condition
default boolean isNotEmpty(String value) {
return value != null && !value.isEmpty();
}
} be Map Struct 1.5.0 Final The generated code is :
// GENERATED CODE
public class CarMapperImpl implements CarMapper {
@Override
public CarDto carToCarDto(Car car) {
if ( car == null ) {
return null;
}
CarDto carDto = new CarDto();
if ( isNotEmpty( car.getOwner() ) ) {
carDto.setOwner( car.getOwner() );
}
// Mapping of other properties
return carDto;
}
}org.mapstruct.Condition Except for the whole bean You can also modify specific attribute values , Realization bean Conditional transformation of attribute dimension .
3. Add support for subclass conversion (Subclass Mapping)
If there is a parent class Fruit And two subclasses Apple and Banana, With the support of new features, our conversion code can be written more succinctly :
@Mapper
public interface FruitMapper {
@SubclassMapping( source = AppleDto.class, target = Apple.class )
@SubclassMapping( source = BananaDto.class, target = Banana.class )
Fruit map( FruitDto source );
} If Fruit It is an abstract class or interface , Compile error will be reported .
recommend
Technical involution group , Learn together !!

PS: Because the official account platform changed the push rules. , If you don't want to miss the content , Remember to click after reading “ Looking at ”, Add one “ Star standard ”, In this way, each new article push will appear in your subscription list for the first time . spot “ Looking at ” Support us !
边栏推荐
- 学者魔改 Qt Creator 插件框架(附实例)
- 2022年安全员-A证考试模拟100题及在线模拟考试
- 巴比特 | 元宇宙每日必读:微信或首次以“涉数字藏品二级交易”为由封禁一公众号,平台新规也对此提出警告...
- Devsecops: ten things that should be done well
- 服务器安全审计系统设计与实现
- 有意思的鼠标指针交互探究
- harmonyos培訓一
- 618掘金数字藏品?Burberry等奢侈品牌鏖战元宇宙
- qt对sqlite数据库多线程的操作
- Hezhili: microbial driven carbon nitrogen sulfur phosphorus cycle in mangrove wetland sediments and its coupling mechanism
猜你喜欢

You must get started with boost

qt对sqlite数据库多线程的操作

【哈尔滨工业大学】考研初试复试资料分享

一文速学-玩转MySQL时间运算函数以及时间匹配操作详解+实例代码

Est le logiciel d'oscilloscope allemand, le logiciel d'ordinateur hôte d'oscilloscope keysight NS scope

Operation control entry to Fang Si

考研政英平均分出炉!你目前什么水平?

QT operation on SQLite database multithreading

It is the German oscilloscope software and the keysight oscilloscope upper computer software ns-scope

Deep water area involvement
随机推荐
QML introduction to advanced
Never ending database injection attack and defense
使用Huggingface在矩池云快速加载预训练模型和数据集
工厂模式实现
游戏机之AR机械臂
Break down tasks
SSD [target detection]
A Kuan food: the battle for "the first share of convenience food" continues
You must get started with boost
广东发产品检测券,消费者也有份
[yolov5s target detection] opencv loads onnx model for reasoning on GPU
有意思的鼠标指针交互探究
Introduction to Clair, a container static security vulnerability scanning tool
Machine learning 2-linear regression
是德示波器软件,Keysight示波器上位机软件NS-Scope
分解任务
Research on DDoS protection for overseas business of Chinese Enterprises
【100个 Unity实用技能】| 游戏中获取鼠标点击的坐标,并将游戏对象移动到鼠标的点击位置
Flynk CDC reads MySQL 8 hours late. Set the servertimezone parameter
Operation and maintenance security, not so simple