前言
最近的項(xiàng)目需要使用到數(shù)據(jù)庫飒筑,本來想用Sqlite數(shù)據(jù)來做的,但是聽同事說使用Greendao數(shù)據(jù)庫是真的好用绽昏,我就半信半疑的去網(wǎng)上找了下greendao數(shù)據(jù)庫的資料协屡,以及簡單的使用之后,徹底把我征服了全谤。其實(shí)寫數(shù)據(jù)庫最繁瑣的還是sql語言肤晓,而對于我這樣的數(shù)據(jù)庫小白都可以輕松的使用,實(shí)在是太好用了认然。使用greendao之前我們需要了解的:
認(rèn)識GreenDao之前必須知道ORM(Object Relation Mapping對象關(guān)系映射)补憾,其表現(xiàn)形式就是通過GreenDao將數(shù)據(jù)庫和Bean對象關(guān)聯(lián)起來,其表現(xiàn)形式如下圖
GreenDao之所以很流行卷员,跟它的優(yōu)點(diǎn)是息息相關(guān)的盈匾,從官網(wǎng)中可以看到這樣一張圖,其表示了在主流的ORM第三方庫中毕骡,其對數(shù)據(jù)庫操作的速度是最快的:
不僅如此削饵,其優(yōu)點(diǎn)還包括有以下幾點(diǎn)
1:存取速度快
2:支持?jǐn)?shù)據(jù)庫加密
3:輕量級
4:激活實(shí)體
5:支持緩存
6:代碼自動
接入greendao到項(xiàng)目
1:需要在項(xiàng)目(Project)的build.gradle中加入依賴:
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依賴
classpath'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
2:在Moudle的build.gradle文件中添加依賴:
//使用greendao
applyplugin:'org.greenrobot.greendao'
添加依賴(dependencies)中:
//greendao3.2的依賴
compile'org.greenrobot:greendao:3.2.0'
3:添加greendao的數(shù)據(jù)庫配置:
greendao {
schemaVersion 1 ?//數(shù)據(jù)庫版本號 每次升級數(shù)據(jù)庫都需要改變版本岩瘦,只能增加
daoPackage ?'com.pacgename.greendao.gen' ? //設(shè)置DaoMaster、DaoSession葵孤、Dao包名
targetGenDir ?'src/main/java' ? ?//設(shè)置DaoMaster、DaoSession橱赠、Dao目錄
//targetGenDirTest:設(shè)置生成單元測試目錄
//generateTests:設(shè)置自動生成單元測試用例
}
使用GreenDao數(shù)據(jù)庫
一:創(chuàng)建Entity類(相當(dāng)于開發(fā)中的Bean類)
greendao采用注解來創(chuàng)建Entity類尤仍,創(chuàng)建完成之后build自己的Moudlle,會自動生成:
1:Bean實(shí)體的構(gòu)造方法和get狭姨、set方法
2:DaoMaster宰啦、DaoSession、DAOS類 ?這里的類生成我們在Moudle的build.gradle中設(shè)置的
daoPackage路徑
這里對Bean對象的注釋進(jìn)行解釋
@Entity:告訴GreenDao該對象為實(shí)體饼拍,只有被@Entity注釋的Bean類才能被dao類操作
@Id:對象的Id赡模,使用Long類型作為EntityId,否則會報(bào)錯(cuò)师抄。(autoincrement = true)表示主鍵會自增漓柑,如果false就會使用舊值
@Property:可以自定義字段名,注意外鍵不能使用該屬性
@NotNull:屬性不能為空
@Transient:使用該注釋的屬性不會被存入數(shù)據(jù)庫的字段中
@Unique:該屬性值必須在數(shù)據(jù)庫中是唯一值
@Generated:編譯后自動生成的構(gòu)造函數(shù)叨吮、方法等的注釋辆布,提示構(gòu)造函數(shù)、方法等不能被修改
二:創(chuàng)建數(shù)據(jù)庫和表茶鉴,初始化數(shù)據(jù)庫:
public class MyApplication extends Application {
public static ? ?DaoSession ?daoSession;
@Override
public voidonCreate() {
super.onCreate();
setupDatabase();
}
/**
*配置數(shù)據(jù)庫
*/
private voidsetupDatabase() {
//創(chuàng)建數(shù)據(jù)庫book.db"
?DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "book.db", null);
//獲取可寫數(shù)據(jù)庫
SQLiteDatabase db = helper.getWritableDatabase();
//獲取數(shù)據(jù)庫對象
DaoMaster daoMaster =newDaoMaster(db);
//獲取Dao對象管理者
daoSession= daoMaster.newSession();
}
public staticDaoSessiongetDaoInstant() {
returndaoSession;
}
}
可以發(fā)現(xiàn)锋玲,GreenDao已經(jīng)將我們的數(shù)據(jù)庫創(chuàng)建縮成幾句話,代碼會自動將Bean對象創(chuàng)建成表涵叮,不再是傳統(tǒng)的手寫SQL語句惭蹂。這里的數(shù)據(jù)庫創(chuàng)建只需要在Application中執(zhí)行一次即可,這里對幾個(gè)類進(jìn)行解釋
DevOpenHelper:創(chuàng)建SQLite數(shù)據(jù)庫的SQLiteOpenHelper的具體實(shí)現(xiàn)
DaoMaster:GreenDao的頂級對象割粮,作為數(shù)據(jù)庫對象盾碗、用于創(chuàng)建表和刪除表
DaoSession:管理所有的Dao對象,Dao對象中存在著增刪改查等API
由于我們已經(jīng)創(chuàng)建好了DaoSession和Shop的Bean對象舀瓢,編譯后會自動生成我們的ShopDao對象置尔,可通過DaoSession獲得
ShopDao dao = daoSession.getBookDao();
這里的Dao(Data Access Object)是指數(shù)據(jù)訪問接口,即提供了數(shù)據(jù)庫操作一些API接口氢伟,可通過dao進(jìn)行增刪改查操作
數(shù)據(jù)庫和表都已經(jīng)建好了榜轿,我們就只需要去操作數(shù)據(jù)庫了:
三:greendao數(shù)據(jù)庫的基本操作(增,刪朵锣,改谬盐,查)
直接附上代碼:
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對象
bookDao.insertOrReplace(newBook((long)1,"西游記",20,10));
//插入一個(gè)List數(shù)組? 只要是Iterable的子類都可以插入
List mBookList =newArrayList<>();
mBookList.add(newBook((long)2,"紅樓夢",30,5));
mBookList.add(newBook((long)3,"水滸傳",20,10));
mBookList.add(newBook((long)4,"三國演義",20,10));
mBookList.add(newBook((long)5,"紅樓夢",20,15));
mBookList.add(newBook((long)6,"紅樓西游",20,15));
bookDao.insertOrReplaceInTx(mBookList);
break;
caseR.id.query_user:
//根據(jù)查詢Bookname查詢出來,并且更具id排序诚些,最后返回list數(shù)組
List books =bookDao.queryBuilder().
where(BookDao.Properties.Bookname.eq("紅樓夢")).
orderDesc(BookDao.Properties.Id).list();
//查詢所有的數(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來更新數(shù)據(jù)? 根據(jù)id更新
bookDao.update(newBook((long)1,"西游記",10,20));
break;
}
}
四:greendao數(shù)據(jù)庫的混淆
在進(jìn)行打包發(fā)布時(shí)候飞傀,不能混淆掉greendao的類以及數(shù)據(jù),在混淆文件中加入下列代碼:
#greendao3.2.0,此是針對3.2.0皇型,如果是之前的,可能需要更換下包名-keepclassorg.greenrobot.greendao.**{*;}
-keepclassmembersclass*extendsorg.greenrobot.greendao.AbstractDao{publicstaticjava.lang.String TABLENAME;
}
-keepclass**$Properties
關(guān)于GreenDao的的基本概念與基本操作就講到這里砸烦,更多對于GreenDao的數(shù)據(jù)庫操作還需要多多從實(shí)戰(zhàn)中去探索弃鸦,這里只是一個(gè)快速入門的引導(dǎo).GreenDao高級操作還包括有:多表查詢、多表關(guān)聯(lián)幢痘、session緩存等用法唬格,可以到GreenDao的官網(wǎng)進(jìn)行學(xué)習(xí)。