溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mapstruct的qualifiedByName怎么用

發(fā)布時間:2022-04-06 11:09:19 來源:億速云 閱讀:532 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“mapstruct的qualifiedByName怎么用”的相關(guān)知識,小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“mapstruct的qualifiedByName怎么用”文章能幫助大家解決問題。

可用于格式化小數(shù)位等,在po轉(zhuǎn)換為vo時就已格式化小數(shù)位完成,所以不必單獨(dú)再寫代碼處理小數(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)于“mapstruct的qualifiedByName怎么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點(diǎn)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI