mapstruct的用法之qualifiedByName示例詳解
qualifiedByName的意思就是使用這個Mapper接口中的指定的默認方法去處理這個屬性的轉(zhuǎn)換,而不是簡單的get set。網(wǎng)上一直沒找到…
可用于格式化小數(shù)位等,在po轉(zhuǎn)換為vo時就已格式化小數(shù)位完成,所以不必單獨再寫代碼處理小數(shù)位。
1 引用pom1 ,能正常使用mapstruct的注解,但不會生成Impl類
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 --> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>1.2.0.Final</version> </dependency>
引用pom2 才會生成Impl類
2 定義ConvertMapper
package com.weather.weatherexpert.common.model.mapper; import com.weather.weatherexpert.common.model.po.AreaPO; import com.weather.weatherexpert.common.model.vo.AreaVO; import org.mapstruct.MapMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Named; import org.mapstruct.factory.Mappers; import java.text.DecimalFormat; /** * <p>Title: </p> * <p>Description: </p> * */ @Mapper public interface ConvertMapper { ConvertMapper INSTANCE = Mappers.getMapper(ConvertMapper.class); @Mapping(source = "pm25", target = "pm25", qualifiedByName = "formatDoubleDef") AreaVO areaPO2areaVO(AreaPO areaPO); @Named("formatDoubleDef")//需要起個名字,不然報錯,可以與方法名一致,當(dāng)然也可以不一致 default Double formatDouble(Double source) { DecimalFormat decimalFormat = new DecimalFormat("0.00");//小數(shù)位格式化 if (source == null) { source = 0.0; } return Double.parseDouble(decimalFormat.format(source)); } }
3 定義源類和目標(biāo)類
public class AreaPO { private String cityName; private Integer haveAir; private Double pm25; private String pm10Str; ............ } public class AreaVO { private String cityName; private Integer haveAir; private Double pm25; private String pm25Str; private Double pm10; ...... }
4 看生成的Impl類ConvertMapperImpl
package com.weather.weatherexpert.common.model.mapper; import com.weather.weatherexpert.common.model.po.AreaPO; import com.weather.weatherexpert.common.model.vo.AreaVO; public class ConvertMapperImpl implements ConvertMapper { public ConvertMapperImpl() { } public AreaVO areaPO2areaVO(AreaPO areaPO) { if (areaPO == null) { return null; } else { AreaVO areaVO = new AreaVO(); areaVO.setPm25(this.formatDouble(areaPO.getPm25())); areaVO.setCityName(areaPO.getCityName()); areaVO.setHaveAir(areaPO.getHaveAir()); return areaVO; } }
5 測試
AreaPO areaPO = new AreaPO("忻州", 1, 1.256879); AreaVO areaVO = ConvertMapper.INSTANCE.areaPO2areaVO(areaPO); logger.info("JSON.toJSONString(areaVO):" + JSON.toJSONString(areaVO));
輸出:
JSON.toJSONString(areaVO):{“cityName”:“忻州”,“haveAir”:1,“pm25”:1.26}
關(guān)于@Target注解的使用可見:
詳解JDK 5 Annotation 注解之@Target的用法介紹
到此這篇關(guān)于mapstruct的用法之qualifiedByName示例詳解的文章就介紹到這了,更多相關(guān)mapstruct的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java微信公眾號開發(fā)(搭建本地測試環(huán)境)
這篇文章主要介紹了java微信公眾號開發(fā),主要內(nèi)容有測試公眾號與本地測試環(huán)境搭建,需要的朋友可以參考下2015-12-12Spring?@DateTimeFormat日期格式化時注解場景分析
這篇文章主要介紹了Spring?@DateTimeFormat日期格式化時注解場景分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05SpringBoot全局異常與數(shù)據(jù)校驗的方法
這篇文章主要介紹了SpringBoot全局異常與數(shù)據(jù)校驗的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11Java中使用synchronized關(guān)鍵字實現(xiàn)簡單同步操作示例
這篇文章主要介紹了Java中使用synchronized關(guān)鍵字實現(xiàn)簡單同步操作示例,本文起講解了synchronized修飾函數(shù)、synchronized修飾代碼塊、synchronized修飾靜態(tài)方法等內(nèi)容,需要的朋友可以參考下2015-04-04