首頁 > 軟體

mapstruct的用法之qualifiedByName範例詳解

2022-04-06 13:01:50

qualifiedByName的意思就是使用這個Mapper介面中的指定的預設方法去處理這個屬性的轉換,而不是簡單的get set。網上一直沒找到…

可用於格式化小數位等,在po轉換為vo時就已格式化小數位完成,所以不必單獨再寫程式碼處理小數位。

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")//需要起個名字,不然報錯,可以與方法名一致,當然也可以不一致
    default Double formatDouble(Double source) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");//小數位格式化
        if (source == null) {
            source = 0.0;
        }
        return Double.parseDouble(decimalFormat.format(source));
    }
}

3 定義源類和目標類

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}

關於@Target註解的使用可見:

詳解JDK 5 Annotation 註解之@Target的用法介紹

到此這篇關於mapstruct的用法之qualifiedByName範例詳解的文章就介紹到這了,更多相關mapstruct的用法內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com