choropleth map_Mapsource

choropleth map_Mapsource简介MapStruct是满足JSR269规范的一个Java注解处理器,用于为JavaBean生成类型安全且高性能的映射。它基于编译阶段生成get/set代码,此实现过程中没有反射,不会造成额外的性能损失。您所要做的就是定义一个mapper接口(@Mapper),该接口用于声明所有必须的映射方法。在编译期间MapStruct会为该接口自动生成实现类。该实现类使用简单的Java方法调用来映射source-target对象,在此过程中没有反射或类似的行为发生。性能优点与手工编..

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

简介

MapStruct是满足JSR269规范的一个Java注解处理器,用于为Java Bean生成类型安全且高性能的映射。它基于编译阶段生成get/set代码,此实现过程中没有反射,不会造成额外的性能损失。

您所要做的就是定义一个mapper接口(@Mapper),该接口用于声明所有必须的映射方法。在编译期间MapStruct会为该接口自动生成实现类。该实现类使用简单的Java方法调用来映射source-target对象,在此过程中没有反射或类似的行为发生。

性能

choropleth map_Mapsource

优点

与手工编写映射代码相比

  • MapStruct通过生成冗长且容易出错的代码来节省时间。

与动态映射框架相比

  • 简单泛型智能转换;
  • 效率高:无需手动 set/get 或 implements Serializable 以达到深拷贝;
  • 性能更高:使用简单的Java方法调用代替反射;
  • 编译时类型安全:只能映射相同名称或带映射标记的属性;
  • 编译时产生错误报告:如果映射不完整(存在未被映射的目标属性)或映射不正确(找不到合适的映射方法或类型转换)则会在编译时抛出异常。

使用

1、引入 Pom

方法一

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>1.2.0.Final</version>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.2.0.Final</version>
</dependency>

Jetbrains全家桶1年46,售后保障稳定

方法二

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.0.Final</version>
</dependency>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>8</source>
        <target>8</target>
        <encoding>UTF-8</encoding>
        <annotationProcessorPaths>
            <!-- 必须要加, 否则生成不了 MapperImpl 实现类 -->
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.0.Final</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

2、注入方式

  • Spring 注入方式
/**
 * @author Lux Sun
 * @date 2021/2/22
 */
@Mapper(componentModel = "spring")
public interface ApiMapper {
}
  • ClassLoader 加载方式
/**
 * @author Lux Sun
 * @date 2021/2/22
 */
@Mapper
public interface ApiMapper {
    ApiMapper INSTANCE = Mappers.getMapper(ApiMapper.class);
}

3、案例(ApiVO 转 ApiDTO)

ApiMapper

/**
 * @author Lux Sun
 * @date 2021/2/22
 */
@Mapper(componentModel = "spring")
public interface ApiMapper {
    // ClassLoader 加载方式
    ApiMapper INSTANCE = Mappers.getMapper(ApiMapper.class);

    /**
     * ApiVO 转 ApiDTO
     * @param apiVO
     */
    @Mapping(source = "method", target = "apiMethod")
    ApiDTO apiVoToApiDto(ApiVO apiVO);
}

ApiMapperTest

@Resource
private ApiMapper apiMapper;

/**
 * ApiVO 转 ApiDTO
 * @param
 * @return void
 */
public void apiVoToApiDtoTest() {
    // 创建 ApiPOList
    ApiPO apiPO1 = ApiPO.builder().name("apiName1").build();
    List<ApiPO> apiPOList = Lists.newArrayList();
    apiPOList.add(apiPO1);
    
    // 创建 ApiConf
    ApiVO.ApiConf apiConf = ApiVO.ApiConf.builder().id("123456").build();
    
    // 创建数字 List
    List<Integer> numberList = Lists.newArrayList();
    numberList.add(1);
    numberList.add(2);
    
    // 创建 ApiVO
    ApiVO apiVO = ApiVO.builder().name("apiName").method(1)
                                .apiPOList(apiPOList).apiConf(apiConf)
                                .list(numberList).build();
    
    // 转换结果 ApiDTO
    // ClassLoader 调用方式
    ApiDTO apiDTO = ApiMapper.INSTANCE.apiVoToApiDto(apiVO);
    // Spring 调用方式
    // ApiDTO apiDTO = apiMapper.apiVoToApiDto(apiVO);
    System.out.println(apiDTO);
}

结果输出

ApiDTO(name=apiName, apiMethod=1, apiConf=ApiDTO.ApiConf(id=123456, open=null), apiPOList=[ApiPO(name=apiName1, method=null, apiConf=null, apiPOList=null)], list=[1, 2])

原理

通过 JSR 269 Java注解处理器自动生成该文件

choropleth map_Mapsource

ApiMapperImpl

package com.luxsun.platform.lux.kernel.common.convert;
import com.luxsun.platform.lux.kernel.common.domain.dto.ApiDTO;
import com.luxsun.platform.lux.kernel.common.domain.dto.ApiDTO.ApiConf;
import com.luxsun.platform.lux.kernel.common.domain.po.ApiPO;
import com.luxsun.platform.lux.kernel.common.domain.vo.ApiVO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-02-22T14:48:03+0800",
    comments = "version: 1.2.0.Final, compiler: javac, environment: Java 1.8.0_121 (Oracle Corporation)"
)
@Component
public class ApiMapperImpl implements ApiMapper {
    @Override
    public ApiDTO apiVoToApiDto(ApiVO apiVO) {
        if ( apiVO == null ) {
            return null;
        }
        ApiDTO apiDTO = new ApiDTO();
        apiDTO.setApiMethod( apiVO.getMethod() );
        apiDTO.setName( apiVO.getName() );
        apiDTO.setApiConf( apiConfToApiConf( apiVO.getApiConf() ) );
        List<ApiPO> list = apiVO.getApiPOList();
        if ( list != null ) {
            apiDTO.setApiPOList( new ArrayList<ApiPO>( list ) );
        }
        else {
            apiDTO.setApiPOList( null );
        }
        apiDTO.setList( integerListToStringList( apiVO.getList() ) );
        return apiDTO;
    }
    protected ApiConf apiConfToApiConf(com.luxsun.platform.lux.kernel.common.domain.vo.ApiVO.ApiConf apiConf) {
        if ( apiConf == null ) {
            return null;
        }
        ApiConf apiConf1 = new ApiConf();
        apiConf1.setId( apiConf.getId() );
        apiConf1.setOpen( apiConf.getOpen() );
        return apiConf1;
    }
    protected List<String> integerListToStringList(List<Integer> list) {
        if ( list == null ) {
            return null;
        }
        List<String> list1 = new ArrayList<String>( list.size() );
        for ( Integer integer : list ) {
            list1.add( String.valueOf( integer ) );
        }
        return list1;
    }
}

Ps:这就是为什么 mapstruct 的效率比较高的原因,相比于反射获取对象进行拷贝的方法,这种更贴近于原生 get/set 方法的框架显得更为高效。这个文件是通过在 mapper 中的注解,使用生成映射器的注解处理器从而自动生成了这段代码。

注意事项

  • 开发过程中遇到修改属性时,重新启动项目还是复制的时候新添加的属性为 null,此时,因为自动生成的实现类并没有重新编译,需要手动在 target 包中找到删除,再重新启动即可!

FAQ

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213391.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 简单window.open()使用方法和按钮关闭window.open页面

    简单window.open()使用方法和按钮关闭window.open页面简单window.open()使用方法和按钮关闭window.open页面

    2022年4月23日
    147
  • rc522优点_51单片机rc522接线图

    rc522优点_51单片机rc522接线图公司需要做刷卡模块,因此选了RC522做demo程序。下面就RC522知识做简要的总结。本人使用stm32的硬件spi接口搭建工程,相关的配置如下:spi配置:引脚配置SDA——-PA4SCLK—-PB13MOSI——-PB15MISO——PB14IRQ—没接RST—-PB0//PB12–NSSPB13–SCKPB14–…

    2022年9月19日
    2
  • Linux下使用thrfit

    Linux下使用thrfit

    2021年11月19日
    51
  • 快速幂的大数运算_快速幂模

    快速幂的大数运算_快速幂模快速幂运算1.什么是快速幂2.快速幂的“小数”运算3.高精度(大数)的快速幂1.什么是快速幂快速幂,是指在进行幂运算的时候,用一种快速方法得出答案。比如,要求2^100的值,那按照最简单的方式,就是一个一个2去相乘,然后最终得到答案,那么这样就要计算100次,非常浪费时间,那么快速幂就是使用一种技巧使得将其计算次数减少,快速得到答案。2.快速幂的“小数”运算对于系统内置类型的整型,暂且叫他“小数”,这个时候进行快速幂运算,代码如下:#include<cstdio>#include&l

    2025年7月14日
    3
  • linux安装wget命令_linux下载文件到本地命令

    linux安装wget命令_linux下载文件到本地命令1、检查是否有安装wgetrpm-qa|grep”wget”若存在则移除,以下为移除命令#移除wgetyumremovewget2、登录wget官网下载地址,下载最新的wget的rpm安装包到本地下载地址:http://mirrors.163.com/centos/7/os/x86_64/Packages/3、将下载的wget上传到服务器#/usr/local目录下手动创建一个wget将下载好的wget-1.14-18.el7_6.1.x86_64.rpm上传到此目录下

    2022年10月16日
    4
  • 一步一步画战车(雷电3接口)

    在用EXCEL做量化分析的时候,经常需要通过一些金融数据平台的API接口,获取各种数据。最常用的公共API接口有YahooFinance,GoogleFinance,新浪财经,搜狐财经等。这些都不需要注册,就可以直接使用。获取方式相对简单,但数据种类不够丰富,通常只包括交易数据和财务数据。另外一些免费的金融数据平台,如国外的Quandl和国内的Tushare也都提供了API接口,数据种类更…

    2022年4月10日
    121

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号