Java中DTO與Entity拷貝轉(zhuǎn)換的方法小結(jié)
引言
在 Java 開發(fā)中,DTO(Data Transfer Object)
和 Entity
(實(shí)體類)是常見的兩種數(shù)據(jù)模型。DTO 通常用于數(shù)據(jù)傳輸,而 Entity 用于與數(shù)據(jù)庫交互。在實(shí)際開發(fā)中,經(jīng)常需要在 DTO 和 Entity 之間進(jìn)行數(shù)據(jù)的拷貝和轉(zhuǎn)換。本文將介紹幾種常見的工具類和自定義方式來實(shí)現(xiàn)這種轉(zhuǎn)換,并提供相應(yīng)的代碼示例。
手動(dòng)拷貝
手動(dòng)拷貝是最直接的方式,通過編寫代碼逐個(gè)字段進(jì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)點(diǎn)
- 精確控制字段的轉(zhuǎn)換邏輯。
- 不依賴外部庫。
缺點(diǎn)
- 代碼冗長,容易出錯(cuò)。
- 當(dāng)字段較多時(shí),維護(hù)成本較高。
使用 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)點(diǎn)
簡化代碼,減少手動(dòng)拷貝的工作量。
缺點(diǎn)
- 需要額外引入
Apache Commons BeanUtils
庫。 - 性能相對(duì)較低,因?yàn)樗峭ㄟ^反射實(shí)現(xiàn)的。
使用 Dozer
Dozer 是一個(gè)開源的 Java Bean
映射框架,支持復(fù)雜的數(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)點(diǎn)
- 支持復(fù)雜的數(shù)據(jù)映射,包括嵌套對(duì)象和集合。
- 配置靈活,可以通過 XML 或注解進(jìn)行映射配置。
缺點(diǎn)
- 需要額外引入 Dozer 庫。
- 配置較為復(fù)雜,尤其是對(duì)于復(fù)雜的映射場(chǎng)景。
使用 MapStruct
MapStruct 是一個(gè)基于注解的代碼生成工具,可以在編譯時(shí)生成數(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)點(diǎn)
- 在編譯時(shí)生成代碼,性能高。
- 支持復(fù)雜的數(shù)據(jù)映射,包括嵌套對(duì)象和集合。
- 代碼簡潔,易于維護(hù)。
缺點(diǎn)
- 需要額外引入
MapStruct
庫。 - 需要配置注解,學(xué)習(xí)成本較高。
使用 ModelMapper
ModelMapper
是一個(gè)簡單易用的 Java Bean
映射庫,支持自動(dòng)映射和自定義映射。
代碼示例
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)點(diǎn)
- 簡單易用,支持自動(dòng)映射。
- 支持自定義映射規(guī)則。
缺點(diǎn)
- 需要額外引入
ModelMapper
庫。 - 性能相對(duì)較低,因?yàn)樗峭ㄟ^反射實(shí)現(xiàn)的。
自定義工具類
如果項(xiàng)目中對(duì)性能要求較高,且字段映射規(guī)則較為固定,可以自定義工具類來實(shí)現(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ǎn)
- 性能高,因?yàn)槭侵苯诱{(diào)用構(gòu)造函數(shù)。
- 代碼簡潔,易于維護(hù)。
缺點(diǎn)
- 不支持復(fù)雜的數(shù)據(jù)映射。
- 如果字段較多,代碼量會(huì)增加。
總結(jié)
在實(shí)際開發(fā)中,選擇哪種方式取決于項(xiàng)目需求和團(tuán)隊(duì)的技術(shù)棧。
- 如果項(xiàng)目對(duì)性能要求較高,推薦使用
MapStruct
或自定義工具類; - 如果項(xiàng)目對(duì)開發(fā)效率要求較高,推薦使用
ModelMapper
或BeanUtils
。
到此這篇關(guān)于Java中DTO與Entity拷貝轉(zhuǎn)換的方法小結(jié)的文章就介紹到這了,更多相關(guān)Java DTO與Entity拷貝轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法
這篇文章主要介紹了spring cloud 使用Eureka 進(jìn)行服務(wù)治理方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05SpringBoot @ComponentScan掃描的局限性方式
文章總結(jié):SpringBoot的@ComponentScan注解在掃描組件時(shí)存在局限性,只能掃描指定的包及其子包,無法掃描@SpringBootApplication注解自動(dòng)配置的組件,使用@SpringBootApplication注解可以解決這一問題,它集成了@Configuration、@EnableAutoConfiguration2025-01-01Mybatis-flex整合達(dá)夢(mèng)數(shù)據(jù)庫的實(shí)現(xiàn)示例
本文討論了國產(chǎn)達(dá)夢(mèng)數(shù)據(jù)庫與Mybatis-flex框架的整合過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10mybatis多層嵌套resultMap及返回自定義參數(shù)詳解
這篇文章主要介紹了mybatis多層嵌套resultMap及返回自定義參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12深入探討Druid動(dòng)態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方式
Druid是一個(gè)高性能的實(shí)時(shí)分析數(shù)據(jù)庫,它可以處理大規(guī)模數(shù)據(jù)集的快速查詢和聚合操作,在Druid中,動(dòng)態(tài)數(shù)據(jù)源是一種可以在運(yùn)行時(shí)動(dòng)態(tài)添加和刪除的數(shù)據(jù)源,使用動(dòng)態(tài)數(shù)據(jù)源,您可以在Druid中輕松地處理不斷變化的數(shù)據(jù)集,本文講給大家介紹一下Druid動(dòng)態(tài)數(shù)據(jù)源該如何實(shí)現(xiàn)2023-08-08