Android Sqlite 与 dao

来源:互联网 发布:越南南海知乎 编辑:程序博客网 时间:2024/06/07 01:52
 public static <T> List<T> readAllTrans(T t, DBManager dbHelper, String tableName) {

        dbHelper.openDatabase();
        SQLiteDatabase db = dbHelper.getDatabase();
        Cursor cursor = db.query(tableName, null, null, null, null, null, null);
        List<T> dataList = new ArrayList<T>();
        
        //表中各项名称
        List<String> columnNames = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
        columnNames.remove("id");
        //对表中各项进行排序,反射得到的set方法是按升序排列,便于匹配
        Collections.sort(columnNames);
        System.out.println("columnNames2" + columnNames.size());                                
        
        try {
            Class<? extends Object> clazz = t.getClass();            
            Method[] declaredMethods = clazz.getDeclaredMethods();
            List<Method> allSetMethods = new ArrayList<Method>();
            List<Method> setMethods = new ArrayList<Method>();                    
            
            //获取所有set方法
            for(int i = 0; i < declaredMethods.length; i++){
                String methodName = declaredMethods[i].getName();
                if(methodName.startsWith("set") ){                    
                    allSetMethods.add(declaredMethods[i]);                    
                    
                }
            }
            //获取与数据库ColumnName相符的set方法                            
            for(int i = 0; i < columnNames.size(); i++){                
                for(int j = i; j < allSetMethods.size(); j++){
                    String methodName = allSetMethods.get(j).getName();
                    if( columnNames.get(i).equalsIgnoreCase(methodName.substring(3, methodName.length()))){                        
                        setMethods.add(allSetMethods.get(j));
                        continue;
                    }
                }
            }
            System.out.println("columnNames.size() : " + columnNames.size() + "setMethods.size() : " + setMethods.size());                                
            if (cursor != null && cursor.moveToFirst()) {  
                do {
               
                    t = (T) clazz.newInstance();
                    for(int i = 0; i < setMethods.size(); i++ ){
                        Method method = setMethods.get(i);                        
                        Class<?>[] parameterTypes = method.getParameterTypes();
                        int columnIndex = cursor.getColumnIndex(columnNames.get(i));                        
                      if (parameterTypes[0] == String.class) {         
                         method.invoke(t,cursor.getString(columnIndex));
                     }else if (parameterTypes[0] == int.class) {
                         method.invoke(t, cursor.getInt(columnIndex));
                     }else if (parameterTypes[0] == long.class) {
                         method.invoke(t, cursor.getLong(columnIndex));
                     }else if (parameterTypes[0] == byte.class) {                             
                         method.invoke(t, (byte)cursor.getInt(columnIndex));
                     }else if (parameterTypes[0] == boolean.class) {                            
                         method.invoke(t, cursor.getInt(columnIndex) == 0 ? false:true);                         
                     }else if(parameterTypes[0] == byte[].class){                        
                         method.invoke(t, cursor.getBlob(columnIndex));
                        
                      }                                                            
                                                            
                    }    
                    dataList.add(t);                    
                }while (cursor.moveToNext());
            
            }            
            
        } catch (InstantiationException e ) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        dbHelper.closeDatabase();
        return dataList;
        
        }
0 0