国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

淺談java對(duì)象之間相互轉(zhuǎn)化的多種方式

瀏覽:100日期:2022-08-26 14:32:51

第一種:使用org.apache.commons.beanutils.PropertyUtils.copyProperties()拷貝一個(gè)bean中的屬性到另一個(gè)bean中,第一個(gè)參數(shù)是目標(biāo)bean,第二個(gè)參數(shù)是源bean。

特點(diǎn):

1.它的性能問題相當(dāng)差

2.PropertyUtils有自動(dòng)類型轉(zhuǎn)換功能,而java.util.Date恰恰是其不支持的類型

3.PropertyUtils支持為null的場(chǎng)景;

public static void copyProperties(Object dest, Object orig) {try { PropertyUtils.copyProperties(dest, orig);} catch (IllegalAccessException | InvocationTargetExceptionthrow new IllegalArgumentException(e);| NoSuchMethodException e) { }}

第二種:使用BeanUtils.copyProperties()拷貝一個(gè)bean中的屬性到另一個(gè)bean中,第一個(gè)參數(shù)是目標(biāo)bean,第二個(gè)參數(shù)是源bean。

特點(diǎn):

1.BeanUtils速度相對(duì)快一些

2.BeanUtils沒有自動(dòng)轉(zhuǎn)換功能,遇到參數(shù)名相同,類型不同的參數(shù)不會(huì)進(jìn)行賦值。

3.BeanUtils對(duì)部分屬性不支持null的情況,Ineger、Boolean、Long等不支持

public static void copyProperties(Object dest, Object orig) {try { BeanUtils.copyProperties(dest, orig);} catch (IllegalAccessException | InvocationTargetException e) {throw new IllegalArgumentException(e);}}

第三種:將java對(duì)象轉(zhuǎn)換為json,然后將JSON轉(zhuǎn)換成Java對(duì)象。注意屬性名要相同---采取com.fasterxml.jackson.databind.ObjectMapper的方法

特點(diǎn):

1.屬性名一致就可以轉(zhuǎn)換

2.效率及異常還未驗(yàn)證

private static final ObjectMapper MAPPER = new ObjectMapper();String jsonStr = MAPPER.writeValueAsString(obj);RabbitMQDataDto detail = MAPPER.readValue(jsonStr , RabbitMQDataDto.class);

第四種:將java對(duì)象轉(zhuǎn)換為json,然后將JSON轉(zhuǎn)換成Java對(duì)象。注意屬性名要相同---采取com.alibaba.fastjson.JSON

特點(diǎn):

1.需要拷貝的兩個(gè)對(duì)象里面都包含有另一個(gè)對(duì)象,但是另一個(gè)對(duì)象里面屬性相同但是對(duì)象名不同;

采用json來轉(zhuǎn)換只要屬性名一致即可

InsurePersonInfoDto applicantInfo = insureRelevantPersonInfoDto.getApplicantInfo();ThirdPartInsurePersonInfo thirdPartApplicantInfo = JSON.parseObject(JSON.toJSONString(applicantInfo), new TypeReference<ThirdPartInsurePersonInfo>() {});

補(bǔ)充知識(shí):javabean實(shí)體類與實(shí)體類之間的快速轉(zhuǎn)換

一、Dozer是什么?

dozer是一個(gè)能把實(shí)體和實(shí)體之間進(jìn)行轉(zhuǎn)換的工具.只要建立好映射關(guān)系.就像是ORM的數(shù)據(jù)庫和實(shí)體映射一樣.

使用方法示例如下:

// article(PO) -> articleVO

ArticleVO articleVO = dozerMapper.map(article, ArticleVO.class);

這段示例代碼。將從數(shù)據(jù)庫里面查詢得到的PO對(duì)象article,轉(zhuǎn)換為VO對(duì)象articleVO,轉(zhuǎn)換過程將所有同名同類型的數(shù)據(jù)自動(dòng)賦值給articleVO的成員變量,當(dāng)然除了reader(因?yàn)镻O里面沒有reader數(shù)組數(shù)據(jù))。轉(zhuǎn)換需要寫屬性之間的映射么?不! 默認(rèn)是根據(jù)屬性名稱來匹配的.

如果沒有Dozer我們進(jìn)行,對(duì)象之間的轉(zhuǎn)換賦值,我們會(huì)怎么做?下面的這5行等于上面的一行。

articleVO.setId(article.getId());articleVO.setAuthor(article.getAuthor());articleVO.setTitle(article.getTitle());articleVO.setContent(article.getContent());articleVO.setCreateTime(article.getCreateTime());

二、使用Dozer進(jìn)行實(shí)體類的轉(zhuǎn)換:

首先引入依賴

<dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.4.0</version> </dependency>

注入一個(gè)工具類DozerBeanMapper 到上下文中,

@Configuration public class DozerBeanMapperConfigure { @Bean public DozerBeanMapper mapper() { DozerBeanMapper mapper = new DozerBeanMapper(); return mapper; } }

注入然后開始使用啦

@Autowired

protected Mapper dozerMapper;

在實(shí)際應(yīng)用中,我們不只需要PO轉(zhuǎn)VO,有時(shí)還需要List轉(zhuǎn)List.寫一個(gè)工具類,實(shí)現(xiàn)List轉(zhuǎn)List

public class DozerUtils { static DozerBeanMapper dozerBeanMapper = new DozerBeanMapper(); public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass){ List destinationList = Lists.newArrayList(); for (Iterator i$ = sourceList.iterator(); i$.hasNext();){ Object sourceObject = i$.next(); Object destinationObject = dozerBeanMapper.map(sourceObject, destinationClass); destinationList.add(destinationObject); } return destinationList; }}

以上這篇淺談java對(duì)象之間相互轉(zhuǎn)化的多種方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 凌云县| 临沂市| 东阳市| 岳阳县| 伊宁市| 仁寿县| 大冶市| 汨罗市| 沿河| 澄城县| 桦南县| 雅江县| 饶阳县| 隆德县| 毕节市| 清新县| 英超| 西平县| 凤山市| 吉首市| 中卫市| 通州市| 远安县| 双鸭山市| 海安县| 高碑店市| 东辽县| 滦南县| 九江市| 阜新市| 津南区| 西昌市| 定南县| 田林县| 彩票| 义乌市| 日喀则市| 青岛市| 乐陵市| 新安县| 郧西县|