利用Dozer實現vo-Entiy-Model中屬性的複用

1.背景描述

        將entity中的屬性,賦值給model,或者是vo.這樣如果用之前的一個個的**entity.setId(**model.getId)這樣的方法,將各個屬性賦值會特別麻煩.所以封裝了BeanMapperUtil這個類.一共兩個方法,一個方法是map(返回entity),一個是mapList(返回List<entity>).


2.代碼實現

package com.dmsdbj.itoo.examinationEvaluation.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.dozer.DozerBeanMapper;

/**
  * Created by chenxiaochan on 2017/7/17.
  */

public class BeanMapperUtil {
	private static DozerBeanMapper dozer = new DozerBeanMapper();

	public static <T> T map(Object sourceObject, Class<T> destObjectclazz) {
		return sourceObject == null ? null : dozer.map(sourceObject, destObjectclazz);
	}

	public static <T, S> List<T> mapList(Collection<S> sourceList, Class<T> destObjectclazz) {
		if (sourceList == null) {
			return null;
		}
		List<T> destinationList = new ArrayList<T>();
		for (Iterator<S> it = sourceList.iterator(); it.hasNext();) {
			destinationList.add(map(it.next(), destObjectclazz));
		}
		return destinationList;
	}
}

需要在pom文件中添加的jar包

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



發佈了153 篇原創文章 · 獲贊 588 · 訪問量 75萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章