欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中DTO與Entity拷貝轉(zhuǎn)換的方法小結(jié)

 更新時間:2025年02月11日 09:30:46   作者:Andya  
在?Java?開發(fā)中,DTO(Data?Transfer?Object)和?Entity(實體類)是常見的兩種數(shù)據(jù)模型,本文將介紹幾種常見的工具類和自定義方式來實現(xiàn)這種轉(zhuǎn)換,感興趣的可以了解下

引言

在 Java 開發(fā)中,DTO(Data Transfer Object)Entity(實體類)是常見的兩種數(shù)據(jù)模型。DTO 通常用于數(shù)據(jù)傳輸,而 Entity 用于與數(shù)據(jù)庫交互。在實際開發(fā)中,經(jīng)常需要在 DTO 和 Entity 之間進行數(shù)據(jù)的拷貝和轉(zhuǎn)換。本文將介紹幾種常見的工具類和自定義方式來實現(xiàn)這種轉(zhuǎn)換,并提供相應的代碼示例。

手動拷貝

手動拷貝是最直接的方式,通過編寫代碼逐個字段進行賦值。

代碼示例

public class UserEntity {
    private Long id;
    private String name;
    private Integer age;
    // 省略 getter 和 setter 方法
}

public class UserDTO {
    private Long id;
    private String name;
    private Integer age;
    // 省略 getter 和 setter 方法
}

public class UserConverter {
    public static UserDTO toDTO(UserEntity entity) {
        UserDTO dto = new UserDTO();
        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setAge(entity.getAge());
        return dto;
    }

    public static UserEntity toEntity(UserDTO dto) {
        UserEntity entity = new UserEntity();
        entity.setId(dto.getId());
        entity.setName(dto.getName());
        entity.setAge(dto.getAge());
        return entity;
    }
}

優(yōu)點

  • 精確控制字段的轉(zhuǎn)換邏輯。
  • 不依賴外部庫。

缺點

  • 代碼冗長,容易出錯。
  • 當字段較多時,維護成本較高。

使用 BeanUtils

Apache Commons BeanUtils 提供了 BeanUtils.copyProperties 方法,可以簡化字段拷貝。

代碼示例

import org.apache.commons.beanutils.BeanUtils;

public class UserConverter {
    public static UserDTO toDTO(UserEntity entity) throws Exception {
        UserDTO dto = new UserDTO();
        BeanUtils.copyProperties(dto, entity);
        return dto;
    }

    public static UserEntity toEntity(UserDTO dto) throws Exception {
        UserEntity entity = new UserEntity();
        BeanUtils.copyProperties(entity, dto);
        return entity;
    }
}

優(yōu)點

簡化代碼,減少手動拷貝的工作量。

缺點

  • 需要額外引入 Apache Commons BeanUtils 庫。
  • 性能相對較低,因為它是通過反射實現(xiàn)的。

使用 Dozer

Dozer 是一個開源的 Java Bean 映射框架,支持復雜的數(shù)據(jù)映射。

代碼示例

import org.dozer.DozerBeanMapper;

public class UserConverter {
    private static final DozerBeanMapper mapper = new DozerBeanMapper();

    public static UserDTO toDTO(UserEntity entity) {
        return mapper.map(entity, UserDTO.class);
    }

    public static UserEntity toEntity(UserDTO dto) {
        return mapper.map(dto, UserEntity.class);
    }
}

優(yōu)點

  • 支持復雜的數(shù)據(jù)映射,包括嵌套對象和集合。
  • 配置靈活,可以通過 XML 或注解進行映射配置。

缺點

  • 需要額外引入 Dozer 庫。
  • 配置較為復雜,尤其是對于復雜的映射場景。

使用 MapStruct

MapStruct 是一個基于注解的代碼生成工具,可以在編譯時生成數(shù)據(jù)映射代碼。

代碼示例

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

@Mapper
public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    UserDTO toDTO(UserEntity entity);

    UserEntity toEntity(UserDTO dto);
}

優(yōu)點

  • 在編譯時生成代碼,性能高。
  • 支持復雜的數(shù)據(jù)映射,包括嵌套對象和集合。
  • 代碼簡潔,易于維護。

缺點

  • 需要額外引入 MapStruct 庫。
  • 需要配置注解,學習成本較高。

使用 ModelMapper

ModelMapper 是一個簡單易用的 Java Bean 映射庫,支持自動映射和自定義映射。

代碼示例

import org.modelmapper.ModelMapper;

public class UserConverter {
    private static final ModelMapper modelMapper = new ModelMapper();

    public static UserDTO toDTO(UserEntity entity) {
        return modelMapper.map(entity, UserDTO.class);
    }

    public static UserEntity toEntity(UserDTO dto) {
        return modelMapper.map(dto, UserEntity.class);
    }
}

優(yōu)點

  • 簡單易用,支持自動映射。
  • 支持自定義映射規(guī)則。

缺點

  • 需要額外引入 ModelMapper 庫。
  • 性能相對較低,因為它是通過反射實現(xiàn)的。

自定義工具類

如果項目中對性能要求較高,且字段映射規(guī)則較為固定,可以自定義工具類來實現(xiàn)字段拷貝。

代碼示例

public class UserConverter {
    public static UserDTO toDTO(UserEntity entity) {
        return new UserDTO(entity.getId(), entity.getName(), entity.getAge());
    }

    public static UserEntity toEntity(UserDTO dto) {
        return new UserEntity(dto.getId(), dto.getName(), dto.getAge());
    }
}

優(yōu)點

  • 性能高,因為是直接調(diào)用構造函數(shù)。
  • 代碼簡潔,易于維護。

缺點

  • 不支持復雜的數(shù)據(jù)映射。
  • 如果字段較多,代碼量會增加。

總結(jié)

在實際開發(fā)中,選擇哪種方式取決于項目需求和團隊的技術棧。

  • 如果項目對性能要求較高,推薦使用 MapStruct 或自定義工具類;
  • 如果項目對開發(fā)效率要求較高,推薦使用 ModelMapper BeanUtils

到此這篇關于Java中DTO與Entity拷貝轉(zhuǎn)換的方法小結(jié)的文章就介紹到這了,更多相關Java DTO與Entity拷貝轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java實現(xiàn)簡單版貪吃蛇游戲

    Java實現(xiàn)簡單版貪吃蛇游戲

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單版貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • spring cloud 使用Eureka 進行服務治理方法

    spring cloud 使用Eureka 進行服務治理方法

    這篇文章主要介紹了spring cloud 使用Eureka 進行服務治理方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Java三大特性-封裝知識小結(jié)

    Java三大特性-封裝知識小結(jié)

    所有的面向?qū)ο缶幊陶Z言的思路都是差不多的,而這三大特性,則是思路中的支柱點,接下來我就重點講解了一下java三大特性-封裝,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-03-03
  • springboot配置mysql連接的實例代碼

    springboot配置mysql連接的實例代碼

    這篇文章主要介紹了springboot配置mysql連接的實例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot @ComponentScan掃描的局限性方式

    SpringBoot @ComponentScan掃描的局限性方式

    文章總結(jié):SpringBoot的@ComponentScan注解在掃描組件時存在局限性,只能掃描指定的包及其子包,無法掃描@SpringBootApplication注解自動配置的組件,使用@SpringBootApplication注解可以解決這一問題,它集成了@Configuration、@EnableAutoConfiguration
    2025-01-01
  • Mybatis-flex整合達夢數(shù)據(jù)庫的實現(xiàn)示例

    Mybatis-flex整合達夢數(shù)據(jù)庫的實現(xiàn)示例

    本文討論了國產(chǎn)達夢數(shù)據(jù)庫與Mybatis-flex框架的整合過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10
  • 圖解如何在Spring Boot中使用JSP頁面

    圖解如何在Spring Boot中使用JSP頁面

    這篇文章主要介紹了圖解如何在Spring Boot中使用JSP頁面,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • 淺析Spring配置文件

    淺析Spring配置文件

    本文主要對Spring配置文件進行了介紹。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • mybatis多層嵌套resultMap及返回自定義參數(shù)詳解

    mybatis多層嵌套resultMap及返回自定義參數(shù)詳解

    這篇文章主要介紹了mybatis多層嵌套resultMap及返回自定義參數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 深入探討Druid動態(tài)數(shù)據(jù)源的實現(xiàn)方式

    深入探討Druid動態(tài)數(shù)據(jù)源的實現(xiàn)方式

    Druid是一個高性能的實時分析數(shù)據(jù)庫,它可以處理大規(guī)模數(shù)據(jù)集的快速查詢和聚合操作,在Druid中,動態(tài)數(shù)據(jù)源是一種可以在運行時動態(tài)添加和刪除的數(shù)據(jù)源,使用動態(tài)數(shù)據(jù)源,您可以在Druid中輕松地處理不斷變化的數(shù)據(jù)集,本文講給大家介紹一下Druid動態(tài)數(shù)據(jù)源該如何實現(xiàn)
    2023-08-08

最新評論