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

Posted guilf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用dozer实现两对象之间的复制相关的知识,希望对你有一定的参考价值。

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实现两对象之间的复制的主要内容,如果未能解决你的问题,请参考以下文章

对象的克隆,Dozer的使用

SpringBoot2.x 集成 Dozer

四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)

四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)

四品达通用权限系统__pd-tools-dozer(对象属性转换)和pd-tools-validator(后端表单数据验证)

JavaBean递归拷贝工具类Dozer