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 deepcopy函数实现_python 多线程

    python deepcopy函数实现_python 多线程我有一个非常简单的python例程,它涉及循环遍历大约20000个纬度、经度坐标的列表,并计算每个点到参考点的距离。defcompute_nearest_points(lat,lon,nPoints=5):”””FindthenearestNpoints,giventheinputcoordinates.”””points=session.query(PointInd…

    2022年9月1日
    1
  • MFC中使用COleVariant获取CMFCPropertyGridProperty属性窗口某个属性值

    MFC中使用COleVariant获取CMFCPropertyGridProperty属性窗口某个属性值  获取MFC属性窗口CMFCPropertyGridProperty中某个item的值时,如果不小心写错了类型,就会导致获取的结果不正确,原因就是COleVariant其实继承自一个特殊的结构体tagVARIANT。COleVariant类声明···C++classCOleVariant:publictagVARIANT{//Constructorspubli…

    2022年7月18日
    14
  • WPF基础五:UI①布局元素WrapPanel[通俗易懂]

    WPF基础五:UI①布局元素WrapPanel[通俗易懂]目录WrapPanelWrapPanel类XAML范例:C#代码WrapPanel按从左到右的顺序位置定位子元素,在包含框的边缘处将内容切换到下一行。后续排序按照从上至下或从右至左的顺序进行,具体取决于Orientation属性的值。WrapPanel包含UIElement对象的集合,这些对象位于Children属性中。WrapPanel的所有子元素都接收ItemWidth与ItemHeight大小相乘的布局分区。WrapPanel类名称…

    2022年7月22日
    10
  • 令人比较失落的IT圈子-关于华为裁员

    令人比较失落的IT圈子-关于华为裁员早在几年前就有人说过程序员在35岁以后如果不做管理就很难混了,如今由于近日的华为事件被炒得沸沸扬扬,显然让这多年前人们的猜测变成了现实,我今年也正好到了这个该“退休”的年龄,所以就想趁机悔恨一番。首先,澄清的一点就是,我并无意诋毁这个IT行业,我只是希望大家可以更加清除的认清这个行业。       什么叫做管理,在程序员的思维里,做管理其实很简单,就是从写代码到不写代码,哪怕是写PPT,只要不写

    2022年7月25日
    32
  • nest expression &amp; Pyparsing[通俗易懂]

    nest expression &amp; Pyparsing

    2022年1月24日
    45
  • 整数补码加减法运算法则是什么_补码加减法中

    整数补码加减法运算法则是什么_补码加减法中整数的补码计算正数的补码计算:与原码相同负数的补码计算:①先求出负数的原码,如-4的原码为10000100②通过原码求出反码,负数的反码就是:除符号位以外,其他位全部取反,如-4的反码为11111011③负数的补码等于负数的反码末位+1,如-4的补码为11111100特例:约定-128的补码为10000000注:若已知补码求原码:正数也是它本身,负数的求法同上,即对补码除符号位外取反,末位加1,就得到原码整数补码加减运算加法[A+B]补=[A]补+[B]补减法[

    2022年9月22日
    1

发表回复

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

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