对象mapping dozer,各种实现对比

Dozer

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.

Please read the about page for detailed information on Dozer.

---------------------------------------------------

Dozer支持 Java Bean 之间数据的递归拷贝。特别是一些复杂的java bean .

Dozer 支持简单的、复杂的、双向的、显隐式的映射以及递归映射。当然也支持集合属性及子元素的映射。

 

一般的BeanUtils需要copy的源和目标对象的字段名字要一致,而Dozer可以支持名字不一样的mapping,而且可以多个字段映射一个段,一个字段映射多个字段。

 

 

帮助文档

扫描二维码关注公众号,回复: 576098 查看本文章

http://dozer.sourceforge.net/dozer-user-guide.pdf

 

 

突然想测试一下:

Spring BeanUtils / Apache common BeanUtils /Dozer 性能测试

测试代码

 

 

package com.practise.dozer;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

import com.practise.dozer.beans.DestinationObject;
import com.practise.dozer.beans.SourceObject;

public class TestPrimitive {

	public static void main(String[] args) throws IllegalAccessException,
			InvocationTargetException {

		final Mapper mapper = new DozerBeanMapper();
		final int times = 1000000;

		new Thread(new Runnable() {

			@Override
			public void run() {
				long start = System.currentTimeMillis();
				DestinationObject destObject = null;
				for (int j = 0; j < times; j++) {
					destObject = mapper.map(getSourceObe(),
							DestinationObject.class);
				}
				System.out.println("Mapper cost "
						+ (System.currentTimeMillis() - start));
				System.out.println(destObject);
			}

		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				DestinationObject destObject2 = new DestinationObject();
				long start2 = System.currentTimeMillis();
				for (int j = 0; j < times; j++) {
					try {
						org.apache.commons.beanutils.BeanUtils.copyProperties(
								destObject2, getSourceObe());
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
				}
				System.out.println("Apache Common BeanUtils cost "
						+ (System.currentTimeMillis() - start2));
				System.out.println(destObject2);
			}

		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				DestinationObject destObject2 = new DestinationObject();
				long start2 = System.currentTimeMillis();
				for (int j = 0; j < times; j++) {
					org.springframework.beans.BeanUtils.copyProperties(
							getSourceObe(), destObject2);
				}
				System.out.println("Spring BeanUtils cost "
						+ (System.currentTimeMillis() - start2));
				System.out.println(destObject2);
			}

		}).start();

	}

	private static SourceObject getSourceObe() {
		int i = 10;
		String str = "this is a 中";
		final SourceObject sourceObject = new SourceObject(i, str);

		DestinationObject innerDestObject = new DestinationObject();
		innerDestObject.setI(100);
		innerDestObject.setStr("innerDestObject");

		sourceObject.setDest(innerDestObject);
		sourceObject.setArray(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
		sourceObject.setListStr(Arrays.asList("1", "2", "1", "2", "1", "2",
				"1", "2"));
		return sourceObject;
	}

}
 

 

结果:

 Spring BeanUtils cost 1941

DestinationObject(i=10, str=this is a 中, dest=DestinationObject(i=100, str=innerDestObject, dest=null, array=null, listStr=null), array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], listStr=[1, 2, 1, 2, 1, 2, 1, 2])

Apache Common BeanUtils cost 17832

DestinationObject(i=10, str=this is a 中, dest=DestinationObject(i=100, str=innerDestObject, dest=null, array=null, listStr=null), array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], listStr=[1, 2, 1, 2, 1, 2, 1, 2])

Mapper cost 58599

DestinationObject(i=10, str=this is a 中, dest=DestinationObject(i=100, str=innerDestObject, dest=null, array=null, listStr=null), array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], listStr=[1, 2, 1, 2, 1, 2, 1, 2])

  Spring BeanUtils 最快而且快的不只一点点。   这里还有一个哥们也做了同样的测试: http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/ 测试代码 http://data.christianschenk.org/java-bean-mapper-performance-tests/xref/index.html   各种测试结果 https://docs.google.com/spreadsheet/ccc?key=0Ag2KaRd-9EF3dGtIa09idXVrNksycURCNEdQQWFzTkE&hl=en_US#gid=0                    

猜你喜欢

转载自k1280000.iteye.com/blog/2162832
今日推荐