码迷,mamicode.com
首页 > 其他好文 > 详细

用dozer实现两对象之间的复制

时间:2018-07-18 17:00:25      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:return   beans   nmap   extc   actor   etl   ring   div   ati   

1。引包

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

2.建一个xml文件,copy_dozer.xml   其中如果有字段没有对上,可以用field

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">
	<!--客户模块 -->
	<mapping>
		<class-a>com.banksteel.openerp.customer.vo.CmCustomerVO
		</class-a>
		<class-b>com.banksteel.openerp.customer.api.entity.CmCustomer
		</class-b>
	</mapping>

      <!-- 财务管理模块 -->
      <mapping>
        <class-a>com.banksteel.openerp.finance.vo.FmReceivablesRecordVO
        </class-a>
        <class-b>com.banksteel.openerp.finance.api.entity.FmReceivablesRecord
        </class-b>
        <field>
          <a get-method="getApplyTimeToLong">applyTime</a>
          <b get-method="getApplyTimeToString">applyTime</b>
        </field>
      </mapping>

</mappings>

3.建一个公共方法类 BeanFactory.java

package com.banksteel.openerp.commons.dozer;

import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BeanFactory {
	private static DozerBeanMapper dozerBeanMapper;
	private static Logger logger = LoggerFactory.getLogger(BeanFactory.class);
	static {
		dozerBeanMapper = new DozerBeanMapper();
          // 这里加上要引人的xml文件 String[] pathes = { "admin-dozer.xml", "customer-dozer.xml", "facade-dozer.xml", "finance-dozer.xml", "log-dozer.xml", "mbpm-dozer.xml", "mobile-dozer.xml", "purchase-dozer.xml", "quartz-dozer.xml", "repertory-dozer.xml", "sale-dozer.xml", "system-dozer.xml", }; try { for (String path : pathes) { InputStream inputStream = null; try { inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("dozer/" + path); } catch (Exception e) { logger.error("dozer文件不存在:" + path); } if (inputStream != null) { dozerBeanMapper.addMapping(inputStream); inputStream.close(); } } } catch (Exception e) { e.printStackTrace(); } } public static void convert(Object source, Object target) { dozerBeanMapper.map(source, target); } public static Map<String, Object> bean2Map(Object obj) { Map<String, Object> params = new HashMap<String, Object>(0); try { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!"class".equals(name)) { params.put(name, propertyUtilsBean.getNestedProperty(obj, name)); } } } catch (Exception e) { e.printStackTrace(); } return params; } }

 4.复制。把一个对象里面的值复制到另一个对象里面

BeanFactory.convert(cmCustomerVO, cmCustomer);

  

 

  

 

用dozer实现两对象之间的复制

标签:return   beans   nmap   extc   actor   etl   ring   div   ati   

原文地址:https://www.cnblogs.com/guilf/p/9329176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!