实体类转换Mapstruct

常见类型转换方法

当我们在代码中遇到实体类之间相关转换的时候,最常用的应该就是BeanUtils.copyProperties();方法了吧,但是这个方法只能转换同名,同类型的属性,如果名称不同,属性不同,则不会转换成功,例如,现在有如下两个实体类:

@Data
@AllArgsConstructor
class SourceEntity{
    private Integer id;
    
    private Integer parentId;

    private String name;

    private LocalDateTime createDate;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class TargetEntity{
    private Integer id;

    private String parentId;

    private String realName;

    private Date updateDate;
}

使用BeanUtils进行实体类转换

public class TestService {
    public static void main(String[] args) {
        SourceEntity sourceEntity = new SourceEntity(1, 0, "ralph", LocalDateTime.now());
        TargetEntity targetEntity = new TargetEntity();
        BeanUtils.copyProperties(sourceEntity,targetEntity);
        System.out.println(targetEntity);
    }
}

转换结果:

TargetEntity(id=1, parentId=null, realName=null, updateDate=null)

这个结果应该都在大家意料之中了,那如果想要让两个实体类属性全部转换,是不是需要一个一个进行set

public static void main(String[] args) {
    SourceEntity sourceEntity = new SourceEntity(1, 0, "ralph", LocalDateTime.now());
    TargetEntity targetEntity = new TargetEntity();
    BeanUtils.copyProperties(sourceEntity,targetEntity);

    targetEntity.setParentId(sourceEntity.getParentId().toString());
    targetEntity.setRealName(sourceEntity.getName());
  targetEntity.setUpdateDate(DateUtil.parse(sourceEntity.getCreateDate().toString()));

    System.out.println(targetEntity);
}

结果:

TargetEntity(id=1, parentId=0, realName=ralph, updateDate=2023-02-11 19:39:10)

显而易见,能过这种方式达到了我们想要的效果,但是这样的方式过于繁琐,而且如果想要复用,必须得抽一个公共方法出来,如果需要转换的类比较多那每一个类都需要抽一个方法。

接下来就该由本文的主角-Mapstruct全场

使用Mapstruct进行类型转换

1、引入相关依赖

<!--mapstruct核心-->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.3.1.Final</version>
</dependency>
<!--mapstruct编译-->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.3.1.Final</version>
</dependency>

2、编写转换类

package com.ralph.business.article.utils;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * 实体类转换接口,即使字段名称,类型不一致,也可实现转换
 */
@Mapper(componentModel = "spring")
public interface EntityConvert {

    /**
     * 获取该类自动生成的实现类的实例
     * 接口中的属性都是 public static final 的 方法都是public abstract的
     */
    EntityConvert INSTANCES = Mappers.getMapper(EntityConvert.class);

    @Mappings({
            @Mapping(target = "parentId", expression = "java(source.getParentId().toString())"),
            @Mapping(source = "name", target = "realName"),
            @Mapping(source = "createDate", target = "updateDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
    })
    TargetEntity toTargetEntity(SourceEntity source);
}

3、调用方法

public class TestService {
    public static void main(String[] args) {
        SourceEntity sourceEntity = new SourceEntity(1, 0, "ralph", LocalDateTime.now());
        TargetEntity targetEntity = EntityConvert.INSTANCES.toTargetEntity(sourceEntity);

        System.out.println(targetEntity);
    }
}

4、运行结果

TargetEntity(id=1, parentId=0, realName=ralph, updateDate=Sun Feb 12 03:48:10 CST 2023)

可以看到,同样完成了类型转换,而且代码更加简洁,如果有多个实体类需要转换,只需要在接口中增加对应的方法即可,通过@Mapping注解标注名称或类型不同的属性,相同的可以不加,会自动转换

高级用法

逆转换

当写好了一个转换方法,想要将转换关系反过来时,只需要加一个注解即可实现@InheritInverseConfiguration

@Mappings({
    @Mapping(target = "parentId", expression = "java(source.getParentId().toString())"),
    @Mapping(source = "name", target = "realName"),
    @Mapping(source = "createDate", target = "updateDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
TargetEntity toTargetEntity(SourceEntity source);

@InheritInverseConfiguration
SourceEntity toTargetEntity(TargetEntity source);

这样SourceEntity toTargetEntity(TargetEntity source);方法就可以将TargetEntity转换为SourceEntity而不需要重新写@Mapping注解

合并转换

即将多个对象的值合并赋值给一个对象

@Mappings({
    @Mapping(target = "parentId", expression = "java(source.getParentId().toString())"),
    @Mapping(source = "article.name", target = "realName"),
    @Mapping(source = "source.createDate", target = "updateDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
TargetEntity mergeEntity(SourceEntity source, BusinessArticle article);

默认值

当源对象的属性没有值时,可以指定一个默认值

@Mapping(source = "name", target = "realName", defaultValue = "defaultName")
TargetEntity defaultValue(SourceEntity source);

如果SourceEntity的name为空,则会默认赋值为defaultName

自动注入

如果想在其他地方通过@Autoware方式注入转换类,需要将转换类的@Mapper的componentModel属性设置为spring,即@Mapper(componentModel = "spring")

自定义映射

当我们想根据自已的想法进行映射时,可以自定义映射规则

@Named("sexFormat")
class CusMapper{

    @Named("sexFormat")
    String toSex(Integer sex){
        if (0 == sex){
            return "男";
        }else {
            return "女";
        }
    }
}

转换类上将自定义映射导入

@Mapper(componentModel = “spring”,uses = {CusMapper.class})

在转换方法上使用自定义映射规则

@Mapping(source = "sexInt", target = "sex", qualifiedByName = "sexFormat")
TargetEntity sexMapper(SourceEntity source);

可能遇到的问题

问题1、作者使用的是Idea2022.1,在编译时报错

java: Internal error in the mapping processor: java.lang.NullPointerException

解决方法:

按以下方法在修改idea的设置

Setting -->Build,Execution,Deployment -->Compiler -->User-local build

加上参数:

-Djps.track.ap.dependencies=false

问题2、在运行时发生如下报错

java: No property named “name” exists in source parameter(s). Did you mean “id”?

根据报错信息得知是没有找到name这个属性,但是我们的实体类是有name这个属性的,在查询后得知,是因为lombok插件导致的

解决方法:

1、将lombok的依赖放在mapstruct前面
2、修改lombok与mapstruct的版本,使两者版本适配
3、在pom文件中增加binding插件,将mapstruct与lombok进行绑定,具体内容如下

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.3.1.Final</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.20</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

👍 欢迎前往博客主页查看更多内容

👍 如果觉得不错,期待您的点赞、收藏、评论、关注

👍 ​ 如有错误欢迎指正!

👍 ​ Gitee地址:https://gitee.com/ralphchen/ralph-cloud

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapStruct是一个用于Java中实体与模型间属性转换的优秀工具。当我们在开发中,经常会遇到实体类与模型类之间的属性不匹配的情况。对于这种情况,我们可以使用MapStruct来简化转换过程。 首先,我们需要在项目中添加MapStruct的依赖。然后,在实体类和模型类中使用注解来标识需要进行属性转换的字段。例如,我们可以在实体类的字段上加上`@Mapping`注解,指定需要转换的目标字段,以及在模型类的字段上使用`@Mapping(target = "sourceField")`注解来指定需要从哪个源字段进行转换。 接下来,我们需要创建一个转换器的接口,并使用`@Mapper(componentModel = "spring")`注解来标识该接口为MapStruct的映射器。然后,我们可以在接口中定义转换方法,并使用`@Mapping`注解来指定具体的属性转换规则。 在使用MapStruct进行属性转换时,只需通过调用映射器接口中的方法即可完成转换MapStruct会根据注解配置自动执行转换过程,将实体类中不匹配的属性值转换到模型类中相应的字段上。 另外,为了提高转换效率,我们可以在编译过程中使用MapStruct的编译器插件。该插件可以生成对应的转换器实现类,避免了通过反射等方式进行转换,大大提高了性能。 总的来说,使用MapStruct可以方便快捷地处理Java中实体与模型间属性不匹配的转换问题。通过注解配置和自动生成的转换器实现类,可以避免手动编写繁琐的属性赋值代码,提高开发效率同时保证转换过程的准确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值