spring mvc + hibernate實現實體化類entity到數據傳輸類dto的自動轉換

package com.wxj233.util;

import java.util.List;

/**
 * 完成數據持久化層到傳輸層自動賦值
 * @version 0.0.1
 * @since 2019/01/09
 * @author wxj233
 *
 */
public interface DataTransfer {

	/**
	 * 完成持久化層到傳輸層數據賦值,(反向賦值也可以)
	 * @param dto 傳輸層
	 * @param dao 持久化辰
	 * @return 賦值後的傳輸層
	 */
	Object getDtoFromEntity(Object dto, Object entity);
	
	/**
	 * 集合類映射
	 * @param <T> 目標集合類型
	 * @param <S> 源集合
	 * @return 目標集合(自動賦值)
	 */
	<T, S> List<T> ListToList(List<S> src, Class<T> t);
	
}
package com.wxj233.util;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class DataTransferImp implements DataTransfer {

	@Override
	public Object getDtoFromEntity(Object dto, Object entity) {
		// TODO Auto-generated method stub
		//Field[] fields = dto.getClass().getDeclaredFields();
		
		Method[] dtoMethods = dto.getClass().getMethods();
		Method[] entityMethods = entity.getClass().getMethods();
		
		for(Method dtoMethod : dtoMethods) {
			if(dtoMethod.getName().startsWith("set")) {
				for(Method entityMethod : entityMethods) {
					if(entityMethod.getName().startsWith("get") 
							&& entityMethod.getName().substring(3).equals(dtoMethod.getName().substring(3))) {
						
						try {
							
							if(dtoMethod.getParameterTypes()[0].isAssignableFrom(List.class) ) {
								//System.out.println(dtoMethod.getGenericParameterTypes()[0].toString());
								Type[] paramTypes = dtoMethod.getGenericParameterTypes();
								Type[] actualTypes = ((ParameterizedType)paramTypes[0]).getActualTypeArguments();
								//System.out.println(actualTypes[0].toString());
								dtoMethod.invoke(dto, ListToList((List<?>)entityMethod.invoke(entity),(Class<?>)actualTypes[0]));
							}else {
								//invoke第一個參數是代表調用該方法的對象
								dtoMethod.invoke(dto, entityMethod.invoke(entity));
							}
							
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}
				}
			}
		}
		
		return dto;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T, S> List<T> ListToList(List<S> src, Class<T> tType) {
		// TODO Auto-generated method stub
		List<T> target = new ArrayList<>();
		for(S s : src) {
//					//Method method = DataTransferImp.class.getMethod("ListToList", List.class,Class.class);
//						
//					//獲取參數
//					Type[] paramTypes = method.getGenericParameterTypes();
//					
//					//獲取參數的類型
//					//Type rawType = ((ParameterizedType)paramTypes[1]).getRawType();
//					
//					//獲取參數中泛型,一個參數中泛型可能有多個
//					Type[] actualTypes = ((ParameterizedType)paramTypes[1]).getActualTypeArguments();
//					System.out.println(((T)actualTypes[0]).getClass().toGenericString());
//					T t = (T) actualTypes[0].getClass().getDeclaredConstructor().newInstance();
					
				    
					try {
						
						T t = tType.getDeclaredConstructor().newInstance();
						target.add((T) getDtoFromEntity(t,s));
						
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

		}
		
		return target;
	}

}

DataTransferImp實現DataTransfer接口,一共兩個方法,使用到的原理主要是java反射機制實現賦值,
測試如下:
 

package com.wxj233.util;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.wxj233.entity.OperationRecord;
import com.wxj233.entity.User;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:springmvc-servlet.xml")
@WebAppConfiguration
public class DataTransferImpTest {

	@Autowired
	DataTransfer dataTransfer;
	
	@Test
	public void testGetclass() {
//作者的用戶類,你可以用其他的類來做測試
		User user = new User();
		user.setId(123);
		OperationRecord operationRecord = new OperationRecord();
		operationRecord.setId(456);
		user.getOperationRecords().add(operationRecord);
		
		User usera = new User();
		
		usera = (User) dataTransfer.getDtoFromEntity(usera, user);
		System.out.println(usera.getOperationRecords().get(0).getId());
		
		fail("Not yet implemented");
	}

}

輸出結果爲:456

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章