/** * * 功能: 实例化一个指定的实体,(调用该方法以前,必须调用rs.next()) * 作者: jiangfuqiang * 创建日期:2014-1-10 * 修改者: mender * 修改日期: modifydate * @param clazz * @param rs * @return * @throws SQLException * @throws IllegalAccessException * @throws InstantiationException */ protected Object getObjectFromResult(Class clazz, ResultSet rs) throws SQLException{ try { Object obj = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); int index = 0; for(int i = 0; i < columnCount; i++) { String columnName = rsmd.getColumnName(i+1); for(Field field : fields) { String name = field.getName(); String newColumnName = columnName; while(((index = newColumnName.indexOf("_")) > 0) && index < newColumnName.length()) { //两个条件排除了下划线是第一个和最后一个字符的可能性 newColumnName = newColumnName.substring(0, index) + Character.toUpperCase(newColumnName.charAt(index+1)) + newColumnName.substring(index+2); } if(name.equals(newColumnName)) { //如果属性相同 Class clType = field.getType(); String typeName = clType.getName(); newColumnName = "set" + Character.toUpperCase(newColumnName.charAt(0)) + newColumnName.substring(1); if(typeName.equals("int")) { int value = rs.getInt(columnName); Method method = clazz.getMethod(newColumnName, Integer.class); method.invoke(obj, value); break; } else if(typeName.equals("double")) { double value = rs.getDouble(columnName); Method method = clazz.getMethod(newColumnName, Double.class); method.invoke(obj, value); break; } else if(typeName.equals("short")) { short value = rs.getShort(columnName); Method method = clazz.getMethod(newColumnName, Short.class); method.invoke(obj, value); break; } else if(typeName.equals("float")) { float value = rs.getFloat(columnName); Method method = clazz.getMethod(newColumnName, Float.class); method.invoke(obj, value); break; } else if(typeName.equals("byte")) { byte value = rs.getByte(columnName); Method method = clazz.getMethod(newColumnName, Byte.class); method.invoke(obj, value); break; } else if(typeName.equals("java.lang.String")) { String value = rs.getString(columnName); Method method = clazz.getMethod(newColumnName, String.class); method.invoke(obj, value); break; } else if(typeName.equals("java.lang.Integer")) { Integer value = rs.getInt(columnName); Method method = clazz.getMethod(newColumnName, Integer.class); method.invoke(obj, value); break; } else if(typeName.equals("java.lang.Double")) { Double value = rs.getDouble(columnName); Method method = clazz.getMethod(newColumnName, Double.class); method.invoke(obj, value); break; } else if(typeName.equals("java.lang.Long")) { Long value = rs.getLong(columnName); Method method = clazz.getMethod(newColumnName, Long.class); method.invoke(obj, value); break; } else if(typeName.equals("long")) { long value = rs.getLong(columnName); Method method = clazz.getMethod(newColumnName, Long.class); method.invoke(obj, value); break; } else if(typeName.equals("java.math.BigDecimal")) { BigDecimal value = rs.getBigDecimal(columnName); Method method = clazz.getMethod(newColumnName, BigDecimal.class); method.invoke(obj, value); break; } else if(typeName.equals("java.util.Date")){ Date value = rs.getDate(columnName); Method method = clazz.getMethod(newColumnName, Date.class); method.invoke(obj, value); break; } else if(typeName.equals("java.sql.Timestamp")) { Timestamp value = rs.getTimestamp(columnName); Method method = clazz.getMethod(newColumnName, Timestamp.class); method.invoke(obj, value); break; } } } } return obj; } catch (InstantiationException e) { log.error("实例化实体"+ clazz.getName()+"出错",e); } catch (IllegalAccessException e) { log.error("无法访问实体" + clazz.getName(),e); } catch (NoSuchMethodException e) { log.error("反射实体" + clazz.getName()+"出错",e); } catch (SecurityException e) { log.error("有不可访问的反射实体属性或方法" + clazz.getName()+"出错",e); } catch (IllegalArgumentException e) { log.error("反射给实体方法赋值出错:" + clazz.getName(),e); } catch (InvocationTargetException e) { log.error("反射出错",e); } return null; } /** * * 功能: 获取实体的集合(调用该方法以前,不要调用rs.next(),直接传参即可) * 作者: jiangfuqiang * 创建日期:2014-1-10 * 修改者: mender * 修改日期: modifydate * @param clazz * @param rs * @return * @throws SQLException */ protected List getListFromResult(Class clazz, ResultSet rs) throws SQLException { List objs = new ArrayList(20); while(rs.next()) { objs.add(getObjectFromResult(clazz, rs)); } return objs; }
java 类反射给属性赋值_java 反射实现实体类的属性赋值
最新推荐文章于 2024-05-13 10:35:17 发布