0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

MapStruct是用来做什么的

Android编程精选 来源:CSDN技术社区 作者:飞飞不 会飞 2022-06-15 17:02 次阅读
首先来了解一下DTO,DTO简单的理解就是做数据传输对象的,类似于VO,但是VO用于传输到前端。

1.MapStruct是用来做什么的?

现在有这么个场景,从数据库查询出来了一个user对象(包含id,用户名,密码,手机号,邮箱,角色这些字段)和一个对应的角色对象role(包含id,角色名,角色描述这些字段),现在在controller需要用到user对象的id,用户名,和角色对象的角色名三个属性。

一种方式是直接把两个对象传递到controller层,但是这样会多出很多没用的属性。更通用的方式是需要用到的属性封装成一个类(DTO),通过传输这个类的实例来完成数据传输。

User.java

@AllArgsConstructor
@Data
publicclassUser{
privateLongid;
privateStringusername;
privateStringpassword;
privateStringphoneNum;
privateStringemail;
privateRolerole;
}

Role.java

@AllArgsConstructor
@Data
publicclassRole{
privateLongid;
privateStringroleName;
privateStringdescription;
}

UserRoleDto.java,这个类就是封装的类

@Data
publicclassUserRoleDto{
/**
*用户id
*/
privateLonguserId;
/**
*用户名
*/
privateStringname;
/**
*角色名
*/
privateStringroleName;
}

测试类,模拟将user对象转换成UserRoleDto对象

publicclassMainTest{
Useruser=null;

/**
*模拟从数据库中查出user对象
*/
@Before
publicvoidbefore(){
Rolerole=newRole(2L,"administrator","超级管理员");
user=newUser(1L,"zhangsan","12345","17677778888","123@qq.com",role);
}

/**
*模拟把user对象转换成UserRoleDto对象
*/
@Test
publicvoidtest1(){
UserRoleDtouserRoleDto=newUserRoleDto();
userRoleDto.setUserId(user.getId());
userRoleDto.setName(user.getUsername());
userRoleDto.setRoleName(user.getRole().getRoleName());
System.out.println(userRoleDto);
}
}

从上面代码可以看出,通过getter、setter的方式把一个对象属性值复制到另一个对象中去还是很麻烦的,尤其是当属性过多的时候。而MapStruct就是用于解决这种问题的。

2.使用MapStruct解决上述问题

这里我们沿用User.java、Role.java、UserRoleDto.java。

新建一个UserRoleMapper.java,这个来用来定义User.java、Role.java和UserRoleDto.java之间属性对应规则:

UserRoleMapper.java

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定义这是一个MapStruct对象属性转换接口,在这个类里面规定转换规则
*在项目构建时,会自动生成改接口的实现类,这个实现类将实现对象属性值复制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*获取该类自动生成的实现类的实例
*接口中的属性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*这个方法就是用于实现对象属性复制的方法
*
*@Mapping用来定义属性复制规则source指定源对象属性target指定目标对象属性
*
*@paramuser这个参数就是源对象,也就是需要被复制的对象
*@return返回的是目标对象,就是最终的结果对象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
UserRoleDtotoUserRoleDto(Useruser);

}

在测试类中测试:

通过上面的例子可以看出,使用MapStruct方便许多。

3.添加默认方法

添加默认方法是为了这个类(接口)不只是为了做数据转换用的,也可以做一些其他的事。

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定义这是一个MapStruct对象属性转换接口,在这个类里面规定转换规则
*在项目构建时,会自动生成改接口的实现类,这个实现类将实现对象属性值复制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*获取该类自动生成的实现类的实例
*接口中的属性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*这个方法就是用于实现对象属性复制的方法
*
*@Mapping用来定义属性复制规则source指定源对象属性target指定目标对象属性
*
*@paramuser这个参数就是源对象,也就是需要被复制的对象
*@return返回的是目标对象,就是最终的结果对象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
UserRoleDtotoUserRoleDto(Useruser);

/**
*提供默认方法,方法自己定义,这个方法是我随便写的,不是要按照这个格式来的
*@return
*/
defaultUserRoleDtodefaultConvert(){
UserRoleDtouserRoleDto=newUserRoleDto();
userRoleDto.setUserId(0L);
userRoleDto.setName("None");
userRoleDto.setRoleName("None");
returnuserRoleDto;
}

}

测试代码:

@Test
publicvoidtest3(){
UserRoleMapperuserRoleMapperInstances=UserRoleMapper.INSTANCES;
UserRoleDtouserRoleDto=userRoleMapperInstances.defaultConvert();
System.out.println(userRoleDto);
}

4. 可以使用abstract class来代替接口

mapper可以用接口来实现,也可以完全由抽象来完全代替

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定义这是一个MapStruct对象属性转换接口,在这个类里面规定转换规则
*在项目构建时,会自动生成改接口的实现类,这个实现类将实现对象属性值复制
*/
@Mapper
publicabstractclassUserRoleMapper{

/**
*获取该类自动生成的实现类的实例
*接口中的属性都是publicstaticfinal的方法都是publicabstract的
*/
publicstaticfinalUserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*这个方法就是用于实现对象属性复制的方法
*
*@Mapping用来定义属性复制规则source指定源对象属性target指定目标对象属性
*
*@paramuser这个参数就是源对象,也就是需要被复制的对象
*@return返回的是目标对象,就是最终的结果对象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
publicabstractUserRoleDtotoUserRoleDto(Useruser);

/**
*提供默认方法,方法自己定义,这个方法是我随便写的,不是要按照这个格式来的
*@return
*/
UserRoleDtodefaultConvert(){
UserRoleDtouserRoleDto=newUserRoleDto();
userRoleDto.setUserId(0L);
userRoleDto.setName("None");
userRoleDto.setRoleName("None");
returnuserRoleDto;
}

}

5.可以使用多个参数

可以绑定多个对象的属性值到目标对象中:

packagecom.mapstruct.demo;

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定义这是一个MapStruct对象属性转换接口,在这个类里面规定转换规则
*在项目构建时,会自动生成改接口的实现类,这个实现类将实现对象属性值复制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*获取该类自动生成的实现类的实例
*接口中的属性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*这个方法就是用于实现对象属性复制的方法
*
*@Mapping用来定义属性复制规则source指定源对象属性target指定目标对象属性
*
*@paramuser这个参数就是源对象,也就是需要被复制的对象
*@return返回的是目标对象,就是最终的结果对象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
UserRoleDtotoUserRoleDto(Useruser);

/**
*多个参数中的值绑定
*@paramuser源1
*@paramrole源2
*@return从源1、2中提取出的结果
*/
@Mappings({
@Mapping(source="user.id",target="userId"),//把user中的id绑定到目标对象的userId属性中
@Mapping(source="user.username",target="name"),//把user中的username绑定到目标对象的name属性中
@Mapping(source="role.roleName",target="roleName")//把role对象的roleName属性值绑定到目标对象的roleName中
})
UserRoleDtotoUserRoleDto(Useruser,Rolerole);

对比两个方法~

5.直接使用参数作为属性值

packagecom.mapstruct.demo;

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定义这是一个MapStruct对象属性转换接口,在这个类里面规定转换规则
*在项目构建时,会自动生成改接口的实现类,这个实现类将实现对象属性值复制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*获取该类自动生成的实现类的实例
*接口中的属性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*直接使用参数作为值
*@paramuser
*@parammyRoleName
*@return
*/
@Mappings({
@Mapping(source="user.id",target="userId"),//把user中的id绑定到目标对象的userId属性中
@Mapping(source="user.username",target="name"),//把user中的username绑定到目标对象的name属性中
@Mapping(source="myRoleName",target="roleName")//把role对象的roleName属性值绑定到目标对象的roleName中
})
UserRoleDtouseParameter(Useruser,StringmyRoleName);

}

测试类:

publicclassTest1{
Rolerole=null;
Useruser=null;

@Before
publicvoidbefore(){
role=newRole(2L,"administrator","超级管理员");
user=newUser(1L,"zhangsan","12345","17677778888","123@qq.com",role);
}
@Test
publicvoidtest1(){
UserRoleMapperinstances=UserRoleMapper.INSTANCES;
UserRoleDtouserRoleDto=instances.useParameter(user,"myUserRole");
System.out.println(userRoleDto);
}
}

6.更新对象属性

在之前的例子中UserRoleDto useParameter(User user, String myRoleName);都是通过类似上面的方法来生成一个对象。而MapStruct提供了另外一种方式来更新一个对象中的属性。@MappingTarget

publicinterfaceUserRoleMapper1{

UserRoleMapper1INSTANCES=Mappers.getMapper(UserRoleMapper1.class);

@Mappings({
@Mapping(source="userId",target="id"),
@Mapping(source="name",target="username"),
@Mapping(source="roleName",target="role.roleName")
})
voidupdateDto(UserRoleDtouserRoleDto,@MappingTargetUseruser);


@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
voidupdate(Useruser,@MappingTargetUserRoleDtouserRoleDto);

}

通过@MappingTarget来指定目标类是谁(谁的属性需要被更新)。@Mapping还是用来定义属性对应规则。

以此为例说明:

@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
voidupdate(Useruser,@MappingTargetUserRoleDtouserRoleDto);

@MappingTarget标注的类UserRoleDto 为目标类,user类为源类,调用此方法,会把源类中的属性更新到目标类中。更新规则还是由@Mapping指定。

7.没有getter/setter也能赋值

对于没有getter/setter的属性也能实现赋值操作

publicclassCustomer{

privateLongid;
privateStringname;

//gettersandsetteromittedforbrevity
}

publicclassCustomerDto{

publicLongid;
publicStringcustomerName;
}

@Mapper
publicinterfaceCustomerMapper{

CustomerMapperINSTANCE=Mappers.getMapper(CustomerMapper.class);

@Mapping(source="customerName",target="name")
CustomertoCustomer(CustomerDtocustomerDto);

@InheritInverseConfiguration
CustomerDtofromCustomer(Customercustomer);
}

@Mapping(source = “customerName”, target = “name”)不是用来指定属性映射的,如果两个对象的属性名相同是可以省略@Mapping的。

MapStruct生成的实现类:

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1521+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
publicclassCustomerMapperImplimplementsCustomerMapper{

@Override
publicCustomertoCustomer(CustomerDtocustomerDto){
if(customerDto==null){
returnnull;
}

Customercustomer=newCustomer();

customer.setName(customerDto.customerName);
customer.setId(customerDto.id);

returncustomer;
}

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();

customerDto.customerName=customer.getName();
customerDto.id=customer.getId();

returncustomerDto;
}
}

@InheritInverseConfiguration在这里的作用就是实现customerDto.customerName = customer.getName();功能的。如果没有这个注解,toCustomerDto这个方法则不会有customerName 和name两个属性的对应关系的。

8.使用Spring依赖注入

@Data
@NoArgsConstructor
@AllArgsConstructor
publicclassCustomer{
privateLongid;
privateStringname;
}

@Data
publicclassCustomerDto{
privateLongid;
privateStringcustomerName;
}

//这里主要是这个componentModel属性,它的值就是当前要使用的依赖注入的环境
@Mapper(componentModel="spring")
publicinterfaceCustomerMapper{

@Mapping(source="name",target="customerName")
CustomerDtotoCustomerDto(Customercustomer);
}

@Mapper(componentModel = “spring”),表示把当前Mapper类纳入spring容器。可以在其它类中直接注入了:

@SpringBootApplication
@RestController
publicclassDemoMapstructApplication{

//注入Mapper
@Autowired
privateCustomerMappermapper;

publicstaticvoidmain(String[]args){
SpringApplication.run(DemoMapstructApplication.class,args);
}

@GetMapping("/test")
publicStringtest(){
Customercustomer=newCustomer(1L,"zhangsan");
CustomerDtocustomerDto=mapper.toCustomerDto(customer);
returncustomerDto.toString();
}

}

看一下由mapstruct自动生成的类文件,会发现标记了@Component注解。

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1517+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
@Component
publicclassCustomerMapperImplimplementsCustomerMapper{

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();

customerDto.setCustomerName(customer.getName());
customerDto.setId(customer.getId());

returncustomerDto;
}
}

9.自定义类型转换

有时候,在对象转换的时候可能会出现这样一个问题,就是源对象中的类型是Boolean类型,而目标对象类型是String类型,这种情况可以通过@Mapper的uses属性来实现:

@Data
@NoArgsConstructor
@AllArgsConstructor
publicclassCustomer{
privateLongid;
privateStringname;
privateBooleanisDisable;
}

@Data
publicclassCustomerDto{
privateLongid;
privateStringcustomerName;
privateStringdisable;
}

定义转换规则的类:

publicclassBooleanStrFormat{
publicStringtoStr(BooleanisDisable){
if(isDisable){
return"Y";
}else{
return"N";
}
}
publicBooleantoBoolean(Stringstr){
if(str.equals("Y")){
returntrue;
}else{
returnfalse;
}
}
}

定义Mapper,@Mapper( uses = { BooleanStrFormat.class}),注意,这里的users属性用于引用之前定义的转换规则的类:

@Mapper(uses={BooleanStrFormat.class})
publicinterfaceCustomerMapper{

CustomerMapperINSTANCES=Mappers.getMapper(CustomerMapper.class);

@Mappings({
@Mapping(source="name",target="customerName"),
@Mapping(source="isDisable",target="disable")
})
CustomerDtotoCustomerDto(Customercustomer);
}

这样子,Customer类中的isDisable属性的true就会转变成CustomerDto中的disable属性的yes。

MapStruct自动生成的类中的代码:

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1618+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
publicclassCustomerMapperImplimplementsCustomerMapper{

//引用uses中指定的类
privatefinalBooleanStrFormatbooleanStrFormat=newBooleanStrFormat();

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();
//转换方式的使用
customerDto.setDisable(booleanStrFormat.toStr(customer.getIsDisable()));
customerDto.setCustomerName(customer.getName());
customerDto.setId(customer.getId());

returncustomerDto;
}
}

要注意的是,如果使用了例如像spring这样的环境,Mapper引入uses类实例的方式将是自动注入,那么这个类也应该纳入Spring容器:

CustomerMapper.java指定使用spring

@Mapper(componentModel="spring",uses={BooleanStrFormat.class})
publicinterfaceCustomerMapper{

CustomerMapperINSTANCES=Mappers.getMapper(CustomerMapper.class);

@Mappings({
@Mapping(source="name",target="customerName"),
@Mapping(source="isDisable",target="disable")
})
CustomerDtotoCustomerDto(Customercustomer);
}

转换类要加入Spring容器:

@Component
publicclassBooleanStrFormat{
publicStringtoStr(BooleanisDisable){
if(isDisable){
return"Y";
}else{
return"N";
}
}
publicBooleantoBoolean(Stringstr){
if(str.equals("Y")){
returntrue;
}else{
returnfalse;
}
}
}

MapStruct自动生成的类:

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1635+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
@Component
publicclassCustomerMapperImplimplementsCustomerMapper{

//使用自动注入的方式引入
@Autowired
privateBooleanStrFormatbooleanStrFormat;

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();

customerDto.setDisable(booleanStrFormat.toStr(customer.getIsDisable()));
customerDto.setCustomerName(customer.getName());
customerDto.setId(customer.getId());

returncustomerDto;
}
}

原文标题:实体映射最强工具类:MapStruct 真香!

文章出处:【微信公众号:Android编程精选】欢迎添加关注!文章转载请注明出处。

审核编辑:汤梓红
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 数据传输
    +关注

    关注

    9

    文章

    1525

    浏览量

    63560
  • JAVA
    +关注

    关注

    19

    文章

    2904

    浏览量

    102989
  • User
    +关注

    关注

    1

    文章

    27

    浏览量

    10715

原文标题:实体映射最强工具类:MapStruct 真香!

文章出处:【微信号:AndroidPush,微信公众号:Android编程精选】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    请问培训生是做什么的

    技术员培训生工程师培训生这些职位是做什么的·····以后怎么发展······
    发表于 02-18 01:24

    请问IIO软件是做什么的

    IIO软件是做什么的?刚接触AD9361,SD卡中的系统带的软件,可我看了半天,不知道具体该怎么设置波形,还有设置好的波形是输出波形,还是输入波形啊?
    发表于 08-03 06:58

    请问sot-223封装的第四管脚是用来做什么的

    sot-223的第四管脚是用来做什么的?上面标有TAB IS VOUT或是TAB IS GND是什么意思?
    发表于 09-25 11:35

    bootloader程序是用来做什么的?与startup程序有区别么?

    想请教一个问题,MCU中的bootloader程序是做什么的?与在KEIL中写的startup程序有区别么?另外ISP升级又与bootloader有什么关系?求大侠赐教
    发表于 01-08 10:55

    Native是用来做什么的

    PDK中的mos管类型中的Native是用来做什么的
    发表于 01-18 06:46

    存储器是用来做什么的?系统总线是什么

    存储器是用来做什么的?系统总线是什么?有何功能?CPU中央处理器是什么?有何功能?
    发表于 01-21 07:24

    RK3399Pro开发板是用来做什么的

    RK3399Pro开发板是用来做什么的?RK3399Pro开发板报考哪些组成部分呢?有何应用?
    发表于 02-15 07:55

    sot-223的第四管脚是用来做什么的

    sot-223的第四管脚是用来做什么的? 上面标有TAB IS VOUT或是TAB IS GND是什么意思?
    发表于 11-22 07:25

    台积电是做什么的_台积电跟富士康谁厉害

    本文主要介绍了台积电是做什么的、台积电发展历史和台积电股份构成。其次介绍了富士康的相关概念,最后说明了目前台积电富士康联发科已经成三大巨头。
    发表于 01-08 10:16 65.4w次阅读

    数据库工程师是做什么的

    每当我给别人介绍我的职位是数据库工程师的时候,别人都会用疑惑的眼睛看着我。总会抛出一个问题:数据库工程师是做什么的
    的头像 发表于 08-27 09:00 8978次阅读

    程序员到底是做什么的

    很多人问程序员是是做什么的?或者问IT是做什么的?对于非IT行业的人很难有时间慢慢解释清楚,下面我结合自己的理解谈一谈吧。
    的头像 发表于 02-12 16:17 9033次阅读

    Mirru 应用是用来做什么的 为什么要使用手部追踪

    Mirru 应用是用来做什么的? Mirru 是一款正在开发中的免费开源 Android 应用,可以通过追踪手部 (Hand Tracking) 来控制机械手。通过这款应用,用户可以将其真手的抓握
    的头像 发表于 08-12 15:04 1723次阅读

    铌酸锂是用来做什么的

    欢迎大家来到小K的实验室。本期小K的实验室迎来了一位叫铌酸锂的客人。 你听说过铌酸锂吗? 你知道铌酸锂是用来做什么的吗? 你知道铌酸锂能让你成为人生赢家吗? 且听小K给您好好介绍这位“客人
    的头像 发表于 03-19 10:40 5911次阅读

    ip地址是做什么的

    如果你现在正在看我的这篇文章,那说明你已经连接上了互联网。说到互联网,你一定听说 ip 地址这个概念,你知道 ip 地址是做什么的吗?与之而来的还有公网 ip ,私网 ip ,你知道有什么区别吗?
    的头像 发表于 09-16 10:03 1858次阅读

    总线是用来做什么的

    昨天一位网友表示自己搞不懂总线是做什么的,网上的标准答案也晦涩难懂,让我用比较通俗的话来解释一下,下面我用例子说明说明。 我们知道,汽车上有很多的电子设备,它们就是通过总线连接的。总线,是一条公共
    的头像 发表于 04-05 14:05 610次阅读