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

com.j2mvc.framework.dao.callback.InvokeObject Maven / Gradle / Ivy

Go to download

强烈建议使用J2mvc 2.1以后的版本。 version 2.1.01 1.更换JSON依赖包. version 2.1.02 1.移除com.j2mvc.StringUtils.getUtf8()方法调用. 更改为getCharset() version 2.1.03 1.更新JNDI连接设置 version 2.1.04 1.修改works.xml配置url-pkg-prefixes改为pkg

There is a newer version: 2.1.12
Show newest version
package com.j2mvc.framework.dao.callback;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import com.j2mvc.framework.dao.DaoSupport;
import com.j2mvc.util.mapping.Column;
import com.j2mvc.util.mapping.DataSourceName;
import com.j2mvc.util.mapping.Foreign;
import com.j2mvc.util.mapping.Sql;
import com.j2mvc.util.FieldUtil;


public class InvokeObject {

	static Logger log = Logger.getLogger(InvokeObject.class.getName());
	/**
	 * 写入字段数组值
	 * @param fields
	 * @param obj
	 * @param rs
	 */
	public static void invoke(List fields,Object obj,ResultSet rs,String dataSourceName){
		for(int i=0;i type = field.getType();	
		try {
			Object value = null;
			if(foreign!=null){
				// 外键对象之主键属性数据类型
				type = FieldUtil.getForeignKey(type).getType();
			}
			if(String.class.isAssignableFrom(type))
				value = rs.getString(column.name());				
			else if(Integer.class.isAssignableFrom(type) 
					|| Short.class.isAssignableFrom(type)
					|| short.class.isAssignableFrom(type)
					|| int.class.isAssignableFrom(type))
				value = rs.getInt(column.name());
			else if(Long.class.isAssignableFrom(type) 
					|| long.class.isAssignableFrom(type))
				value = rs.getLong(column.name());
			else if(Float.class.isAssignableFrom(type)
					|| float.class.isAssignableFrom(type))
				value = rs.getFloat(column.name());
			else if(Double.class.isAssignableFrom(type)
					|| double.class.isAssignableFrom(type))
				value = rs.getDouble(column.name());
			else if(Boolean.class.isAssignableFrom(type)
					|| boolean.class.isAssignableFrom(type))
				value = rs.getBoolean(column.name());
			else if(Time.class.isAssignableFrom(type))
				value = rs.getTime(column.name());
			else if(Date.class.isAssignableFrom(type)){
				value = rs.getDate(column.name());
			}else if(byte[].class.isAssignableFrom(type)){
				value = rs.getBytes(column.name());
			}else if(Collection.class.isAssignableFrom(type)
					|| String[].class.isAssignableFrom(type)){
				byte[] bytes = rs.getBytes(column.name());
				if(bytes!=null)
					value = StreamUtil.bytesToObject(bytes);
			}		
			if(foreign!=null){
				// 外键,字段为对象,获取类主键,并实例
				try {
					Class fieldClass =  field.getType();
					DataSourceName dsn = fieldClass.getAnnotation(DataSourceName.class);	
					DaoSupport daoSupport = null;
					if(dsn!=null){
						daoSupport = new DaoSupport(fieldClass,dsn.value());
					}else if(dataSourceName!=null && !dataSourceName.equals("")){
						daoSupport = new DaoSupport(fieldClass,dataSourceName);
					}else {
						daoSupport = new DaoSupport(fieldClass);
					}
					value = daoSupport.get(value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else if(sql!=null){
				// 根据SQL语句查询值
				try {
					DaoSupport daoSupport = new DaoSupport(field.getType());
					value = daoSupport.get(sql.value());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			// 执行写入操作
			PropertyDescriptor pd = new PropertyDescriptor(name, obj.getClass());
			Method wm = pd.getWriteMethod();
			if(value!=null)
				wm.invoke(obj, value);			
		} catch (IntrospectionException e) {
			e.printStackTrace();
		}catch (IllegalArgumentException e) {
			e.printStackTrace();
		}catch (IllegalAccessException e) {
			e.printStackTrace();
		}catch (InvocationTargetException e) {
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		} 
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy