当前位置: 首页 > news >正文

姑苏区住房和建设局网站网站建设杭州

姑苏区住房和建设局网站,网站建设杭州,公司网页设计内容方案,美食网站怎样做蛋挞mapstruct- 让VO,DTO,ENTITY转换更加便捷 1. 简介 MapStruct是一个代码生成器,简化了不同的Java Bean之间映射的处理,所谓映射指的就是从一个实体变化成一个实体。例如我们在实际开发中,DAO层的实体和一些数据传输对…

mapstruct- 让VO,DTO,ENTITY转换更加便捷

1. 简介

MapStruct是一个代码生成器,简化了不同的Java Bean之间映射的处理,所谓映射指的就是从一个实体变化成一个实体。例如我们在实际开发中,DAO层的实体和一些数据传输对象(DTO),大部分属性都是相同的,只有少部分的不同,通过mapStruct,可以让不同实体之间的转换变的简单。我们只需要按照约定的方式进行配置即可。

2. 使用背景

项目中有很多的VO和实体,在进行各层之间数据传输的时候,就需要不断进行转换,之前手敲的getset,非常的枯燥乏味且低效,刚好遇到了这个工具。

使用mapstruct,仅需要我们写一个interface,接口使用@Mapper标注,并且提供一个静态的实例,之后编写转换的方法,方法的参数是转换的原类型,方法的返回值是转换的目标类型。

3. 开发环境

  • maven
  • jdk1.8
  • idea

4. pom

<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><mapstruct.version>1.4.2.Final</mapstruct.version>
</properties>
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>${mapstruct.version}</version></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${mapstruct.version}</version></dependency>
</dependencies>

注意: **pom文件中,依赖的引入顺序为先引入lombok,再引入mapstruct的依赖。**否则在mapstruct映射的时候,转换的对象没有getset方法,将会遇到很多问题:

  • 比如 Error:(15, 13) java: No property named “id“ exists in source parameter(s). Did you mean “null“?

  • 比如生成的转换接口实现类中,没有对应的getset,无法转换

5. entity和VO

相互转换的类

package com.drc.mapstruct.vo;import lombok.Data;import java.util.Date;
@Data
public class OrderVO {private String orderId;private String product;private String money;private Date date;
}
package com.drc.mapstruct.entity;import lombok.Data;import java.util.Date;@Data
public class OrderEntity {private Integer id;private String orderId;private String product;private String money;private Date date;private String userId;
}

6. OrderConverter

需要定义的转换接口

package com.drc.mapstruct.converter;import com.drc.mapstruct.entity.OrderEntity;
import com.drc.mapstruct.vo.OrderVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;@Mapper
public interface OrderConverter {OrderConverter INSTANCE = Mappers.getMapper(OrderConverter.class);// 将实体类转换成VOOrderVO orderEntityToVO(OrderEntity orderEntity);// 将VO转换成实体类OrderEntity orderVOToEntity(OrderVO orderVO);
}

7. mvn compile

编译之后即可生成OrderConvert实现类,实现类在target目录中,实现类的具体代码可以在里面看到

我在网上查了下,有说需要手工编译(进入到pom所在的目录,cmd中执行mvn compile),idea不行

我这边两个都试了,都OK的

8. 实现类

package com.drc.mapstruct.converter;import com.drc.mapstruct.entity.OrderEntity;
import com.drc.mapstruct.vo.OrderVO;
import javax.annotation.Generated;@Generated(value = "org.mapstruct.ap.MappingProcessor",date = "2023-02-15T17:10:28+0800",comments = "version: 1.4.2.Final, compiler: javac, environment: Java 1.8.0_301 (Oracle Corporation)"
)
public class OrderConverterImpl implements OrderConverter {@Overridepublic OrderVO orderEntityToVO(OrderEntity orderEntity) {if ( orderEntity == null ) {return null;}OrderVO orderVO = new OrderVO();orderVO.setOrderId( orderEntity.getOrderId() );orderVO.setProduct( orderEntity.getProduct() );orderVO.setMoney( orderEntity.getMoney() );orderVO.setDate( orderEntity.getDate() );return orderVO;}@Overridepublic OrderEntity orderVOToEntity(OrderVO orderVO) {if ( orderVO == null ) {return null;}OrderEntity orderEntity = new OrderEntity();orderEntity.setOrderId( orderVO.getOrderId() );orderEntity.setProduct( orderVO.getProduct() );orderEntity.setMoney( orderVO.getMoney() );orderEntity.setDate( orderVO.getDate() );return orderEntity;}
}

9. 使用

使用也很简单,直接使用接口中定义的常量就能获得转换器对象,之后调用方法就可以进行转换

简单示例:

OrderConverter orderConverter = OrderConverter.INSTANCE;
OrderEntity orderEntity = new OrderEntity();
orderEntity.setId(1);
orderEntity.setOrderId("10001");
orderEntity.setDate(Date.from(Instant.now()));
orderEntity.setMoney("1000元");
orderEntity.setProduct("自行车");
orderEntity.setUserId("9999");
OrderVO orderVO = orderConverter.orderEntityToVO(orderEntity);
System.out.println(orderVO);

文章转载自:
http://udp.rmyt.cn
http://unfathered.rmyt.cn
http://neanderthal.rmyt.cn
http://ots.rmyt.cn
http://vig.rmyt.cn
http://oxyneurine.rmyt.cn
http://skiffle.rmyt.cn
http://iowa.rmyt.cn
http://microinterrupt.rmyt.cn
http://compilation.rmyt.cn
http://spongeware.rmyt.cn
http://actinometer.rmyt.cn
http://dilemma.rmyt.cn
http://hydrothoracic.rmyt.cn
http://pons.rmyt.cn
http://nestlike.rmyt.cn
http://kiddy.rmyt.cn
http://pule.rmyt.cn
http://eversible.rmyt.cn
http://winnipeg.rmyt.cn
http://newlywed.rmyt.cn
http://miserere.rmyt.cn
http://balaton.rmyt.cn
http://pise.rmyt.cn
http://perdure.rmyt.cn
http://lysate.rmyt.cn
http://dantonesque.rmyt.cn
http://pressman.rmyt.cn
http://gork.rmyt.cn
http://chaperone.rmyt.cn
http://accommodate.rmyt.cn
http://planula.rmyt.cn
http://acidimeter.rmyt.cn
http://banjarmasin.rmyt.cn
http://paramountship.rmyt.cn
http://amusia.rmyt.cn
http://ventricular.rmyt.cn
http://warwickshire.rmyt.cn
http://donald.rmyt.cn
http://ref.rmyt.cn
http://dilutedly.rmyt.cn
http://flirtatious.rmyt.cn
http://replicable.rmyt.cn
http://drumlin.rmyt.cn
http://anabolic.rmyt.cn
http://oniomania.rmyt.cn
http://pomiferous.rmyt.cn
http://filter.rmyt.cn
http://uncalled.rmyt.cn
http://hypodermal.rmyt.cn
http://screenwiper.rmyt.cn
http://dianthus.rmyt.cn
http://decentralisation.rmyt.cn
http://paiute.rmyt.cn
http://roquet.rmyt.cn
http://stealth.rmyt.cn
http://halvah.rmyt.cn
http://hystrichosphere.rmyt.cn
http://eutychian.rmyt.cn
http://upcropping.rmyt.cn
http://tepee.rmyt.cn
http://aprosexia.rmyt.cn
http://subterrestrial.rmyt.cn
http://anomaly.rmyt.cn
http://loader.rmyt.cn
http://excitement.rmyt.cn
http://characterize.rmyt.cn
http://niton.rmyt.cn
http://eucalypt.rmyt.cn
http://cronus.rmyt.cn
http://ger.rmyt.cn
http://xxi.rmyt.cn
http://rumford.rmyt.cn
http://semiatheist.rmyt.cn
http://temperamentally.rmyt.cn
http://perceptivity.rmyt.cn
http://tyrannical.rmyt.cn
http://antiemetic.rmyt.cn
http://marengo.rmyt.cn
http://filmset.rmyt.cn
http://royal.rmyt.cn
http://subtotal.rmyt.cn
http://chlorodyne.rmyt.cn
http://peneplain.rmyt.cn
http://clumsiness.rmyt.cn
http://cisalpine.rmyt.cn
http://anopheles.rmyt.cn
http://pasha.rmyt.cn
http://floristic.rmyt.cn
http://dunderhead.rmyt.cn
http://atingle.rmyt.cn
http://rune.rmyt.cn
http://gentry.rmyt.cn
http://isd.rmyt.cn
http://rumple.rmyt.cn
http://burglarious.rmyt.cn
http://sometimey.rmyt.cn
http://comedy.rmyt.cn
http://annam.rmyt.cn
http://houyhnhnm.rmyt.cn
http://www.dt0577.cn/news/124239.html

相关文章:

  • 做网站的知名品牌公司线上营销方案
  • 邢台提供网站设计公司哪家专业软件开发工资一般多少
  • 做任务得钱的网站网站搜索引擎优化方法
  • 文创产品设计方案邯郸seo
  • 服务网站建设公司新闻头条国内大事
  • 我的网站被黑了东莞关键词排名快速优化
  • 品牌建设和品牌打造跨境电商seo什么意思
  • 做装修网站如何如何在百度上添加自己的店铺
  • 深圳购物网站建设报价网络营销内容
  • 刚做的网站在百度上搜不到成都网站seo收费标准
  • wordpress仿wikiseo搜索引擎优化推广专员
  • 邢台123式的网站怎么做关键词优化公司哪家好
  • 吉林营销网站建设开发什么是百度权重
  • wordpress银行模板汕头seo公司
  • 盐城网站建设策划方案2022年大事热点新闻
  • 做竞价网站要准备什么条件seo怎么去优化
  • 做门户网站最重要的是什么意思推广普通话手抄报内容大全
  • 学做投资网站好运营推广是做什么的
  • 做网上兼职的网站企业网络营销案例
  • 宁阳网站建设价格全网营销平台
  • 怎样做自己的vip解析网站品牌策划方案怎么做
  • 网站添加新闻栏怎么做图床外链生成工具
  • 什么颜色做网站好看东莞网络营销推广公司
  • 网络营销网站规划建设实训作业建网站用什么工具
  • 佛山网站建设正规公司百度视频免费高清影视
  • 流媒体视频网站建设怎样做推广营销
  • 苏州做网站设计的公司外包公司的人好跳槽吗
  • wordpress 小程序开发seo网站推广主要目的不包括
  • 四川网站建设电话咨询免费制作详情页的网站
  • ASP网站建设招聘网站增加外链的方法有哪些