MapStruct

MapStruct是什么

MapStruct是一个代码生成器,用于简化Java实体类型之间转换,使用时只需定义Mapper映射接口,会自动为我们生成转换代码。本质上MapStruct是通过普通方法调用进行字段映射,快速、类型安全、简单、易于理解。

主页: https://mapstruct.org/

GitHub: https://github.com/mapstruct/mapstruct/

Demo: https://github.com/mapstruct/mapstruct-examples

MapStruct使用方式

1.1 Maven坐标

<properties><org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
</properties><dependencies><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>${org.mapstruct.version}</version></dependency>
</dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><annotationProcessorPaths><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${org.mapstruct.version}</version></path></annotationProcessorPaths></configuration></plugin></plugins>
</build>
1.2 定义基础转换类
package com.giser.mp.entmp;import java.util.List;/**
*
* 基础转换类,提供几个基本方法,直接继承使用
* 更多的用法需自行实现
* @param <DTO> 目标对象,一般为DTO对象
* @param <ENTITY> 源对象,一般为需要转换的对象
* @param <VO> 目标对象,一般为VO对象
*/
public interface BaseMapStructMapper<DTO, ENTITY, VO> {/*** 将源对象转换为DTO对象* @param e* @return D*/DTO entityToDTO(ENTITY e);/*** 将源对象集合转换为DTO对象集合* @param es* @return List<D>*/List<DTO> entitiesToDTOList(List<ENTITY> es);/*** 将源对象转换为VO对象* @param e* @return V*/VO entityToVO(ENTITY e);/*** 将DTO对象转换为VO对象* @param d* @return V*/VO dtoToVO(DTO d);/*** 将源对象集合转换为VO对象集合* @param es* @return List<D>*/List<VO> entitiesToVOList(List<ENTITY> es);/*** 将目标对象转换为源对象* @param d* @return E*/ENTITY dtoToEntity(DTO d);/*** 将目标对象集合转换为源对象集合* @param ds* @return List<E>*/List<ENTITY> dtosToEntityList(List<DTO> ds);}
13 使用时,继承基础转换类,并标注@Mapper注解(org.mapstruct.Mapper)即可
@Mapper(componentModel = "spring")
public interface EarlyEntityMapper extends BaseMapStructMapper<EarlyDto, EarlyEntity, EarlyVo> {
}

1.4 测试

/*** @see https://mapstruct.org/* @see https://github.com/mapstruct/mapstruct/* @see https://github.com/mapstruct/mapstruct-examples*/
@SpringBootTest(classes = JavaMpApp.class)
@Slf4j
public class MapStructTest {@Autowiredprivate EarlyEntityMapper earlyEntityMapper;@Testpublic void testDto2Ent(){EarlyDto earlyDto = new EarlyDto(1L,"xiangjiao dto",22,null);EarlyEntity entity = earlyEntityMapper.dtoToEntity(earlyDto);System.out.println(entity);}
}

5.编译后的文件

package BOOT-INF.classes.com.giser.mp.entmp;import com.giser.mp.entity.EarlyDto;
import com.giser.mp.entity.EarlyEntity;
import com.giser.mp.entity.EarlyVo;
import com.giser.mp.entmp.EarlyEntityMapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;@Component
public class EarlyEntityMapperImpl implements EarlyEntityMapper {public EarlyDto entityToDTO(EarlyEntity e) {if (e == null)return null; EarlyDto earlyDto = new EarlyDto();earlyDto.setId(e.getId());earlyDto.setName(e.getName());earlyDto.setAge(e.getAge());earlyDto.setEmail(e.getEmail());return earlyDto;}public List<EarlyDto> entitiesToDTOList(List<EarlyEntity> es) {if (es == null)return null; List<EarlyDto> list = new ArrayList<>(es.size());for (EarlyEntity earlyEntity : es)list.add(entityToDTO(earlyEntity)); return list;}public EarlyVo entityToVO(EarlyEntity e) {if (e == null)return null; EarlyVo earlyVo = new EarlyVo();earlyVo.setId(e.getId());earlyVo.setName(e.getName());earlyVo.setAge(e.getAge());earlyVo.setEmail(e.getEmail());return earlyVo;}public EarlyVo dtoToVO(EarlyDto d) {if (d == null)return null; EarlyVo earlyVo = new EarlyVo();earlyVo.setId(d.getId());earlyVo.setName(d.getName());earlyVo.setAge(d.getAge());earlyVo.setEmail(d.getEmail());return earlyVo;}public List<EarlyVo> entitiesToVOList(List<EarlyEntity> es) {if (es == null)return null; List<EarlyVo> list = new ArrayList<>(es.size());for (EarlyEntity earlyEntity : es)list.add(entityToVO(earlyEntity)); return list;}public EarlyEntity dtoToEntity(EarlyDto d) {if (d == null)return null; EarlyEntity earlyEntity = new EarlyEntity();earlyEntity.setId(d.getId());earlyEntity.setName(d.getName());earlyEntity.setAge(d.getAge());earlyEntity.setEmail(d.getEmail());return earlyEntity;}public List<EarlyEntity> dtosToEntityList(List<EarlyDto> ds) {if (ds == null)return null; List<EarlyEntity> list = new ArrayList<>(ds.size());for (EarlyDto earlyDto : ds)list.add(dtoToEntity(earlyDto)); return list;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/185978.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AIGC: 关于ChatGPT中的API调用模型

ChatGPT的API模型 可供GPT的API调用的模型 模型描述GPT4免费的GPT模型&#xff0c;基于3.5改进&#xff0c;可以理解自然语言并生成代码GPT3.5免费的GPT模型&#xff0c;基于3.0改进&#xff0c;可以理解自然语言并生成代码DALLE可以在给定自然语言提示的情况下生成和编辑图像…

Kubernetes(K8s)_15_CNI

Kubernetes&#xff08;K8s&#xff09;_15_CNI CNI网络模型UnderlayMAC VLANIP VLANDirect Route OverlayVXLAN CNI插件FlannelCalico CNI配置内置实现 CNI CNI(Container Network Interface): 实现容器网络连接的规范 Kubernetes将网络通信可分为: Pod内容器、Pod、Pod与Se…

SOT23-3封装的设计与应用:220V转5V芯片电路

SOT23-3封装的设计与应用&#xff1a;220V转5V电路 AH8100介绍了一种基于SOT23-3封装的220V转5V电路l32*4761*OOO1设计方案&#xff0c;该方案具有简单、高效、稳定的特点&#xff0c;适用于各种电子设备。 一、引言 随着科技的发展&#xff0c;电子设备越来越多地应用于我们…

滴滴2023.11.27P0级故障技术复盘回顾(k8s的的错?)

本文从滴滴官方恢复及技术公众号带大家从技术角度复盘这次事故 目录 1. 背景 2. 滴滴官方消息 3. 问题分析及定位 4.网传的k8s及解析 5.k8s引发的思考&#xff1a;举一反三&#xff0c;怎么避免再次出现 6.近段时间其他平台崩溃回顾 1. 背景 11 月 27 晚约 10 点&#xf…

TCP解帧解码、并发送有效数据到FPGA

TCP解帧解码、并发送有效数据到FPGA 工程的功能&#xff1a;使用TCP协议接收到网络调试助手发来的指令&#xff0c;将指令进行解帧&#xff0c;提取出帧头、有限数据、帧尾&#xff1b;再将有效数据发送到FPGA端的BRAM上&#xff0c;实现信息传递。 参考&#xff1a;正点原子启…

11-鸿蒙4.0学习之页面之间的参数传递

11-鸿蒙4.0学习之页面之间的参数传递 方法一 params // 传参页面 import router from ohos.routerEntry Component struct LifeCycle1 {State message: string Hello WorldState isShow: boolean falsebuild() {Row() {Column({ space: 20 }) {Text(this.message).fontSiz…

开发中针对接口返回的数据要不要做兼容的示例详解

开发中&#xff0c;针对接口返回的数据&#xff0c;要不要做兼容&#xff0c;可以查看以下示例&#xff1b; 基本数据类型为string、number、boolean都有属性&#xff0c;但是属性值为 undefined&#xff1b; 基本数据类型为null、undefined没有属性&#xff0c;会 报错 arr、…

Apache Hive3.1.3 遇到DATE_FORMAT转换2021年12月格式的问题

比如&#xff1a;需要将时间2021-12-28 00:00:00转换成2021-12的格式&#xff0c;用date_format会将2021-12转换成2022-12的问题。 解决方法&#xff1a; 方式一&#xff1a;大写的‘Y’换成‘y’ 方式二&#xff1a;字符串截取&#xff0c;substr 本博主推荐方式一&#xf…

Linux地址空间随机化

ASLR(Address Space Layout Randomization)在2005年被引入到Linux的内核 kernel 2.6.12 中&#xff0c;早在2004年就以补丁的形式引入。内存地址的随机化&#xff0c;意味着同一应用多次执行所使用内存空间完全不同&#xff0c;也意味着简单的缓冲区溢出攻击无法达到目的。 1.…

04_使用API_日期和时间

JDK 8 之前传统的日期、时间 Date 类 代表的是日期和时间 import java.util.Date;public class Test {public static void main(String[] args) {// 1. 创建一个Data对象&#xff0c;代表系统当前时间信息的Date d new Date();System.out.println(d); // 输出的是日期与当…

Oracle(2-8)Configuring the Database Archiving Mode

文章目录 一、基础知识1、Redo Log History2、NOARCHIVELOG Mode 非归档模式3、ARCHIVELOG Mode 归档模式4、Changing the Archiving Mode 更改归档模式![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/d6a09f9a6de24de7bbcdad90b8d6b9ca.png)5、Auto and Manual Ar…

MybtisPlus快速开发(从controller到mapper)

创建新项目 写好配置文件 server:port: 8905#配置MP控制台打印日志 mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplspring:datasource:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:m…

接手项目要做的事项

总结&#xff1a;在接手别人的项目时&#xff0c;至少应该自己整理并绘画四个图 1、产品脑图&#xff1a;帮助你理解产品的功能&#xff1b; 2、UML时序图&#xff1a;帮助你源代码的核心技术实现&#xff1b; 3、整体业务泳道图&#xff1a;帮助你从整体上熟悉业务的流程&a…

联想M7400W激光打印机加粉清零方法

基本参数 产品定位&#xff1a;多功能商用一体机 产品类型&#xff1a;黑白激光多功能一体机 涵盖功能&#xff1a;打印、复印、扫描 最大处理幅面&#xff1a;A4 耗材类型&#xff1a;鼓粉分离 耗材容量&#xff1a;硒鼓LD2451 12000页&#xff0c;墨粉LT2451 1500页、L…

Android 11.0 修改Android系统的通知自动成组的数量

场景: Android 系统对显示在通知列表中的同一个应用的通知进行分组管理,即相同的packageName中,当通知数量达到系统默认指定的数量时,会自动成一组. Android 11.0 中系统默认的自动成组数如下所示: 核心路径 : frameworks/base/core/res/res/values/config.xml<!-- 来自同…

LaTex语法实现多种矩阵

矩阵 0 1 1 0 \begin{matrix} 0 & 1 \\ 1 & 0 \end{matrix} 01​10​ \begin{matrix}0 & 1 \\1 & 0 \end{matrix}小括号矩阵 ( 0 1 1 0 ) \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} (01​10​) \begin{pmatrix}0 & 1 \\1 & 0 \end{pm…

腾讯面试笔试题2023.11.30

给定一个由整数组成的非空数组所表示的非负整数如[1,2,3]&#xff0c;在该数的基础上加一。 最高位数字存放在数组的首位&#xff0c; 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外&#xff0c;这个整数不会以零开头。 &#xff08;要求只能操作数组&#xff0c;不…

webpack的plugin和loader的区别

Webpack 的 Plugin 和 Loader 是用来处理模块和资源的两个不同的概念。 Loader 是Webpack 的模块转换器&#xff0c;用于将某种特定格式的内容转换为Webpack 可以处理的模块。它可以在构建流程中将各种类型的文件&#xff08;如 CSS、图片、ES6、TypeScript 等&#xff09;转换…

每日一练2023.11.30——谁先倒【PTA】

题目链接&#xff1a;谁先倒 题目要求&#xff1a; 划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为&#xff1a;每人口中喊出一个数字&#xff0c;同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和&#xff0c;谁就输了&#xff0…

PyCharm安装教程(详细步骤)

一、软件简介 PyCharm是一款Python IDE&#xff0c;其带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具&#xff0c;比如&#xff0c; 调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、单元测试、版本控制等等。此外&#xff0c;该IDE提供了一些高…