今天心情烦躁,一直在想着怎么起本篇博文的开头…
之前有在做一个即时通讯的功能,既然是即时通讯吧,那么就需要存储数据在本地咯,所以就有了关于数据库使用的一个简单封装,今天抽空整理了一下思路,故而有了本文。
本文将从 数据库基础接口抽离,greenDao知识,greenDao的个性化基类封装和一个简单示例 为序向各位读者说明一下作者的思路以及实践。
数据库操作无非就是 增、删、改、查、事务,好接下来就可以细化了(可能不全,欢迎建议与指正)。
INSERT
DELETE
UPDATE
SELECT
Transaction
interface IDatabase
public interface IDatabase {
boolean insert(M m);
boolean delete(M m);
boolean deleteByKey(K key);
boolean deleteList(List mList);
boolean deleteByKeyInTx(K... key);
boolean deleteAll();
boolean insertOrReplace(@NonNull M m);
boolean update(M m);
boolean updateInTx(M... m);
boolean updateList(List mList);
M selectByPrimaryKey(K key);
List loadAll();
boolean refresh(M m);
/**
* 清理缓存
*/
void clearDaoSession();
/**
* Delete all tables and content from our database
*/
boolean dropDatabase();
/**
* 事务
*/
void runInTx(Runnable runnable);
/**
* 添加集合
*
* @param mList
*/
boolean insertList(List mList);
/**
* 添加集合
*
* @param mList
*/
boolean insertOrReplaceList(List mList);
/**
* 自定义查询
*
* @return
*/
QueryBuilder getQueryBuilder();
/**
* @param where
* @param selectionArg
* @return
*/
List queryRaw(String where, String... selectionArg);
}
特性
- 知识点
- 数据库类型有只读、读写两种
- AsyncSession(API : https://github.com/greenrobot/greenDAO)
- 使用技巧:
这边需要针对于 greenDao 的一些个性化的动作做相应的封装。这边分为 “库操作” 与 “独有动作”两类
calss AbstractDatabaseManager
public abstract class AbstractDatabaseManager<M, K> implements IDatabase<M, K> {
private static final String DEFAULT_DATABASE_NAME = "wenld.db";
/**
* The Android Activity reference for access to DatabaseManager.
*/
private static DaoMaster.DevOpenHelper mHelper;
protected static DaoSession daoSession;
/**
* 初始化OpenHelper
* @param context
*/
public static void initOpenHelper(@NonNull Context context) {
mHelper = getOpenHelper(context, DEFAULT_DATABASE_NAME);
openWritableDb();
}
/**
* 初始化OpenHelper *
* @param context
* @param dataBaseName
*/
public static void initOpenHelper(@NonNull Context context, @NonNull String dataBaseName) {
mHelper = getOpenHelper(context, dataBaseName);
openWritableDb();
}
/** * Query for readable DB */
protected static void openReadableDb() throws SQLiteException {
daoSession = new DaoMaster(getReadableDatabase()).newSession();
}
/** * Query for writable DB */
protected static void openWritableDb() throws SQLiteException {
daoSession = new DaoMaster(getWritableDatabase()).newSession();
}
private static SQLiteDatabase getWritableDatabase() {
return mHelper.getWritableDatabase();
}
private static SQLiteDatabase getReadableDatabase() {
return mHelper.getReadableDatabase();
}
/** * 在applicaiton中初始化DatabaseHelper */
private static DaoMaster.DevOpenHelper getOpenHelper(@NonNull Context context, @Nullable String dataBaseName) {
closeDbConnections();
return new DaoMaster.DevOpenHelper(context, dataBaseName, null);
}
/**
* 只关闭helper就好,看源码就知道helper关闭的时候会关闭数据库
*/
public static void closeDbConnections() {
if (mHelper != null) {
mHelper.close();
mHelper = null;
}
if (daoSession != null) {
daoSession.clear();
daoSession = null;
}
}
@Override
public void clearDaoSession() {
if (daoSession != null) {
daoSession.clear();
daoSession = null;
}
}
@Override
public boolean dropDatabase() {
try {
openWritableDb();
// DaoMaster.dropAllTables(database, true); // drops all tables
// mHelper.onCreate(database); // creates the tables
// daoSession.deleteAll(BankCardBean.class); // clear all elements
// from
// a table
} catch (Exception e) {
return false;
}
return true;
}
@Override
public boolean insert(@NonNull M m) {
try {
if (m == null)
return false;
openWritableDb();
getAbstractDao().insert(m);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean insertOrReplace(@NonNull M m) {
try {
if (m == null)
return false;
openWritableDb();
getAbstractDao().insertOrReplace(m);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean delete(@NonNull M m) {
try {
if (m == null)
return false;
openWritableDb();
getAbstractDao().delete(m);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean deleteByKey(K key) {
try {
if (StringUtils.isEmpty(key.toString()))
return false;
openWritableDb();
getAbstractDao().deleteByKey(key);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean deleteByKeyInTx(K... key) {
try {
openWritableDb();
getAbstractDao().deleteByKeyInTx(key);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean deleteList(List mList) {
try {
if (mList == null || mList.size() == 0)
return false;
openWritableDb();
getAbstractDao().deleteInTx(mList);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean deleteAll() {
try {
openWritableDb();
getAbstractDao().deleteAll();
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean update(@NonNull M m) {
try {
if (m == null)
return false;
openWritableDb();
getAbstractDao().update(m);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean updateInTx(M... m) {
try {
if (m == null)
return false;
openWritableDb();
getAbstractDao().updateInTx(m);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public boolean updateList(List mList) {
try {
if (mList == null || mList.size() == 0)
return false;
openWritableDb();
getAbstractDao().updateInTx(mList);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public M selectByPrimaryKey(@NonNull K key) {
try {
openReadableDb();
return getAbstractDao().load(key);
} catch (SQLiteException e) {
return null;
}
}
@Override
public List loadAll() {
openReadableDb();
return getAbstractDao().loadAll();
}
@Override
public boolean refresh(@NonNull M m) {
try {
if (m == null)
return false;
openWritableDb();
getAbstractDao().refresh(m);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public void runInTx(Runnable runnable) {
try {
openWritableDb();
daoSession.runInTx(runnable);
} catch (SQLiteException e) {
}
}
@Override
public boolean insertList(@NonNull List list) {
try {
if (list == null || list.size() == 0)
return false;
openWritableDb();
getAbstractDao().insertInTx(list);
} catch (SQLiteException e) {
return false;
}
return true;
}
/**
* @param list
* @return
*/
@Override
public boolean insertOrReplaceList(@NonNull List list) {
try {
if (list == null || list.size() == 0)
return false;
openWritableDb();
getAbstractDao().insertOrReplaceInTx(list);
} catch (SQLiteException e) {
return false;
}
return true;
}
@Override
public QueryBuilder getQueryBuilder() {
openReadableDb();
return getAbstractDao().queryBuilder();
}
/**
* @param where
* @param selectionArg
* @return
*/
@Override
public List queryRaw(String where, String... selectionArg) {
openReadableDb();
return getAbstractDao().queryRaw(where, selectionArg);
}
public Query queryRawCreate(String where, Object... selectionArg) {
openReadableDb();
return getAbstractDao().queryRawCreate(where, selectionArg);
}
public Query queryRawCreateListArgs(String where, Collection
在Application 内初始化 helper:
AbstractDatabaseManager.initOpenHelper(getApplicationContext());//初始化数据库
实际类:
public class ChatDbManger extends AbstractDatabaseManager<ChatBean, String> {
@Override
public AbstractDao getAbstractDao() {
return daoSession.getChatBeanDao();
}
}
调用:
new ChatDbManger().insertOrReplace(model);
new ChatDbManger().deleteByKey(chatBean.getId())
.........
Ps: 真没想到,整理加写本博文花了我一天。。。。。
至于greenDao升级 部分 后面会抽出一篇文章来写:greenDao数据库升级(暂时没写…);