处理JdbcTemplate放回的结果集

ResultSet中结果反射

 

import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class ResultToBean {
	
	/**
	 * 将ResultSet内容映射到实体 须有 ColToProperty注解
	 * @param rs
	 * @param clazz
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static <T> List<T> getBean(ResultSet rs, Class<T> clazz)
			throws SecurityException, NoSuchMethodException,
			InstantiationException, IllegalAccessException, SQLException,
			IllegalArgumentException, InvocationTargetException {
		List<T> l = new ArrayList<T>();
		Method method = null;
		T obj = null;
		while (rs.next()) {
			obj = clazz.newInstance();
			Field fs[] = clazz.getDeclaredFields();
			int count = 1;
			for (Field f : fs) {
				ColToProperty colToProperty = f
						.getAnnotation(ColToProperty.class);
				if (null == colToProperty) {
					continue;
				}
				String m = f.getName().substring(0, 1).toUpperCase()
						+ f.getName().substring(1);
				if (f.getType() == java.lang.Boolean.class) {
					method = clazz.getDeclaredMethod("set" + m, Boolean.class);
					method.invoke(obj, rs.getBoolean(count));
				} else if (f.getType() == boolean.class) {
					method = clazz.getDeclaredMethod("set" + m, boolean.class);
					method.invoke(obj, rs.getBoolean(count));
				} else if (f.getType() == java.util.Date.class
						|| f.getType() == Date.class) {
					method = clazz.getDeclaredMethod("set" + m, Date.class);
					method.invoke(obj, rs.getDate(count));
				} else if (f.getType() == java.lang.Double.class) {
					method = clazz.getDeclaredMethod("set" + m, Double.class);
					method.invoke(obj, rs.getDouble(count));
				} else if (f.getType() == Double.class) {
					method = clazz.getDeclaredMethod("set" + m, double.class);
					method.invoke(obj, rs.getDouble(count));
				} else if (f.getType() == java.lang.Float.class) {
					method = clazz.getDeclaredMethod("set" + m, Float.class);
					method.invoke(obj, rs.getFloat(count));
				} else if (f.getType() == Float.class) {
					method = clazz.getDeclaredMethod("set" + m, float.class);
					method.invoke(obj, rs.getFloat(count));
				} else if (f.getType() == java.lang.Integer.class) {
					method = clazz.getDeclaredMethod("set" + m, Integer.class);
					method.invoke(obj, rs.getInt(count));
				} else if (f.getType() == int.class) {
					method = clazz.getDeclaredMethod("set" + m, int.class);
					method.invoke(obj, rs.getInt(count));
				} else if (f.getType() == java.lang.Short.class
						|| f.getType() == short.class) {
					method = clazz.getDeclaredMethod("set" + m, Short.class);
					method.invoke(obj, rs.getShort(count));
				} else if (f.getType() == java.lang.String.class) {
					method = clazz.getDeclaredMethod("set" + m, String.class);
					method.invoke(obj, rs.getString(count));
				} else if (f.getType() == java.lang.Long.class
						|| f.getType() == long.class) {
					method = clazz.getDeclaredMethod("set" + m, Long.class);
					method.invoke(obj, rs.getLong(count));
				} else {
					throw new RuntimeException("非基础数据类型,无法正确匹配,请检查ColToProperty是否使用正确!");
				}
				count++;
			}
			l.add(obj);
		}
		return l;
	}
}

 

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface ColToProperty {

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值