ResultSet转java对象的通用解决方案
2011-08-04 19:22 hanwesley 阅读(1310) 评论(0) 编辑 收藏 举报根据resultSet,以及给定对象的Class对象进行填充,Class需要遵循JavaBean规范。
- private Collection mergeObject(ResultSet rs, Class clazz) throws Exception {
- ResultSetMetaData rmd = (ResultSetMetaData) rs.getMetaData();
- Collection coll = new ArrayList();
- ;
- while (rs.next()) {
- Object obj = clazz.newInstance();
- for (int i = 1; i <= rmd.getColumnCount(); i++) {
- beanRegister(obj, rmd.getColumnName(i), rs.getString(i));//根据列名和对应值对对象属性进行赋值
- }
- coll.add(obj);
- }
- return coll;
- }
根据get方法的返回值类型,判断当前属性的类型,调用method.invoke(Object,param);
- private void beanRegister(Object object, String beanProperty, String value) {
- Object[] beanObject = beanMatch(object.getClass(), beanProperty);
- Object[] cache = new Object[1];
- Method getter = (Method) beanObject[0];
- Method setter = (Method) beanObject[1];
- try {
- // 通过get获得方法类型
- String methodType = getter.getReturnType().getName();
- if ((methodType.equalsIgnoreCase("long") || methodType.equalsIgnoreCase("java.lang.Long"))
- && !"".equals(value) && value != null) {
- cache[0] = new Long(value);
- setter.invoke(object, cache);
- } else if ((methodType.equalsIgnoreCase("int") || methodType.equalsIgnoreCase("java.lang.Integer"))
- && !"".equals(value) && value != null) {
- cache[0] = new Integer(value);
- setter.invoke(object, cache);
- } else if ((methodType.equalsIgnoreCase("short") || methodType.equalsIgnoreCase("java.lang.Short"))
- && !"".equals(value) && value != null) {
- cache[0] = new Short(value);
- setter.invoke(object, cache);
- } else if ((methodType.equalsIgnoreCase("float") || methodType.equalsIgnoreCase("java.lang.Float"))
- && !"".equals(value) && value != null) {
- cache[0] = new Float(value);
- setter.invoke(object, cache);
- } else if ((methodType.equalsIgnoreCase("double") || methodType.equalsIgnoreCase("java.lang.Double"))
- && !"".equals(value) && value != null) {
- cache[0] = new Double(value);
- setter.invoke(object, cache);
- } else if ((methodType.equalsIgnoreCase("boolean") || methodType.equalsIgnoreCase("java.lang.Boolean"))
- && !"".equals(value) && value != null) {
- cache[0] = new Boolean(value);
- setter.invoke(object, cache);
- } else if ((methodType.equalsIgnoreCase("java.lang.String")) && !"".equals(value) && value != null) {
- cache[0] = value;
- setter.invoke(object, cache);
- } else if (methodType.equalsIgnoreCase("java.io.InputStream")) {
- } else if (methodType.equalsIgnoreCase("char")) {
- cache[0] = (Character.valueOf(value.charAt(0)));
- setter.invoke(object, cache);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private Object[] beanMatch(Class clazz, String beanProperty) {
- Object[] result = new Object[2];
- char beanPropertyChars[] = beanProperty.toCharArray();
- beanPropertyChars[0] = Character.toUpperCase(beanPropertyChars[0]);
- String s = new String(beanPropertyChars);
- String names[] = { ("set" + s).intern(), ("get" + s).intern(), ("is" + s).intern(), ("write" + s).intern(),
- ("read" + s).intern() };
- Method getter = null;
- Method setter = null;
- Method methods[] = clazz.getMethods();
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- if (!Modifier.isPublic(method.getModifiers()))
- continue;
- String methodName = method.getName().intern();
- for (int j = 0; j < names.length; j++) {
- String name = names[j];
- if (!name.equals(methodName))
- continue;
- if (methodName.startsWith("set") || methodName.startsWith("read"))
- setter = method;
- else
- getter = method;
- }
- }
- result[0] = getter;
- result[1] = setter;
- return result;
- }