CustomJdbcTemplateRowMapper.java 3.58 KB
package com.taover.repository.mapper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSetMetaData;

public class CustomJdbcTemplateRowMapper <E> implements RowMapper<E>{
	Log log = LogFactory.getLog(this.getClass());
	
	private Map<String, Field> beanFielNameToField = new HashMap<String, Field>();
	private Method beforeMethod = null;
	private Method afterMethod = null;
	
	private Class<E> classInfo;
	private Map<String, String> tableToBeanMap;
		
	public CustomJdbcTemplateRowMapper(Class<E> classInfo, Map<String, String> tableToBeanMap) {
		this.classInfo = classInfo;
		this.tableToBeanMap = tableToBeanMap;
		
		Field[] fields = this.classInfo.getDeclaredFields();
		for(Field item: fields) {
			try {
				item.setAccessible(true);
				this.beanFielNameToField.put(item.getName(), item);	
			}catch (Exception e) {
				log.error("set field accessible:"+e.getMessage());
			}
		}
		
		boolean hasImplementPointCut = false;
		Class[] interfaceArr = this.classInfo.getInterfaces();
		for(int i=0; i<interfaceArr.length; ++i){
			if(interfaceArr[i].getName().equals("com.taover.repository.advice.EntityPointCut")){
				hasImplementPointCut = true;
				break;
			}
		}
		
		if(hasImplementPointCut){
			try{
				beforeMethod = this.classInfo.getDeclaredMethod("before");
				beforeMethod.setAccessible(true);
			}catch(Exception e){
				log.error("set before method exception:"+e.getMessage());
			}
			
			try{
				afterMethod = this.classInfo.getDeclaredMethod("after");
				afterMethod.setAccessible(true);
			}catch(Exception e){
				log.error("set after method exception:"+e.getMessage());
			}
		}
	}

	@Override
	public E mapRow(ResultSet rs, int index) throws SQLException {		
		E targetObj;
		try {
			targetObj = this.classInfo.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		if(this.beforeMethod != null){
			try{
				beforeMethod.invoke(targetObj);
			}catch(Exception e){ 
				log.error("invoke before exception:"+e.getMessage());
			}
		}
		
		ResultSetWrappingSqlRowSetMetaData wapping = new ResultSetWrappingSqlRowSetMetaData(rs.getMetaData());
		int columnCount = wapping.getColumnCount();
		for (int i = 1; i<=columnCount; i++) {
			String tableFieldName = wapping.getColumnLabel(i);
			String beanFieldName = this.tableToBeanMap.get(tableFieldName);
			Object value = rs.getObject(i);
			Field beanField = this.beanFielNameToField.get(beanFieldName);
			if(null == value || beanFieldName == null && beanField == null){
				continue;					
			}
			try {
				beanField.set(targetObj, value);
			} catch (IllegalArgumentException e) {
				if("Integer".equals(value.getClass().getSimpleName()) && "Long".equals(beanField.getType().getSimpleName())) {
					try {
						beanField.set(targetObj, Long.valueOf(value.toString()));
					} catch (IllegalArgumentException | IllegalAccessException e1) {
						log.error("map set object field error -> Integer convert Long:"+e.getMessage());
					}
				}else {
					log.error("map set object field error:"+e.getMessage());
				}				
			} catch (IllegalAccessException e) {
				log.error("map set object field error:"+e.getMessage());
			}
		}
		
		if(this.afterMethod != null){
			try {
				afterMethod.invoke(targetObj);
			}catch (Exception e) {
				log.error("invoke after exception:"+e.getMessage());
			}
		}
		
		return targetObj;
	}
}