mapstruct的qualifiedByName怎么用

发布时间:2022-04-06 11:09:19 作者:iii
来源:亿速云 阅读:511

这篇文章主要介绍“mapstruct的qualifiedByName怎么用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“mapstruct的qualifiedByName怎么用”文章能帮助大家解决问题。

可用于格式化小数位等,在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}

关于“mapstruct的qualifiedByName怎么用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. MapStruct实体转换及List转换的方法讲解
  2. MapStruct实体间转换的简单用法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mapstruct

上一篇:FreeRTOS实时操作系统结构是什么

下一篇:python怎么使用opencv对图像添加噪声

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》