`

JAVA进阶:VO(DTO)与PO(DAO)之间的转换

    博客分类:
  • JAVA
 
阅读更多

 

PO即 Persistence Object
  VO即 Value Object

 VO和PO的主要区别在于:
  VO是独立的Java Object。
  PO是由Hibernate纳入其实体容器(Entity Map)的对象,它代表了与数据库中某条记录对应的Hibernate实体,PO的变化在事务提交时将反应到实际数据库中。

 实际上,这个VO被用作Data Transfer Object,即所谓的DTO。想必,VO就是Data Access Object ---DAO了啦。为什么要有这二者之分呢?如在传统的MVC架构中,位于Model层的PO,是否允许被传递到其他层面。由于PO的更新最终将被映射到 实际数据库中,如果PO在其他层面(如View层)发生了变动,那么可能会对Model层造成意想不到的破坏。

 主要想说的还是如何进行二者之间的转换:
  属性复制可以通过Apache Jakarta Commons Beanutils(http://jakarta.apache.org/commons/beanutils/ )组件提供的属性批量复制功能,避免繁复的get/set操作。down下来之后,里面的API DOC一应俱全。

 对于一些无需处理其它处理(如过滤)直接用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:

TUseruser = new TUser();
TUseranotherUser
= 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 (IllegalAccessExceptione) {
e.printStackTrace();
}
catch (InvocationTargetExceptione) {
e.printStackTrace();
}
 



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

 范例2

/***/ /** //*
*Createdon2006-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 {

/***/ /** */ /***/ /**
*Copypropertiesoforigtodest
*ExceptiontheEntityandCollectionType
*
@param dest
*
@param orig
*
@return thedestbean
*/

public static ObjectcopyProperties(Objectdest,Objectorig) {
if (dest == null || orig == null ) {
return dest;
}


PropertyDescriptor[]destDesc
= PropertyUtils.getPropertyDescriptors(dest);
try {
for ( int i = 0 ;i < destDesc.length;i ++ ) {
ClassdestType
= destDesc[i].getPropertyType();
ClassorigType
= PropertyUtils.getPropertyType(orig,destDesc[i].getName());
if (destType != null && destType.equals(origType)
&& ! destType.equals(Class. class )) {
if ( ! Collection. class .isAssignableFrom(origType)) {
try {
Objectvalue
= PropertyUtils.getProperty(orig,destDesc[i].getName());
PropertyUtils.setProperty(dest,destDesc[i].getName(),value);
}
catch (Exceptionex) {
}

}

}

}


return dest;
}
catch (Exceptionex) {
throw new CopyException(ex);
// returndest;
}

}


/***/ /** */ /***/ /**
*Copypropertiesoforigtodest
*ExceptiontheEntityandCollectionType
*
@param dest
*
@param orig
*
@param ignores
*
@return thedestbean
*/

public static ObjectcopyProperties(Objectdest,Objectorig,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 ;
}


ClassdestType
= destDesc[i].getPropertyType();
ClassorigType
= PropertyUtils.getPropertyType(orig,destDesc[i].getName());
if (destType != null && destType.equals(origType)
&& ! destType.equals(Class. class )) {
if ( ! Collection. class .isAssignableFrom(origType)) {
Objectvalue
= PropertyUtils.getProperty(orig,destDesc[i].getName());
PropertyUtils.setProperty(dest,destDesc[i].getName(),value);
}

}

}


return dest;
}
catch (Exceptionex) {
throw new CopyException(ex);
}

}


static boolean contains(String[]ignores,Stringname) {
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中则可能是此对象的全部属性。其中的一些转换则需要依据前台需要针对性地处理啦!

Reference: Apache DOC and <>

分享到:
评论

相关推荐

    Java中 PO VO BO DTO DAO 和 POJO 关系图

    Java中 PO VO BO DTO DAO 和 POJO 关系图

    java术语(PO/POJO/VO/BO/DAO/DTO)

    NULL 博文链接:https://ewf-momo.iteye.com/blog/1738853

    vo bo po dto dao区别

    本人以前搞不懂这些o的区别,特意查找资料总结了一下,希望也可以帮到其他人

    POBOVODTOPOJODAO.zip_dto_java dto dao_java vo_qovod

    PO可以严格对应数据库表,一张表对映一个PO。... VO:value object值对象、view object视图对象 PO:持久对象 QO:查询对象 DAO:数据访问对象——同时还有DAO模式 DTO:数据传输对象——同时还有DTO模式

    java中PO、VO、BO、POJO、DAO、DTO、TO、QO、Bean、conn的理解

    主要介绍了java中PO、VO、BO、POJO、DAO、DTO、TO、QO、Bean、conn的理解,需要的朋友可以参考下

    tim-root:基于Spring引导构建Spring Cloud项目

    common-dto:数据交互层(VO、PO、BO、DTO等) service-user:用户服务提供者(用户注册、登录、权限等) operation-system:运营系统(服务消费者) 集成技术 spring spring mvc spring boot spring boot actuator...

    java8源码-ICS:SpringCloud项目集成

    java8 源码 ICS项目 模型篇 请求出入参 请求入参 所有Controller入参,一律使用 DTO结尾进行交互 所有 DTO 命名,前面采用驼峰命名,后面DTO大写 所有DTO内参数,如果是一组,请用List&lt;类型&gt;进行接口,尽量不用分隔符隔...

    Blog:充当日常笔记或者个人博客吧

    Nginx配置文件详解Linux 安装JenkinsJAVA 多线程详解java 多线程学习How to create a Hello World with IntelliJ and Aspect JJava各种对象(PO,BO,VO,DTO,POJO,DAO,Entity,JavaBean,JavaBeans)的区分Java finally...

    knowledge

    应用对象,Web层和Service层之间抽象的复用对象 VO View Object 显示层对象,Web向模板传输的对象 Query 数据查询对象 各层接收上层的查询请求 个人使用 简写 全写 说明 DO Data Object 数据库表映射类,DAO层向上...

Global site tag (gtag.js) - Google Analytics