mapstruct的用法之qualifiedByName示例詳解
qualifiedByName的意思就是使用這個(gè)Mapper接口中的指定的默認(rèn)方法去處理這個(gè)屬性的轉(zhuǎn)換,而不是簡(jiǎn)單的get set。網(wǎng)上一直沒找到…
可用于格式化小數(shù)位等,在po轉(zhuǎn)換為vo時(shí)就已格式化小數(shù)位完成,所以不必單獨(dú)再寫代碼處理小數(shù)位。
1 引用pom1 ,能正常使用mapstruct的注解,但不會(huì)生成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 才會(huì)生成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")//需要起個(gè)名字,不然報(bào)錯(cuò),可以與方法名一致,當(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 測(cè)試
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java微信公眾號(hào)開發(fā)(搭建本地測(cè)試環(huán)境)
這篇文章主要介紹了java微信公眾號(hào)開發(fā),主要內(nèi)容有測(cè)試公眾號(hào)與本地測(cè)試環(huán)境搭建,需要的朋友可以參考下2015-12-12
淺談mybatis返回單一對(duì)象或?qū)ο罅斜淼膯栴}
這篇文章主要介紹了淺談mybatis返回單一對(duì)象或?qū)ο罅斜淼膯栴},具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring?@DateTimeFormat日期格式化時(shí)注解場(chǎng)景分析
這篇文章主要介紹了Spring?@DateTimeFormat日期格式化時(shí)注解場(chǎng)景分析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
SpringBoot全局異常與數(shù)據(jù)校驗(yàn)的方法
這篇文章主要介紹了SpringBoot全局異常與數(shù)據(jù)校驗(yàn)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
Java中使用synchronized關(guān)鍵字實(shí)現(xiàn)簡(jiǎn)單同步操作示例
這篇文章主要介紹了Java中使用synchronized關(guān)鍵字實(shí)現(xiàn)簡(jiǎn)單同步操作示例,本文起講解了synchronized修飾函數(shù)、synchronized修飾代碼塊、synchronized修飾靜態(tài)方法等內(nèi)容,需要的朋友可以參考下2015-04-04

