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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python爬虫超详细讲解(零基础入门,老年人都看的懂)

    Python爬虫超详细讲解(零基础入门,老年人都看的懂)关于Python爬虫的超详细讲解,用例子来给大家一步步分析爬虫的代码原理,由浅入深,老年人来了,我也给你整明白。

    2022年6月14日
    34
  • WEB日志格式

    WEB日志格式WEB日志格式日志格式类型:常见日志格式:参考:WEB日志格式CustomLogFormats:普通日志格式日志格式类型:目前常见的WEB日志格式主要由两类Apache的NCSA日志格式,NCSA格式分为NCSA普通日志格式(CLF)NCSA扩展日志格式(ECLF)IIS的W3C日志格式目前最常用的是NCSA扩展日志格式(ECLF…

    2022年6月10日
    30
  • PHP中如何使用Redis接管文件存储Session详解

    PHP中如何使用Redis接管文件存储Session详解

    2021年11月4日
    45
  • python字符串转换成数字_python 字符与数字如何转换[通俗易懂]

    展开全部一、python中字符串转换成数字1、类中进行导入:importstring,str=’555’,num=string.atoi(str),num即为str转换成的数字转换为浮点数:string.atof(str)2、直接62616964757a686964616fe59b9ee7ad9431333365666261intint(str)即可。二、数字转换成字符串num=322,str…

    2022年4月18日
    82
  • ExecutorService 的理解与使用「建议收藏」

    ExecutorService 的理解与使用「建议收藏」接口java.util.concurrent.ExecutorService表述了异步执行的机制,并且可以让任务在后台执行。壹個ExecutorService实例因此特别像壹個线程池。事实上,在java.util.concurrent包中的ExecutorService的实现就是壹個线程池的实现。ExecutorService样例这里有壹個简单的使用Java实现的

    2022年9月10日
    0
  • zv-1像素_尺寸 像素

    zv-1像素_尺寸 像素近期由于项目需要,开始接触视频像素格式,因此在这里做一个小结;像素格式描述了像素数据存储所用的格式,定义了像素在内存中的编码方式。RGB和YUV为两种经常使用的像素格式。RGB格式一般较为熟悉,RGB图像具有三个通道R、G、B,分别对应红、绿、蓝三个分量,由三个分量的值决定颜色;通常,会给RGB图像加一个通道alpha,即透明度,于是共有四个分量共同控制颜色。YUV格式(YCr…

    2022年9月25日
    0

发表回复

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

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