前言
最近的項(xiàng)目需要使用到數(shù)據(jù)庫(kù),本來(lái)想用Sqlite數(shù)據(jù)來(lái)做的,但是聽(tīng)同事說(shuō)使用Greendao數(shù)據(jù)庫(kù)是真的好用,我就半信半疑的去網(wǎng)上找了下greendao數(shù)據(jù)庫(kù)的資料,以及簡(jiǎn)單的使用之后,徹底把我征服了。其實(shí)寫(xiě)數(shù)據(jù)庫(kù)最繁瑣的還是sql語(yǔ)言,而對(duì)于我這樣的數(shù)據(jù)庫(kù)小白都可以輕松的使用,實(shí)在是太好用了。使用greendao之前我們需要了解的:
認(rèn)識(shí)GreenDao之前必須知道ORM(Object Relation Mapping對(duì)象關(guān)系映射),其表現(xiàn)形式就是通過(guò)GreenDao將數(shù)據(jù)庫(kù)和Bean對(duì)象關(guān)聯(lián)起來(lái),其表現(xiàn)形式如下圖

GreenDao之所以很流行,跟它的優(yōu)點(diǎn)是息息相關(guān)的,從官網(wǎng)中可以看到這樣一張圖,其表示了在主流的ORM第三方庫(kù)中,其對(duì)數(shù)據(jù)庫(kù)操作的速度是最快的:

不僅如此,其優(yōu)點(diǎn)還包括有以下幾點(diǎn)
1:存取速度快
2:支持?jǐn)?shù)據(jù)庫(kù)加密
3:輕量級(jí)
4:激活實(shí)體
5:支持緩存
6:代碼自動(dòng)
接入greendao到項(xiàng)目
1:需要在項(xiàng)目(Project)的build.gradle中加入依賴(lài):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
//GreenDao3.2依賴(lài)
classpath'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
2:在Moudle的build.gradle文件中添加依賴(lài):
//使用greendao
applyplugin:'org.greenrobot.greendao'
添加依賴(lài)(dependencies)中:
//greendao3.2的依賴(lài)
compile'org.greenrobot:greendao:3.2.0'
3:添加greendao的數(shù)據(jù)庫(kù)配置:
greendao {
schemaVersion 1 ?//數(shù)據(jù)庫(kù)版本號(hào) 每次升級(jí)數(shù)據(jù)庫(kù)都需要改變版本,只能增加
daoPackage ?'com.pacgename.greendao.gen' ? //設(shè)置DaoMaster、DaoSession、Dao包名
targetGenDir ?'src/main/java' ? ?//設(shè)置DaoMaster、DaoSession、Dao目錄
//targetGenDirTest:設(shè)置生成單元測(cè)試目錄
//generateTests:設(shè)置自動(dòng)生成單元測(cè)試用例
}
使用GreenDao數(shù)據(jù)庫(kù)
一:創(chuàng)建Entity類(lèi)(相當(dāng)于開(kāi)發(fā)中的Bean類(lèi))



greendao采用注解來(lái)創(chuàng)建Entity類(lèi),創(chuàng)建完成之后build自己的Moudlle,會(huì)自動(dòng)生成:
1:Bean實(shí)體的構(gòu)造方法和get、set方法
2:DaoMaster、DaoSession、DAOS類(lèi) ?這里的類(lèi)生成我們?cè)贛oudle的build.gradle中設(shè)置的
daoPackage路徑
這里對(duì)Bean對(duì)象的注釋進(jìn)行解釋
@Entity:告訴GreenDao該對(duì)象為實(shí)體,只有被@Entity注釋的Bean類(lèi)才能被dao類(lèi)操作
@Id:對(duì)象的Id,使用Long類(lèi)型作為EntityId,否則會(huì)報(bào)錯(cuò)。(autoincrement = true)表示主鍵會(huì)自增,如果false就會(huì)使用舊值
@Property:可以自定義字段名,注意外鍵不能使用該屬性
@NotNull:屬性不能為空
@Transient:使用該注釋的屬性不會(huì)被存入數(shù)據(jù)庫(kù)的字段中
@Unique:該屬性值必須在數(shù)據(jù)庫(kù)中是唯一值
@Generated:編譯后自動(dòng)生成的構(gòu)造函數(shù)、方法等的注釋?zhuān)崾緲?gòu)造函數(shù)、方法等不能被修改
二:創(chuàng)建數(shù)據(jù)庫(kù)和表,初始化數(shù)據(jù)庫(kù):
public class MyApplication extends Application {
public static ? ?DaoSession ?daoSession;
@Override
public voidonCreate() {
super.onCreate();
setupDatabase();
}
/**
*配置數(shù)據(jù)庫(kù)
*/
private voidsetupDatabase() {
//創(chuàng)建數(shù)據(jù)庫(kù)book.db"
?DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "book.db", null);
//獲取可寫(xiě)數(shù)據(jù)庫(kù)
SQLiteDatabase db = helper.getWritableDatabase();
//獲取數(shù)據(jù)庫(kù)對(duì)象
DaoMaster daoMaster =newDaoMaster(db);
//獲取Dao對(duì)象管理者
daoSession= daoMaster.newSession();
}
public staticDaoSessiongetDaoInstant() {
returndaoSession;
}
}
可以發(fā)現(xiàn),GreenDao已經(jīng)將我們的數(shù)據(jù)庫(kù)創(chuàng)建縮成幾句話(huà),代碼會(huì)自動(dòng)將Bean對(duì)象創(chuàng)建成表,不再是傳統(tǒng)的手寫(xiě)SQL語(yǔ)句。這里的數(shù)據(jù)庫(kù)創(chuàng)建只需要在Application中執(zhí)行一次即可,這里對(duì)幾個(gè)類(lèi)進(jìn)行解釋
DevOpenHelper:創(chuàng)建SQLite數(shù)據(jù)庫(kù)的SQLiteOpenHelper的具體實(shí)現(xiàn)
DaoMaster:GreenDao的頂級(jí)對(duì)象,作為數(shù)據(jù)庫(kù)對(duì)象、用于創(chuàng)建表和刪除表
DaoSession:管理所有的Dao對(duì)象,Dao對(duì)象中存在著增刪改查等API
由于我們已經(jīng)創(chuàng)建好了DaoSession和Shop的Bean對(duì)象,編譯后會(huì)自動(dòng)生成我們的ShopDao對(duì)象,可通過(guò)DaoSession獲得
ShopDao dao = daoSession.getBookDao();
這里的Dao(Data Access Object)是指數(shù)據(jù)訪(fǎng)問(wèn)接口,即提供了數(shù)據(jù)庫(kù)操作一些API接口,可通過(guò)dao進(jìn)行增刪改查操作
數(shù)據(jù)庫(kù)和表都已經(jīng)建好了,我們就只需要去操作數(shù)據(jù)庫(kù)了:
三:greendao數(shù)據(jù)庫(kù)的基本操作(增,刪,改,查)
直接附上代碼:
BookDaobookDao;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.query_user).setOnClickListener(this);
findViewById(R.id.insert_user).setOnClickListener(this);
findViewById(R.id.delete_user).setOnClickListener(this);
BookDao bookDao = MyApplication.getDaoInstant().getBookDao();
}
@Override
public voidonClick(View v) {
switch(v.getId()) {
caseR.id.insert_user:
//插入數(shù)據(jù) 單個(gè)的Book對(duì)象
bookDao.insertOrReplace(newBook((long)1,"西游記",20,10));
//插入一個(gè)List數(shù)組? 只要是Iterable的子類(lèi)都可以插入
List mBookList =newArrayList<>();
mBookList.add(newBook((long)2,"紅樓夢(mèng)",30,5));
mBookList.add(newBook((long)3,"水滸傳",20,10));
mBookList.add(newBook((long)4,"三國(guó)演義",20,10));
mBookList.add(newBook((long)5,"紅樓夢(mèng)",20,15));
mBookList.add(newBook((long)6,"紅樓西游",20,15));
bookDao.insertOrReplaceInTx(mBookList);
break;
caseR.id.query_user:
//根據(jù)查詢(xún)Bookname查詢(xún)出來(lái),并且更具id排序,最后返回list數(shù)組
List books =bookDao.queryBuilder().
where(BookDao.Properties.Bookname.eq("紅樓夢(mèng)")).
orderDesc(BookDao.Properties.Id).list();
//查詢(xún)所有的數(shù)據(jù)
List list =bookDao.queryBuilder().list();
break;
caseR.id.delete_user:
//根據(jù)id刪除數(shù)據(jù)
bookDao.deleteByKey((long)1);
break;
caseR.id.update_user:
//其實(shí)插入可以使用插入InsertorReplace來(lái)更新數(shù)據(jù)? 根據(jù)id更新
bookDao.update(newBook((long)1,"西游記",10,20));
break;
}
}
四:greendao數(shù)據(jù)庫(kù)的混淆
在進(jìn)行打包發(fā)布時(shí)候,不能混淆掉greendao的類(lèi)以及數(shù)據(jù),在混淆文件中加入下列代碼:
#greendao3.2.0,此是針對(duì)3.2.0,如果是之前的,可能需要更換下包名-keepclassorg.greenrobot.greendao.**{*;}
-keepclassmembersclass*extendsorg.greenrobot.greendao.AbstractDao{publicstaticjava.lang.String TABLENAME;
}
-keepclass**$Properties
關(guān)于GreenDao的的基本概念與基本操作就講到這里,更多對(duì)于GreenDao的數(shù)據(jù)庫(kù)操作還需要多多從實(shí)戰(zhàn)中去探索,這里只是一個(gè)快速入門(mén)的引導(dǎo).GreenDao高級(jí)操作還包括有:多表查詢(xún)、多表關(guān)聯(lián)、session緩存等用法,可以到GreenDao的官網(wǎng)進(jìn)行學(xué)習(xí)。