mapstruct的用法之qualifiedByName示例详解

2022-07-15,,,

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

《mapstruct的用法之qualifiedByName示例详解.doc》

下载本文的Word格式文档,以方便收藏与打印。