All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zlyx.easydatabase.interceptors.AbstractResultSetInterceptor Maven / Gradle / Ivy

package com.zlyx.easydatabase.interceptors;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.zlyx.easycore.tool.FormatTool;
import com.zlyx.easycore.utils.ClassUtils;
import com.zlyx.easycore.utils.DateUtils;
import com.zlyx.easycore.utils.LogUtils;
import com.zlyx.easycore.utils.ObjectUtils;
import com.zlyx.easydatabase.DaoMarkerFactory;
import com.zlyx.easydatabase.local.ReturnType;
import com.zlyx.easydatabase.utils.TableUtils;

/**
 * @Auth 赵光
 * @Describle
 * @2019年1月14日 上午9:57:50
 */
public abstract class AbstractResultSetInterceptor {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected List getResults(Class resultType, ResultSet resultSet) throws SQLException {
		ResultSetMetaData rsmd = resultSet.getMetaData();
		List resList = new ArrayList();
		if("JSONObject".equals(resultType.getSimpleName())||"JSON".equals(resultType.getSimpleName())){
			JSONObject jsonObj = null;
			Object value = null;
			while (resultSet.next()) {
				jsonObj = new JSONObject();
				for (int i = 0 ; i < rsmd.getColumnCount();i++) {
					value = resultSet.getObject(i+1);
					if(value != null){
						if(value.getClass().getSimpleName().contains("Date") || value.getClass().getSimpleName().contains("Time")){
							value = DateUtils.format(value);
						}
						jsonObj.put(rsmd.getColumnLabel(i+1).toLowerCase(), value);
					}
					
				}
				resList.add(jsonObj);
			}
		}else {
			Map fieldAndValues = null;
			Object value = null;
			while (resultSet.next()) {
				fieldAndValues = new HashMap<>();
				for(int i = 0 ; i < rsmd.getColumnCount();i++){
					value = resultSet.getObject(i+1);
					if(value != null) {
						fieldAndValues.put(rsmd.getColumnLabel(i+1).toLowerCase(), value);
					}
				}
				resList.add(packageModel(fieldAndValues, resultType));
			}
		} 
		resultSet.close();
		ReturnType.clear();
		return resList;
	}

	/**
     *  数据转实体
     * @param map
     * @param c
     * @return
     */
	public Object packageModel(Map map, Class resultType) {
		boolean notNull = false;
		try {
			List fields = ClassUtils.getMapClass(resultType).getFields();
			Object obj = resultType.getConstructor().newInstance();
			String tableAlias = TableUtils.getAlias(resultType);
			Object value = null;
			for(Field field : fields){
			     if(!Modifier.isStatic(field.getModifiers())) {
				       if (!field.getType().toString().contains("java")) {
						    value = this.packageModel(map, field.getType());
					   } else if(map.containsKey(tableAlias+"_"+field.getName())) {
							value = map.get(tableAlias+"_"+field.getName());
					   } else if(map.containsKey(DaoMarkerFactory.getFieldName(field))) {
						   value = map.get(DaoMarkerFactory.getFieldName(field));
					   } else if(map.containsKey(FormatTool.camel2Underline(field.getName()))) {
						   value = map.get(FormatTool.camel2Underline(field.getName()));
					   } else {
						   value = null;
					   }
			    	   if(ObjectUtils.isNotEmpty(value)) {
					    	if(BigInteger.class == value.getClass() && Long.class == field.getType()){
								value = Long.parseLong(String.valueOf(value));
							}
					    	if(String.class == field.getType()) {
					    		value = String.valueOf(value);
					    	}
					    	if(Integer.class == field.getType()) {
					    		value = Integer.parseInt(String.valueOf(value));
					    	}
					    	field.setAccessible(true);
					    	field.set(obj, value);
					    	notNull = true;
					   }
			    }
			    
			}
			if(notNull) {
				return obj;
			}
		} catch (Exception e) {
			LogUtils.err(e);
		}
		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy