- GreenDao是一個對象關(guān)系型框架平窘,也就是我們常說的ORM框架炉旷,使用它可以通過操作對象的方式去操作數(shù)據(jù)庫楷兽。從而不用手寫sql代碼地熄,簡化開發(fā)。
1. 在項目的build.gradle中相應(yīng)的代碼
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // 添加
}
}
2. 在app/build.gradle中相應(yīng)的代碼
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // 添加
android {
greendao {
// 指定數(shù)據(jù)庫schema版本號芯杀,遷移等操作會用到
schemaVersion 1
// 設(shè)置生成數(shù)據(jù)庫文件的目錄端考,默認(rèn)是在build中,可以將生成的文件放到我們的java目錄中
targetGenDir 'src/main/java'
// 設(shè)置生成的數(shù)據(jù)庫相關(guān)文件的包名揭厚,默認(rèn)為entity所在的包名
daoPackage '應(yīng)用包名.db.greendao'
}
}
dependencies {
implementation 'org.greenrobot:greendao:3.3.0' // 添加
}
3. 需要根據(jù)第二步中的daoPackage配置的包名創(chuàng)建出對應(yīng)的空文件却特。
4. 在entity目錄下新建自己的實體類,如IdentificationResultsEntry
圖片.png
需要注意的是@Id:通過這個注解標(biāo)記的字段必須是包裝類型Long類型筛圆,表示該字段為主鍵裂明,并且它默認(rèn)就是自增。否則在升級/降級數(shù)據(jù)庫的時候太援,會報錯漾岳。*
5. 通過Make Project 后,我們會發(fā)現(xiàn)在greendao目錄下自動生成了UserInfoDao文件粉寞。
圖片.png
生成數(shù)據(jù)庫操作類
我們先封裝一個DBManager類
public class DaoManager {
private static final String TAG = DaoManager.class.getSimpleName();
private static final String DB_NAME = "greendaotest";
private Context context;
//多線程中要被共享的使用volatile關(guān)鍵字修飾
private volatile static DaoManager manager = new DaoManager();
private static DaoMaster sDaoMaster;
private static DaoMaster.DevOpenHelper sHelper;
private static DaoSession sDaoSession;
/**
* 單例模式獲得操作數(shù)據(jù)庫對象
*
* @return
*/
public static DaoManager getInstance() {
return manager;
}
private DaoManager() {
setDebug();
}
public void init(Context context) {
this.context = context;
}
/**
* 判斷是否有存在數(shù)據(jù)庫尼荆,如果沒有則創(chuàng)建
*
* @return
*/
public DaoMaster getDaoMaster() {
if (sDaoMaster == null) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
sDaoMaster = new DaoMaster(helper.getWritableDatabase());
}
return sDaoMaster;
}
/**
* 完成對數(shù)據(jù)庫的添加、刪除唧垦、修改捅儒、查詢操作,僅僅是一個接口
*
* @return
*/
public DaoSession getDaoSession() {
if (sDaoSession == null) {
if (sDaoMaster == null) {
sDaoMaster = getDaoMaster();
}
sDaoSession = sDaoMaster.newSession();
}
return sDaoSession;
}
/**
* 打開輸出日志,默認(rèn)關(guān)閉
*/
public void setDebug() {
if (BuildConfig.DEBUG) {
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
}
}
/**
* 關(guān)閉所有的操作巧还,數(shù)據(jù)庫開啟后鞭莽,使用完畢要關(guān)閉
*/
public void closeConnection() {
closeHelper();
closeDaoSession();
}
public void closeHelper() {
if (sHelper != null) {
sHelper.close();
sHelper = null;
}
}
public void closeDaoSession() {
if (sDaoSession != null) {
sDaoSession.clear();
sDaoSession = null;
}
}
}
對數(shù)據(jù)表的進(jìn)行操作工具類
public class DbOperationUtils {
private static final String TAG = DbOperationUtils.class.getSimpleName();
private DaoManager mManager;
public DbOperationUtils(Context context) {
mManager = DaoManager.getInstance();
mManager.init(context);
}
/**
* 完成IdentificationResultsEntry記錄的插入,如果表未創(chuàng)建麸祷,先創(chuàng)建IdentificationResultsEntry表
*
* @param ident
* @return
*/
public boolean insertIdent(IdentificationResultsEntry ident) {
boolean flag = false;
flag = mManager.getDaoSession().getIdentificationResultsEntryDao().insert(ident) == -1 ? false : true;
Log.i(TAG, "insert ident :" + flag + "-->" + ident.toString());
return flag;
}
/**
* 插入多條數(shù)據(jù)澎怒,在子線程操作
*
* @param identList
* @return
*/
public boolean insertMultIdent(final List<IdentificationResultsEntry> identList) {
boolean flag = false;
try {
mManager.getDaoSession().runInTx(new Runnable() {
@Override
public void run() {
for (IdentificationResultsEntry ident : identList) {
mManager.getDaoSession().insertOrReplace(ident);
}
}
});
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 修改一條數(shù)據(jù)
*
* @param ident
* @return
*/
public boolean updateIdent(IdentificationResultsEntry ident) {
boolean flag = false;
try {
mManager.getDaoSession().update(ident);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 刪除單條記錄
*
* @param ident
* @return
*/
public boolean deleteIdent(IdentificationResultsEntry ident) {
boolean flag = false;
try {
//按照id刪除
mManager.getDaoSession().delete(ident);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 刪除所有記錄
*
* @return
*/
public boolean deleteAll() {
boolean flag = false;
try {
//按照id刪除
mManager.getDaoSession().deleteAll(IdentificationResultsEntry.class);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 查詢所有記錄
*
* @return
*/
public List<IdentificationResultsEntry> queryAllIdent() {
return mManager.getDaoSession().loadAll(IdentificationResultsEntry.class);
}
/**
* 根據(jù)主鍵id查詢記錄
*
* @param key
* @return
*/
public IdentificationResultsEntry queryIdentById(long key) {
return mManager.getDaoSession().load(IdentificationResultsEntry.class, key);
}
/**
* 使用native sql進(jìn)行查詢操作
*/
public List<IdentificationResultsEntry> queryIdentByNativeSql(String sql, String[] conditions) {
return mManager.getDaoSession().queryRaw(IdentificationResultsEntry.class, sql, conditions);
}
/**
* 使用queryBuilder進(jìn)行查詢
*
* @return
*/
public List<IdentificationResultsEntry> queryIdentByQueryBuilder(long id) {
QueryBuilder<IdentificationResultsEntry> queryBuilder = mManager.getDaoSession().queryBuilder(IdentificationResultsEntry.class);
return queryBuilder.where(IdentificationResultsEntryDao.Properties.SearchId.eq(id)).list();
}
}