MapStruct代替BeanUtils.copyProperties ()使用

1.为什么MapStruct代替BeanUtils.copyProperties ()

第一:因为BeanUtils 采用反射的机制动态去进行拷贝映射,特别是Apache的BeanUtils的性能很差,而且并不支持所有数据类型的拷贝,虽然使用较为方便,但是强烈不建议使用;

第二:虽然Spring的BeanUtils类所带方法比Apache的的BeanUtils的性能好点,但是性能还是很差,没办法跟mapStruct相对比,使用方便但是也不建议使用(性能不好会很卡机);

第三:MapStruct采用的是单纯的 get 和 set 方法,但是使用相对BeanUtils来说要多一个步骤,推荐使用,性能很好。

2.MapStruct的使用步骤

官网:Installation – MapStruct(个人始终坚持学一个东西先从官网了解开始)

1.添加依赖:

<properties>
 <org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
    </properties>
<dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

2.准备好要转换的类:AccountDTO属性值赋给Accout

AccountDTO:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AccountDTO {

    private static final long serialVersionUID = 1L;

    @NotBlank
    private String userName;

    @NotNull
    private Integer age;

    private String password;

    private String phone;

    @Email
    private String email;

}

Account为:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(value = "tb_account")
public class Account {

    private static final long serialVersionUID = 1L;

    private String accountId;

    private String userName;

    private String password;

    private String phone;

    private String email;

    private Integer age;

    private Date birthday;

}

3.写一个Interface接口:

@Mapper
public interface AccountConvertor {
    AccountConvertor ACCOUNT = Mappers.getMapper(AccountConvertor.class);

    /**
     * 将账户类AccountDTO的值赋给为Account
     * @param accountDTO
     * @return
     */
    Account convertAccount(AccountDTO accountDTO);

}

4.Interface接口的使用:

上述是对于AccountDTO和Account对应的属性名以及属性的数据类型完全一致的情况可执行。

但是如果属性名或者数据类型对不上的话比如如下:

要解决问题需按以下来:

第一步:继续添加依赖:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source> <!-- depending on your project -->
                <target>1.8</target> <!-- depending on your project -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <!-- other annotation processors -->
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

2.改良Interface接口:

重新测试即可。

相关推荐

  1. MapStruct

    2024-03-16 02:58:02       56 阅读
  2. springboot mapstruct

    2024-03-16 02:58:02       52 阅读
  3. ruoyi-nbcio-plus后端里mapstruct-plus和lombok的使用

    2024-03-16 02:58:02       42 阅读
  4. Spring Boot集成mapstruct快速入门指南

    2024-03-16 02:58:02       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 02:58:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 02:58:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 02:58:02       82 阅读
  4. Python语言-面向对象

    2024-03-16 02:58:02       91 阅读

热门阅读

  1. PHP将HTML标签转化为图片

    2024-03-16 02:58:02       41 阅读
  2. js判断有无从登录页面打开

    2024-03-16 02:58:02       38 阅读
  3. 如何声明一个类?类如何继承?

    2024-03-16 02:58:02       45 阅读
  4. C++设计模式-单例模式

    2024-03-16 02:58:02       39 阅读
  5. 使用Autogen和本地LLM加速开发周期

    2024-03-16 02:58:02       41 阅读
  6. Qt+FFmpeg+opengl从零制作视频播放器-7.OpenGL播放视频

    2024-03-16 02:58:02       40 阅读
  7. 【烹饪】清炒菠菜的学习笔记

    2024-03-16 02:58:02       44 阅读
  8. Python中有哪些常用的标准库?

    2024-03-16 02:58:02       44 阅读
  9. CMake官方教程4--使用表达式生成器

    2024-03-16 02:58:02       42 阅读
  10. GB4806.9 食品接触金属餐具、厨具检测第三方机构

    2024-03-16 02:58:02       57 阅读
  11. EXCEL+PYTHON学习2:

    2024-03-16 02:58:02       42 阅读