无需配置,但需要按照一定的规则写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());
}
Android基于Java反射机制的简单ORM-Dao层
最新推荐文章于 2021-05-27 13:15:35 发布