`
snoopy7713
  • 浏览: 1140807 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

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.

 

 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属性的时候达到自己的要求,如:

 

/** //*
 * 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中则可能是此对象的全部属性。其中的一些转换则需要依据前台需要针对性地处理啦!

Reference:  Apache DOC and <>   

 

 

 

分享到:
评论

相关推荐

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

    本文将详细解析"PO/POJO/VO/BO/DAO/DTO"这六个概念,并探讨它们在实际项目开发中的作用和应用场景。 1. PO(Persistent Object,持久化对象) PO是指与数据库表结构一一对应的Java对象,它通常包含了数据库表中的...

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

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

    po vo dto bo to

    ### Java中的PO、VO、TO、BO、DAO与POJO详解 #### 一、概述 在Java企业级应用开发中,经常会遇到各种类型的对象,如PO、VO、TO、BO、DAO以及POJO等。这些对象各有侧重,在系统架构的不同层次扮演着不同的角色。...

    VO / DTO / BO / ORM DAO entity DO PO/ POJO(分层领域模型规约)整理

    本文将详细介绍VO (View Object)、DTO (Data Transfer Object)、BO (Business Object)、ORM (Object Relational Mapping)、DAO (Data Access Object)、Entity (实体)、DO (Data Object)、PO (Persistent Object)、...

    Java的几种对象(PO-VO-DAO-BO-POJO)解释

    值对象(Value Object,简称VO),通常用于业务层之间的数据传递,与PO类似,它也仅包含数据。但是,VO更倾向于抽象出具体的业务对象,可以根据实际业务需求选择是否与数据库表进行对应。在Web开发中,VO经常被用作...

    Java的(PO,VO,TO,BO,DAO,POJO)解释

    "Java的(PO,VO,TO,BO,DAO,POJO)解释" Java作为一门流行的编程语言,在软件开发中扮演着重要角色,其中一些关键概念和技术为开发者提供了方便快捷的开发体验。本篇文章将对Java中的PO、VO、TO、BO、DAO、POJO等概念...

    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 的理解 PO(Persistent Object):持久对象,指的是在 O/R Mapping 中将对象与关系数据库绑定的对象。PO 是由一组属性和属性的 get 和 set 方法组成。它...

    EX快速开发一个功能.docx

    - **POJO(Plain Ordinary Java Object)**:简单的Java对象,纯粹的Java类,只有属性和getter、setter方法,无特定框架依赖,可以作为VO或DTO(Data Transfer Object)使用,用于不同层之间数据的传输。 3. **...

    《阿里巴巴Java开发手册》v1.4.0(详尽版).docx

    - **规定**:类名统一使用 UpperCamelCase 风格,特殊情况除外(如 DO/BO/DTO/VO/AO/PO/UID 等)。 - **示例**:正确的命名方式如 `MarcoPolo`、`UserDO`、`XmlService`、`TcpUdpDeal`、`TaPromotion`。 4. **变量...

    Java代码规范.pdf

    - 类名应使用UpperCamelCase风格,例外情况为DO、BO、DTO、VO、AO、PO、UID等。 - 方法名、参数名、成员变量、局部变量应统一使用lowerCamelCase风格。 - 常量命名应全部大写,单词间用下划线隔开。 - 抽象类...

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

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

    阿里最新Java开发规范

    类名应当遵循`UpperCamelCase`风格,除非是某些特定的缩写词如`DO`(Data Object)、`BO`(Business Object)、`DTO`(Data Transfer Object)、`VO`(View Object)、`AO`(Application Object)、`PO`(Persistent...

    java8源码-ICS:SpringCloud项目集成

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

    Java Web开发学习路线图(JBoss Seam方向)

    4. **DAO模式**:了解DAO(Data Access Object)模式,掌握DO、PO、VO、DTO、Domain Object等术语的含义。 #### 七、企业级应用开发 1. **EJB2**:探索Enterprise JavaBeans 2.0规范,包括EntityBean和SessionBean...

    MVC设计模式

    #### 五、PO/VO/DAO/BO/DTO的区分 - **PO (Persistant Object)**:持久化对象,用来表示数据库表中的记录,每个PO对象对应一条数据库记录。 - **VO (Value Object)**:值对象,用于业务逻辑层中的数据传递。VO更多...

Global site tag (gtag.js) - Google Analytics