为什么要用ORM
在开发android的过程中,我们基本上都会用到sqlite数据库,一般省事的情况下都是使用网上的一些orm框架操作sqlite数据库,比较有名的ORMLite、GreenDao和Active Android等。一般情况下,当预料到数据库会复杂到某个程度,就有必要引入数据库的ORM框架,这样可以大大降低开发和维护的成本。当然,站在巨人的肩膀上,选择好的开源库更可以事半功倍。
选择怎么样的ORM
Android上的ORM框架不少:GreenDao, Active Android,Schematic,Ollie,Sprinkles...到底应该如何选择?这些ORM框架用到的实现方式主要有几种:1.注解(运行时 Annotation,编译时 Annotation)2.反射3.直接生成代码这里简单地直接说结论:通过运行时注解或反射去建立数据表和实体的关系会导致性能比较低。从性能角度出发,应该选择使用编译时注解或代码生成的框架。当然,成熟程度,文档资料等也是考量点。
Android提供了一个SQLiteOpenHelper类来可以很方便的操作数据库,继承和扩展SQLiteOpenHelper类主要做的工作就是重写以下两个方法。
onCreate: 当数据库被首次创建时执行该方法,一般将创建表等初始化操作在该方法中执行。
onUpgrade:当打开数据库时传入的版本号与当前的版本号不同时会调用该方法。
想要封装自己的数据库操作类就要继承SQLiteOpenHelper,到达自己的目的了,首先看下我写的。
public class DBHelper extends SQLiteOpenHelper{
private static Logger logger = LoggerFactory.getLogger(DBHelper.class);
private final static String DATABASE_PATH = Environment.getExternalStorageDirectory() + "/";
private Context context;
private int dbVersion;
public DBHelper(Context context, String dbName, int initVersion) {
super(context, dbName, (SQLiteDatabase.CursorFactory)null, initVersion);
this.context = context;
this.dbVersion = initVersion;
}
@Override
public void onCreate(SQLiteDatabase db) {
for(int i = 1; i <= this.dbVersion; ++i) {
this.upgradeSqlData(db, i, false);
}
}
private void upgradeSqlData(SQLiteDatabase db, int version, boolean isDowngrade) {
String fileName;
if(isDowngrade) {
fileName = "_" + version + "_db.sql";
} else {
fileName = version + "_db.sql";
}
BufferedReader bufferedReader = null;
db.beginTransaction();
try {
InputStream e = this.context.getAssets().open(fileName);
InputStreamReader reader = new InputStreamReader(e, "UTF-8");
bufferedReader = new BufferedReader(reader);
String line = bufferedReader.readLine();
StringBuilder sb = new StringBuilder();
while(true) {
while(line != null) {
line = line.trim();
if(!"".equals(line) && line.charAt(0) != 47 && line.charAt(0) != 45) {
boolean middleIndex = true;
int middleIndex1;
if((middleIndex1 = line.lastIndexOf("--")) != -1) {
line = line.substring(0, middleIndex1);
}
sb.append(line);
String sql = sb.toString();
if(!"".equals(sql) && sql.charAt(sql.length() - 1) == 59) {
logger.debug("load sql:{}", sql);
db.execSQL(sql.substring(0, sql.indexOf(59)));
sb = new StringBuilder();
}
line = bufferedReader.readLine();
} else {
line = bufferedReader.readLine();
}
}
db.setTransactionSuccessful();
logger.info("load file {} success.", fileName);
Log.e("TAG","load file {} success." + fileName);
break;
}
} catch (Exception var20) {
logger.error("load file {} failed.", fileName, var20);
Log.e("TAG","load file {} success." + fileName);
} finally {
try {
if(bufferedReader != null) {
bufferedReader.close();
}
db.endTransaction();
} catch (IOException var19) {
;
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for(int i = oldVersion + 1; i <= newVersion; ++i) {
this.upgradeSqlData(db, i, false);
}
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for(int i = oldVersion - 1; i >= newVersion; --i) {
this.upgradeSqlData(db, i, false);
}
}
// @Override
// public SQLiteDatabase getWritableDatabase() {
// File file = new File(DATABASE_PATH + "dbtest");
// if (!file.exists()) {
// file.mkdirs();
//
// }
// File dbFile = new File(file,"mydb.db");
// if (!dbFile.exists())
// try {
// dbFile.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return SQLiteDatabase.openOrCreateDatabase(dbFile,null);
// }
//
// @Override
// public SQLiteDatabase getReadableDatabase() {
// return getWritableDatabase();
// }
}
-- Describe USER
CREATE TABLE cdd_user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_name TEXT, --对应userCode
user_age TEXT --对应userName
);
public class BaseRepository {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
protected DBHelper dbHelper;
public BaseRepository(Context context) {
dbHelper = DBUtil.getInstance(context).getDbHelper();
}
/**
* 数据插入
* @param table
* @param nullColumnHack
* @param values
* @return
*/
public long insert(String table, String nullColumnHack, ContentValues values) {
long ret = 0;
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.beginTransaction();
try {
ret = database.insert(table, nullColumnHack, values);
database.setTransactionSuccessful();
} catch (RuntimeException e) {
logger.error("insert error. ", e);
} finally {
database.endTransaction();
}
return ret;
}
/**
* 数据查询
* @param table
* @param columns
* @param selection
* @param selectionArgs
* @param groupBy
* @param having
* @param orderBy
* @param limit
* @param
* @return
*/
public List query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, Integer limit) {
List results = new ArrayList();
Cursor cursor = null;
try {
if (limit != null) {
cursor = dbHelper.getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit + "");
} else {
cursor = dbHelper.getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
results = queryResult(cursor);
} catch (RuntimeException e) {
logger.error("query error. ", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return results;
}
/**
* 转换为对象
* @param cursor
* @param
* @return
*/
public List queryResult(Cursor cursor) {
throw new RuntimeException("Please overwrite method.");
}
/**
* 数据更新
* @param table
* @param values
* @param whereClause
* @param whereArgs
* @return
*/
public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
int ret = 0;
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.beginTransaction();
try {
ret = database.update(table, values, whereClause, whereArgs);
database.setTransactionSuccessful();
} catch (RuntimeException e) {
logger.error("update error. ", e);
} finally {
database.endTransaction();
}
return ret;
}
/**
* 数据删除
* @param table
* @param whereClause
* @param whereArgs
* @return
*/
public int delete(String table, String whereClause, String[] whereArgs) {
int ret = 0;
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.beginTransaction();
try {
ret = database.delete(table, whereClause, whereArgs);
database.setTransactionSuccessful();
} catch (RuntimeException e) {
logger.error("delete error. ", e);
} finally {
database.endTransaction();
}
return ret;
}
}
public class UserRepository extends BaseRepository{
public UserRepository(Context context) {
super(context);
}
/**
* 插入数据
* @param user
* @return
*/
public long insert(User user) {
ContentValues values = getContentValues(user);
values.remove("id");
return insert(DBConstant.TABLE_USER,null,values);
}
/**
* 更新数据
* @param user
*/
public void update(User user) {
ContentValues cv = getContentValues(user);
cv.remove("id");
update(DBConstant.TABLE_USER,cv,"id = ?",new String[]{user.getId()+""});
}
/**
* 删除数据
* @param id
*/
public void delete(int id) {
delete(DBConstant.TABLE_USER,"id = ?",new String[]{id+""});
}
/**
* 查询所有数据
* @return
*/
public List readAllUsers() {
List list = query(DBConstant.TABLE_USER,null,null, null, null, null, null,null);
if(list != null && !list.isEmpty())
{
return list;
}
return null;
}
@Override
public List queryResult(Cursor cursor) {
List userList = new ArrayList();
User user;
while (cursor.moveToNext()) {
user = new User();
user.setId(cursor.getInt(cursor.getColumnIndex("id")));
user.setUserName(cursor.getString(cursor.getColumnIndex("user_name")));
user.setAge(cursor.getInt(cursor.getColumnIndex("user_age")));
userList.add(user);
}
return userList;
}
private ContentValues getContentValues(User user) {
ContentValues cv = new ContentValues();
cv.put("id",user.getId());
cv.put("user_name",user.getUserName());
cv.put("user_age",user.getAge());
return cv;
}
public class DBUtil {
private static DBUtil instance;
private static DBHelper dbHelper;
private DBUtil() {
}
public static DBUtil getInstance(Context context) {
if (instance == null) {
instance = new DBUtil();
initDBHelper(context);
}
return instance;
}
private static void initDBHelper(Context context) {
dbHelper = new DBHelper(context, DBConstant.DB_NAME,DBConstant.DB_INIT_VERSION);
dbHelper.onCreate(dbHelper.getWritableDatabase());
}
public DBHelper getDbHelper(){
return dbHelper;
}
}