Android基于Java反射机制的简单ORM-Dao层
来源:互联网 发布:eclipse php 编辑:程序博客网 时间:2024/06/05 19:46
无需配置,但需要按照一定的规则写POJO。规则:①基础类型只能使用short,int,long,float,double,String。②表名需与类名相同。③表的属性名需与类成员变量名相同。④每个类都需要有一个基础类型的id成员变量。⑤对象之间可以嵌套存取,但不要出现循环嵌套循环嵌套如下: public class Fruit { private Apple apple; } public class Apple { private Fruit fruit; }⑥成员变量中可以为 基础类型 已成表的对象类型 已成表的对象类型的容器(只支持ArrayList)//存储对象,对象已存在抛出异常public void save(Object object, SQLiteDatabase db) throws Exception { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); ContentValues contentValues = new ContentValues(); Object id = null; for(Field field : fields) { if(field.getName().equals("id")) { String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); id = method.invoke(object); break; } } try { if (id == null) throw new Exception(); } catch (Exception e) { e.printStackTrace(); } for(Field field : fields) { Class filedClazz = field.getType(); String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); Object filedObject = method.invoke(object); if(filedObject == null) continue; if(isBaseDataType(filedClazz)) { Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedObject); } else if(Collection.class.isAssignableFrom(filedClazz)) { Collection collection = (Collection) filedObject; Iterator iterator = collection.iterator(); while(iterator.hasNext()) { ContentValues contentValues2 = new ContentValues(); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(id.getClass())); putMethod.invoke(contentValues2, getSimpleName(clazz.getName()), id); Object object2 = iterator.next(); saveOrUpdate(object2, db, contentValues2); } } else { String filedMethodName = "getId"; Method filedMethod = filedClazz.getMethod(filedMethodName); Object filedFiledObject = filedMethod.invoke(filedObject); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedFiledObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedFiledObject); saveOrUpdate(filedObject, db); } } String tableName = getSimpleName(clazz.getName()); Cursor cursor = db.query(tableName ,null,"id=?", new String[] {String.valueOf(contentValues.get("id"))}, null, null, null); if(!cursor.moveToFirst()) db.insert(tableName, null, contentValues); cursor.close();}//保存对象,对象已存在则更新对象public void saveOrUpdate(Object object, SQLiteDatabase db) throws Exception { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); ContentValues contentValues = new ContentValues(); Object id = null; for(Field field : fields) { if(field.getName().equals("id")) { String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); id = method.invoke(object); break; } } try { if (id == null) throw new Exception(); } catch (Exception e) { e.printStackTrace(); } for(Field field : fields) { Class filedClazz = field.getType(); String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); Object filedObject = method.invoke(object); if(filedObject == null) continue; if(isBaseDataType(filedClazz)) { Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedObject); } else if(Collection.class.isAssignableFrom(filedClazz)) { Collection collection = (Collection) filedObject; Iterator iterator = collection.iterator(); while(iterator.hasNext()) { ContentValues contentValues2 = new ContentValues(); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(id.getClass())); putMethod.invoke(contentValues2, getSimpleName(clazz.getName()), id); Object object2 = iterator.next(); saveOrUpdate(object2, db, contentValues2); } } else { String filedMethodName = "getId"; Method filedMethod = filedClazz.getMethod(filedMethodName); Object filedFiledObject = filedMethod.invoke(filedObject); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedFiledObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedFiledObject); saveOrUpdate(filedObject, db); } } String tableName = getSimpleName(clazz.getName()); Cursor cursor = db.query(tableName ,null,"id=?", new String[] {String.valueOf(contentValues.get("id"))}, null, null, null); if(!cursor.moveToFirst()) db.insert(tableName, null, contentValues); else db.update(tableName, contentValues, "id=?", new String[] {String.valueOf(contentValues.get("id"))}); cursor.close();}//配合上一个方法使用public void saveOrUpdate(Object object, SQLiteDatabase db, ContentValues contentValues) throws Exception { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); Object id = null; for(Field field : fields) { if(field.getName().equals("id")) { String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); id = method.invoke(object); break; } } try { if (id == null) throw new Exception(); } catch (Exception e) { e.printStackTrace(); } for(Field field : fields) { Class filedClazz = field.getType(); String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); Object filedObject = method.invoke(object); if(filedObject == null) continue; if(isBaseDataType(filedClazz)) { Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedObject); } else if(Collection.class.isAssignableFrom(filedClazz)) { Collection collection = (Collection) filedObject; Iterator iterator = collection.iterator(); while(iterator.hasNext()) { ContentValues contentValues2 = new ContentValues(); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(id.getClass())); putMethod.invoke(contentValues2, getSimpleName(clazz.getName()), id); Object object2 = iterator.next(); saveOrUpdate(object2, db, contentValues2); } } else { String filedMethodName = "getId"; Method filedMethod = filedClazz.getMethod(filedMethodName); Object filedFiledObject = filedMethod.invoke(filedObject); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedFiledObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedFiledObject); saveOrUpdate(filedObject, db); } } String tableName = getSimpleName(clazz.getName()); Cursor cursor = db.query(tableName ,null,"id=?", new String[] {String.valueOf(contentValues.get("id"))}, null, null, null); if(!cursor.moveToFirst()) db.insert(tableName, null, contentValues); else db.update(tableName, contentValues, "id=?", new String[] {String.valueOf(contentValues.get("id"))}); cursor.close();}//更新对象,对象不存在则抛出异常public void update(Object object, SQLiteDatabase db) throws Exception { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); ContentValues contentValues = new ContentValues(); Object id = null; for(Field field : fields) { if(field.getName().equals("id")) { String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); id = method.invoke(object); break; } } try { if (id == null) throw new Exception(); } catch (Exception e) { e.printStackTrace(); } for(Field field : fields) { Class filedClazz = field.getType(); String methodName = createGetMethodName(field.getName()); Method method = clazz.getMethod(methodName); Object filedObject = method.invoke(object); if(filedObject == null) continue; if(isBaseDataType(filedClazz)) { Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedObject); } else if(Collection.class.isAssignableFrom(filedClazz)) { Collection collection = (Collection) filedObject; Iterator iterator = collection.iterator(); while(iterator.hasNext()) { ContentValues contentValues2 = new ContentValues(); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(id.getClass())); putMethod.invoke(contentValues2, getSimpleName(clazz.getName()), id); Object object2 = iterator.next(); saveOrUpdate(object2, db, contentValues2); } } else { String filedMethodName = "getId"; Method filedMethod = filedClazz.getMethod(filedMethodName); Object filedFiledObject = filedMethod.invoke(filedObject); Method putMethod = ContentValues.class.getMethod("put", String.class, toObjectClass(filedFiledObject.getClass())); putMethod.invoke(contentValues, field.getName(), filedFiledObject); saveOrUpdate(filedObject, db); } } String tableName = getSimpleName(clazz.getName()); Cursor cursor = db.query(tableName ,null,"id=?", new String[] {String.valueOf(contentValues.get("id"))}, null, null, null); if(cursor.moveToFirst()) db.update(tableName, contentValues, "id=?", new String[] {String.valueOf(contentValues.get("id"))}); else throw new Exception(); cursor.close();}//根据id取对象public Object get(Class clazz, Object id, SQLiteDatabase db) throws Exception { Cursor cursor = db.rawQuery("select * from " + getSimpleName(clazz.getName()) + " where id=?", new String[]{String.valueOf(id)}); if(cursor.moveToFirst()) { Constructor constructor = clazz.getConstructor(); Object object = constructor.newInstance(); Field[] fields = clazz.getDeclaredFields(); for(Field field : fields) { Log.d("sss", field.getName()); String methodName = createSetMethodName(field.getName()); Method method = clazz.getMethod(methodName, field.getType()); Class cs2 = field.getType(); Class cursorType = null; if(isBaseDataType(field.getType())) { cursorType = field.getType(); String cusorTypeName = getSimpleName(toBaseClass(cursorType).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); Object cursorResult = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); method.invoke(object, cursorResult); } else if(Collection.class.isAssignableFrom(field.getType())) { String name = getSingleName(field.getName()); Collection collection = new ArrayList(); collection.addAll(list(Class.forName(getClassName(name)), db, "select * from " + name + " where " + getSimpleName(clazz.getName()) + "=?", new String[]{String.valueOf(id)})); method.invoke(object, collection); } else { cursorType = field.getType().getDeclaredField("id").getType(); String cusorTypeName = getSimpleName(toBaseClass(cursorType).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); Object cursorResult = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); method.invoke(object, get(field.getType(), cursorResult, db)); } } cursor.close(); return object; } cursor.close(); return null;}//根据sql语句取对象public Object get(Class clazz, SQLiteDatabase db, String sql, String... strings) throws Exception { Cursor cursor = db.rawQuery(sql, strings); if(cursor.moveToFirst()) { Constructor constructor = clazz.getConstructor(); Object object = constructor.newInstance(); Field[] fields = clazz.getDeclaredFields(); Object id = null; for(Field field : fields) { if(field.getName().equals("id")) { String cusorTypeName = getSimpleName(toBaseClass(field.getType()).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); id = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); break; } } for(Field field : fields) { Log.d("sss", field.getName()); String methodName = createSetMethodName(field.getName()); Method method = clazz.getMethod(methodName, field.getType()); Class cursorType = null; if(isBaseDataType(field.getType())) { cursorType = field.getType(); String cusorTypeName = getSimpleName(toBaseClass(cursorType).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); Object cursorResult = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); method.invoke(object, cursorResult); } else if(Collection.class.isAssignableFrom(field.getClass())) { String name = getSingleName(field.getName()); Collection collection = new ArrayList(); collection.addAll(list(Class.forName(getClassName(name)), db, "select * from " + name + " where " + getSimpleName(clazz.getName()) + "=?", new String[]{String.valueOf(id)})); method.invoke(object, collection); } else { cursorType = field.getType().getDeclaredField("id").getType(); String cusorTypeName = getSimpleName(toBaseClass(cursorType).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); Object cursorResult = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); method.invoke(object, get(field.getType(), cursorResult, db)); } } cursor.close(); return object; } cursor.close(); return null;}//根据sql取多个对象public List list(Class clazz, SQLiteDatabase db, String sql, String... strings) throws Exception { Cursor cursor = db.rawQuery(sql, strings); List list = new ArrayList(); while(cursor.moveToNext()) { Constructor constructor = clazz.getConstructor(); Object object = constructor.newInstance(); Field[] fields = clazz.getDeclaredFields(); Object id = null; for(Field field : fields) { if(field.getName().equals("id")) { String cusorTypeName = getSimpleName(toBaseClass(field.getType()).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); id = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); break; } } for(Field field : fields) { Log.d("sss", field.getName()); String methodName = createSetMethodName(field.getName()); Method method = clazz.getMethod(methodName, field.getType()); Class cursorType = null; if(isBaseDataType(field.getType())) { cursorType = field.getType(); String cusorTypeName = getSimpleName(toBaseClass(cursorType).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); Object cursorResult = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); method.invoke(object, cursorResult); } else if(Collection.class.isAssignableFrom(field.getClass())) { String name = getSingleName(field.getName()); Collection collection = new ArrayList(); collection.addAll(list(Class.forName(getClassName(name)), db, "select * from " + name + " where " + getSimpleName(clazz.getName()) + "=?", new String[]{String.valueOf(id)})); method.invoke(object, collection); } else { cursorType = field.getType().getDeclaredField("id").getType(); String cusorTypeName = getSimpleName(toBaseClass(cursorType).toString()); String cursorMethodName = createGetMethodName(cusorTypeName); Method cursorMethod = Cursor.class.getMethod(cursorMethodName, int.class); Object cursorResult = cursorMethod.invoke(cursor, cursor.getColumnIndex(field.getName())); method.invoke(object, get(field.getType(), cursorResult, db)); } } list.add(object); } cursor.close(); return list;}private String getClassName(String name) { char[] chars = name.toCharArray(); if(Character.isLowerCase(chars[0])) chars[0] -= 32; name = "com.example.administrator.share.model." + String.valueOf(chars); return name;}private String getSingleName(String name) { return name.substring(0, name.length() - 1);}private String createGetMethodName(String name) { String methodName = "get" + name; char[] chars = methodName.toCharArray(); if(Character.isLowerCase(chars[3])) chars[3] -= 32; return String.valueOf(chars);}private String createSetMethodName(String name) { String methodName = "set" + name; char[] chars = methodName.toCharArray(); if(Character.isLowerCase(chars[3])) chars[3] -= 32; return String.valueOf(chars);}private String getSimpleName(String name) { String[] strings = name.split("\\."); return strings[strings.length - 1];}private Class toBaseClass(Class clazz) { if(clazz.equals(Integer.class)) return int.class; if(clazz.equals(Short.class)) return short.class; if(clazz.equals(Byte.class)) return byte.class; if(clazz.equals(Long.class)) return long.class; if(clazz.equals(Double.class)) return double.class; if(clazz.equals(Float.class)) return float.class; if(clazz.equals(Character.class)) return char.class; if(clazz.equals(Boolean.class)) return boolean.class; return clazz;}private Class toObjectClass(Class clazz) { if(clazz.equals(int.class)) return Integer.class; if(clazz.equals(short.class)) return Short.class; if(clazz.equals(byte.class)) return Byte.class; if(clazz.equals(long.class)) return Long.class; if(clazz.equals(double.class)) return Double.class; if(clazz.equals(float.class)) return Float.class; if(clazz.equals(char.class)) return Character.class; if(clazz.equals(boolean.class)) return Boolean.class; return clazz;}private boolean isBaseDataType(Class clazz) { return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(Boolean.class) || clazz.isPrimitive());}
阅读全文
0 0
- Android基于Java反射机制的简单ORM-Dao层
- Java反射机制在DAO层的应用实例
- java反射机制编写简单万能DAO类
- 反射机制简化Dao层开发
- 简单的Java反射机制
- 简单的Java反射机制
- Java反射机制在dao类的应用
- 利用java的反射机制实现通用dao
- JAVA反射技术实现简单的JDBCUtil(ORM)
- java反射机制的简单示例(android中)
- 基于Java反射实现简易ORM
- Web基础之反射机制优化JDBC中DAO层对象的封装
- 泛型,反射,公共dao层的使用
- 基于JAVA反射机制的一个题目
- Java基于自定义注释和反射机制实现初级通用DAO
- java反射机制的简单实现步骤
- JAVA反射机制的简单应用
- JAVA反射机制的简单应用
- 梯度下降
- 复习一:分治算法
- 《CLR via C#》读书笔记---10属性
- ListView拓展-下拉刷新,顶部图片放大效果
- 287. Find the Duplicate Number
- Android基于Java反射机制的简单ORM-Dao层
- MVC5.0 怎么会数据库中的html标签读取到页面上????
- C#服务器端与客户端的通信(服务器端)
- Java零基础学习日记-配置jdk环境
- cygwin小记
- 计算机网络常考面试题
- JaveScript之DOM
- 算法分析与设计课程16——376. Wiggle Subsequence
- QT 中Vertical Layout 和 Horizontal Layout