广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mapstruct的用法之qualifiedByName示例详解
  • 146
分享到

mapstruct的用法之qualifiedByName示例详解

2024-04-02 19:04:59 146人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

qualifiedByName的意思就是使用这个Mapper接口中的指定的默认方法去处理这个属性的转换,而不是简单的get set。网上一直没找到… 可用于格式化小数位

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;

@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的用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: mapstruct的用法之qualifiedByName示例详解

本文链接: https://www.lsjlt.com/news/145051.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作