日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dto转化 vo_VO(DTO)与PO(DAO)之间的转换

發(fā)布時間:2023/12/20 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dto转化 vo_VO(DTO)与PO(DAO)之间的转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

VO和PO的主要區(qū)別在于:

VO是獨立的Java Object。

PO是由Hibernate納入其實體容器(Entity Map)的對象,它代表了與數(shù)據(jù)庫中某條記錄對應(yīng)的Hibernate實體,PO的變化在事務(wù)提交時將反應(yīng)到實際數(shù)據(jù)庫中。

實際上,這個VO被用作Data Transfer Object,即所謂的DTO。想必,VO就是Data Access Object ---DAO了啦。為什么要有這二者之分呢?如在傳統(tǒng)的MVC架構(gòu)中,位于Model層的PO,是否允許被傳遞到其他層面。由于PO的更新最終將被映射到實際數(shù)據(jù)庫中,如果PO在其他層面(如View層)發(fā)生了變動,那么可能會對Model層造成意想不到的破壞。

主要想說的還是如何進行二者之間的轉(zhuǎn)換:

屬性復(fù)制可以通過Apache Jakarta Commons Beanutils(http://jakarta.apache.org/commons/beanutils/)組件提供的屬性批量復(fù)制功能,避免繁復(fù)的get/set操作。down下來之后,里面的API DOC一應(yīng)俱全。

對于一些無需處理其它處理(如過濾)直接用BeanUtilsBean.copyProperties方法,其參考如下:

public static void copyProperties(java.lang.Object dest,

java.lang.Object orig)

throws java.lang.IllegalAccessException,

java.lang.reflect.InvocationTargetExceptioCopy property values from the origin bean to the destination bean for all cases where the property names are the same.

范例1:

TUser?user = new TUser();

TUser?anotherUser = new TUser();

user.setName( " Emma " );

user.setUserType( 1 );

try {

BeanUtils.copyProperties(anotherUser,user);

System.out.println( " UserName?=> " + anotherUser.getName()

);

System.out.println( " UserType?=> " + anotherUser.getUserType()

);

} catch (IllegalAccessException?e) {

e.printStackTrace();

} catch (InvocationTargetException?e) {

e.printStackTrace();

}

也可以利用其中的一些方法在copy屬性的時候達到自己的要求,如:

范例2

/**?*/ /** //*

?*?Created?on?2006-4-26

*/package com.util;

import java.beans.PropertyDescriptor;

import java.util.Collection;

import org.apache.commons.beanutils.PropertyUtils;

/**?*/ /** *//**?*/ /** ?*?CopyUtil

?* @author Jkallen

*/

public class CopyUtil {

????

/**?*/ /** *//**?*/ /** ?????*?Copy?properties?of?orig?to?dest

?????*?Exception?the?Entity?and?Collection?Type

?????* @param dest

?????* @param orig

?????* @return the?dest?bean

*/

public static Object?copyProperties(Object?dest,?Object?orig) {

if (dest == null || orig == null ) {

return dest;

????????}????????

????????PropertyDescriptor[]?destDesc = PropertyUtils.getPropertyDescriptors(dest);

try {

for ( int i = 0 ;?i < destDesc.length;?i ++ ) {

????????????????Class?destType = destDesc[i].getPropertyType();

????????????????Class?origType = PropertyUtils.getPropertyType(orig,?destDesc[i].getName());

if (destType != null && destType.equals(origType)

&& ! destType.equals(Class. class )) {

if ( ! Collection. class .isAssignableFrom(origType)) {????????????????????

try {

????????????????????????????Object?value = PropertyUtils.getProperty(orig,?destDesc[i].getName());

????????????????????????????PropertyUtils.setProperty(dest,?destDesc[i].getName(),?value);

????????????????????????}catch (Exception?ex) {????????????????????????????

????????????????????????}????????????????????}????????????????}????????????}????????????

return dest;

????????}catch (Exception?ex) {

throw new CopyException(ex);

// return?dest; }????}????

/**?*/ /** *//**?*/ /** ?????*?Copy?properties?of?orig?to?dest

?????*?Exception?the?Entity?and?Collection?Type

?????* @param dest

?????* @param orig

?????* @param ignores

?????* @return the?dest?bean

*/

public static Object?copyProperties(Object?dest,?Object?orig,?String[]?ignores) {

if (dest == null || orig == null ) {

return dest;

????????}????????

????????PropertyDescriptor[]?destDesc = PropertyUtils.getPropertyDescriptors(dest);

try {

for ( int i = 0 ;?i < destDesc.length;?i ++ ) {

if (contains(ignores,?destDesc[i].getName())) {

continue ;

????????????????}????????????????

????????????????Class?destType = destDesc[i].getPropertyType();

????????????????Class?origType = PropertyUtils.getPropertyType(orig,?destDesc[i].getName());

if (destType != null && destType.equals(origType)

&& ! destType.equals(Class. class )) {

if ( ! Collection. class .isAssignableFrom(origType)) {

????????????????????????Object?value = PropertyUtils.getProperty(orig,?destDesc[i].getName());

????????????????????????PropertyUtils.setProperty(dest,?destDesc[i].getName(),?value);

????????????????????}????????????????}????????????}????????????

return dest;

????????}catch (Exception?ex) {

throw new CopyException(ex);

????????}????}????

static boolean contains(String[]?ignores,?String?name) {

boolean ignored = false ;

for ( int j = 0 ;?ignores != null && j < ignores.length;?j ++ ) {

if (ignores[j].equals(name)) {

????????????????ignored = true ;

break ;

????????????}????????}????????

return ignored;

????}}

可以看到,在范例1中通過方法copyProperties的時候,二者之間在的屬性名必須相同(Copy property values from the origin bean to the destination bean for all cases where the property names are the same)。而在范例2中通過

Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());

PropertyUtils.setProperty(dest, destDesc[i].getName(), value);

也是將源與目的之間copy相同的屬性名。而VO是在前臺顯示,所以難免會用到PO中所不存在的屬性值。比如PO中可能是一個對象,而VO中則可能是此對象的全部屬性。其中的一些轉(zhuǎn)換則需要依據(jù)前臺需?要針對性地處理啦!

在copy的過程中,若實體中存在一對多,多對多等關(guān)系,則DTO中也應(yīng)該存在此關(guān)系,此時不能直接將內(nèi)部的DTO or List(一對多)里面的數(shù)據(jù)一次性全部拷過去,這時應(yīng)該對每個DTO進行copy,當(dāng)然,若在前臺你無需用到相關(guān)的DTO則可以跳過。而對于一對多(LIST),而應(yīng)將實體里面的每個一對多轉(zhuǎn)換成對應(yīng)的DTO,再依次放到LSIT里面,再將此LIST賦值給(父)DTO,這里面的關(guān)系如同(一個簡單的)遞歸關(guān)系。---更新于5-25-2006

posted on 2008-04-24 12:04 caihaibo 閱讀(2715) 評論(0) ?編輯 ?收藏 所屬分類: hibernate

總結(jié)

以上是生活随笔為你收集整理的dto转化 vo_VO(DTO)与PO(DAO)之间的转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。