第三方數(shù)據(jù)庫GreenDao總結(jié)

一.GreenDao的概述以及特點:

基于對象關(guān)系的映射方式來操作數(shù)據(jù)庫的框架,提供一個接口通過操作對象的方式操作數(shù)據(jù)庫

適用于 Android 的ORM 框架,現(xiàn)在市面上主流的框架有 OrmLite、SugarORM、Active Android、Realm 與 GreenDAO。

GreenDAO是一種Android數(shù)據(jù)庫ORM(對象映射關(guān)系(Object Relation Mapping))框架,與OrmLite、ActiveOrm、LitePal等數(shù)據(jù)庫相比,單位時間內(nèi)可以插入、更新和查詢更多的數(shù)據(jù),而且提供了大量的靈活通用接口。

1,通常我們在使用GreenDao的時候,我們只需定義數(shù)據(jù)模型,GreenDao框架將創(chuàng)建數(shù)據(jù)對象(實體)和DAO(數(shù)據(jù)訪問對象),能夠節(jié)省部分代碼。

2,不向性能妥協(xié),使用了GreenDao,大多數(shù)實體可以以每秒幾千個實體的速率進行插入,更新和加載。

3,GreenDao支持加密數(shù)據(jù)庫來保護敏感數(shù)據(jù)。

4,微小的依賴庫,GreenDao的關(guān)鍵依賴庫大小不超過100kb.

5,如果需要,實體是可以被激活的。而活動實體可以透明的解析關(guān)系(我們要做的只是調(diào)用getter即可),并且有更新、刪除和刷新方法,以便訪問持久性功能。

6,GreenDao允許您將協(xié)議緩沖區(qū)(protobuf)對象直接保存到數(shù)據(jù)庫中。如果您通過protobuf通話到您的服務器,則不需要另一個映射。常規(guī)實體的所有持久性操作都可以用于protobuf對象。所以,相信這是GreenDao的獨特之處。

7,自動生成代碼,我們無需關(guān)注實體類以及Dao,因為GreenDao已經(jīng)幫我們生成了。

8,開源 有興趣的同學可以查看源碼,深入去了解機制。



二.GreenDao配置依賴:

官網(wǎng):

https://github.com/greenrobot/greenDAO/

配置:

//1.工程配置:添加插件 更好支持GreenDao

buildscript {

? ? repositories {

? ? ? ? jcenter()

? ? ? ? mavenCentral() // 添加的代碼

? ? }

? ? dependencies {

? ? ? ? classpath 'com.android.tools.build:gradle:2.3.3'

? ? ? ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin

? ? }

}

//2.項目配置:添加插件

apply plugin: 'com.android.application'

apply plugin: 'org.greenrobot.greendao' // apply plugin

//3.項目配置:添加依賴

dependencies {

? ? //greendao

? ? implementation 'org.greenrobot:greendao:3.2.2' // add library

}

//4.初始化GreenDao配置

greendao{

? ? ? ? schemaVersion 1 //數(shù)據(jù)庫版本號

? ? ? ? daoPackage 'com.example.lizhengjun.dao'? //數(shù)據(jù)庫全路徑

? ? ? ? targetGenDir 'src/main/java'? //存放位置

? ? }

schemaVersion--> 指定數(shù)據(jù)庫schema版本號,遷移等操作會用到;

daoPackage --> dao的包名,包名默認是entity所在的包;

targetGenDir --> 生成數(shù)據(jù)庫文件的目錄;



三.GreenDao的使用思路:

//1、配置文件中的設(shè)置

// 在根工程中完成內(nèi)容配置:

buildscript {

? ? repositories {

? ? ? ? jcenter()

? ? ? ? mavenCentral() // 添加的代碼

? ? }

? ? dependencies {

? ? ? ? classpath 'com.android.tools.build:gradle:2.3.3'

? ? ? ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin

? ? }

}

//在子工程中完成內(nèi)容配置:

apply plugin: 'com.android.application'

apply plugin: 'org.greenrobot.greendao' // apply plugin

dependencies {

? ? compile 'org.greenrobot:greendao:3.2.2' // add library

}

2、設(shè)置Green中的DaoMaster/DaoSession/實體類Dao生成路徑

//自定義生成路徑 daoPackage 'a.b.c'

greendao {

? ? schemaVersion 1

? ? daoPackage 'com.example.lizhengjun.dao'

? ? targetGenDir 'src/main/java'

}

3、設(shè)置實體類

@Entity

public class Student {

? ? @Id(autoincrement = true)

? ? private Long id;

? ? @Property

? ? @NotNull

? ? private String name;

? ? @Property

? ? private int age;

}

4、文件生成

Build - > ReBuild Project

5、在Application類中完成內(nèi)容配置(或者使用工具類)

Application有自己的生命周期,OnCreate方法必須首先被調(diào)用

①本類對象的獲取

②DaoMaster、DaoSession對象的獲取

③提供方法,獲取DaoSession對象

需要注意:必須在AndroidManifest.xml文件完成配置

<application

? ? ? ? android:name=".App">

</application>

6、獲取實體類Dao對象

XXXDao xxxdao =? App.getInstance().getDaoSession().getXXXDao();

xxxdao.增刪改查();


四.GreenDao的注解:

@Entity 標識實體類,greenDAO會映射成sqlite的一個表,表名為實體類名的大寫形式

@Id 標識主鍵,該字段的類型為long或Long類型,autoincrement設(shè)置是否自動增長

@Property? ? ? 標識該屬性在表中對應的列名稱, nameInDb設(shè)置名稱

@Transient? ? ? 標識該屬性將不會映射到表中,也就是沒有這列

@NotNull? ? ? ? 設(shè)置表中當前列的值不可為空

@Convert? ? ? ? 指定自定義類型(@linkPropertyConverter)

@Generated 運行所產(chǎn)生的構(gòu)造函數(shù)或者方法,被此標注的代碼可以變更或者下次運行時清除

@Index? ? 使用@Index作為一個屬性來創(chuàng)建一個索引;

@JoinEntity? ? 定義表連接關(guān)系

@JoinProperty? ? ? ? 定義名稱和引用名稱屬性關(guān)系

@Keep? ? 注解的代碼段在GreenDao下次運行時保持不變

@OrderBy? ? ? ? 指定排序方式

@ToMany? ? ? ? 定義與多個實體對象的關(guān)系

@ToOne? 定義與另一個實體(一個實體對象)的關(guān)系

@Unique 向數(shù)據(jù)庫列添加了一個唯一的約束

/**

* @Entity

* @Id(autoincrement = true)? 標志主鍵

* @NotNull 標志這個字段不能是null

* @Property(nameInDb = "User")

* @Transient 表示不存儲在數(shù)據(jù)庫中

* @Index(unique = true)

* @Unique 用于標志列的值的唯一性。

*/


五.GreenDao對外提供的核心類簡介:

1,DaoMaster

DaoMaster保存數(shù)據(jù)庫對象(SQLiteDatabase)并管理特定模式的Dao類。它具有靜態(tài)方法來創(chuàng)建表或?qū)⑺麄儎h除。其內(nèi)部類OpenHelper和DevOpenHelper是在SQLite數(shù)據(jù)庫中創(chuàng)建模式的SQLiteOpenHelper實現(xiàn)。

2,DaoSession

管理特定模式的所有可用Dao對象,您可以使用其中一個getter方法獲取。DaoSession還為實體提供了一些通用的持久性方法,如插入,加載,更新,刷新和刪除。最后,DaoSession對象也跟蹤一個身份范圍。

3,Dao層

數(shù)據(jù)訪問對象(Dao)持續(xù)存在并查詢實體。對于每個實體,GreenDao生成一個Dao,它比DaoSession有更多的持久化方法,例如:count,loadAll和insertInTx。

4,實體

持久對象,通常實體是使用標準Java屬性(如POJO或JavaBean)來表示數(shù)據(jù)庫的對象。

1、DevOpenHelper:創(chuàng)建SQLite數(shù)據(jù)庫的SQLiteOpenHelper的具體實現(xiàn)。

2、DaoMaster:GreenDao的頂級對象,作為數(shù)據(jù)庫對象、用于創(chuàng)建表和刪除表。

3、DaoSession:管理所有的Dao對象,Dao對象中存在著增刪改查等API。




六.GreenDao的使用:

1,DaoMaster

DaoMaster保存數(shù)據(jù)庫對象(SQLiteDatabase)并管理特定模式的Dao類。它具有靜態(tài)方法來創(chuàng)建表或?qū)⑺麄儎h除。其內(nèi)部類OpenHelper和DevOpenHelper是在SQLite數(shù)據(jù)庫中創(chuàng)建模式的SQLiteOpenHelper實現(xiàn)。

2,DaoSession

管理特定模式的所有可用Dao對象,您可以使用其中一個getter方法獲取。DaoSession還為實體提供了一些通用的持久性方法,如插入,加載,更新,刷新和刪除。最后,DaoSession對象也跟蹤一個身份范圍。

3,Dao層

數(shù)據(jù)訪問對象(Dao)持續(xù)存在并查詢實體。對于每個實體,GreenDao生成一個Dao,它比DaoSession有更多的持久化方法,例如:count,loadAll和insertInTx。

4,實體

持久對象,通常實體是使用標準Java屬性(如POJO或JavaBean)來表示數(shù)據(jù)庫的對象。

1、DevOpenHelper:創(chuàng)建SQLite數(shù)據(jù)庫的SQLiteOpenHelper的具體實現(xiàn)。

2、DaoMaster:GreenDao的頂級對象,作為數(shù)據(jù)庫對象、用于創(chuàng)建表和刪除表。

3、DaoSession:管理所有的Dao對象,Dao對象中存在著增刪改查等API。




七.完整使用步驟(工具類)

public class MyApp extends Application {

? ? private static MyApp myApp;

? ? @Override

? ? public void onCreate() {

? ? ? ? super.onCreate();

? ? ? ? myApp = this;

? ? }

? ? public static MyApp getMyApp() {

? ? ? ? return myApp;

? ? }

}

? ? <application

? ? ? ? android:name=".MyApp"

? ? ? ? ...>


? ? </application>

public class MyDatabaseHelper {

? ? public StudentDao studentDao;//Dao操作類

? ? private static MyDatabaseHelper myDatabaseHelper;

? ? //創(chuàng)建生成數(shù)據(jù)庫

? ? private MyDatabaseHelper(){

? ? ? ? //1.創(chuàng)建數(shù)據(jù)庫

? ? ? ? DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApp.getMyApp(), "student.db");

? ? ? ? //2.獲取讀寫對象

? ? ? ? DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());

? ? ? ? //3.獲取管理器類

? ? ? ? DaoSession daoSession = daoMaster.newSession();

? ? ? ? //4.獲取表對象

? ? ? ? studentDao = daoSession.getStudentDao();

? ? }

? ? public static MyDatabaseHelper getMyDatabaseHelper() {

? ? ? ? if(myDatabaseHelper == null){

? ? ? ? ? ? synchronized (MyDatabaseHelper.class){

? ? ? ? ? ? ? ? if (myDatabaseHelper ==null){

? ? ? ? ? ? ? ? ? ? myDatabaseHelper = new MyDatabaseHelper();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return myDatabaseHelper;

? ? }

。。。增刪改查操作。。。

}


八.GreenDao的增刪改查

1、增 insert

2、刪 deleteByKey

3、改 update

4、查 loadAll

? ? //插入

? ? public void insertAll(List<Student> list){

? ? ? ? studentDao.insertInTx(list);

? ? }

? ? public void insert(Student student){

? ? ? ? studentDao.insert(student);

? ? }

? ? public void insert(Student student){

? ? ? ? studentDao.insertOrReplace(student);

? ? }

? ? //刪除

? ? public void deleteAll(){

? ? ? ? studentDao.deleteAll();

? ? }

? ? public void delete(Student student){

? ? ? ? studentDao.delete(student);

? ? }

? ? //更改

? ? public void updateAll(List<Student> list){

? ? ? ? studentDao.updateInTx(list);

? ? }

? ? public void update(Student student){

? ? ? ? studentDao.update(student);

? ? }

? ? //查詢

? ? public List<Student> queryAll(){

? ? ? ? return? studentDao.queryBuilder().list();

? ? }

? ? public List<Student> queryStudent(Student student){

? ? ? ? return? studentDao.queryBuilder().where(StudentDao.Properties.Name.eq(student.getName())).list();

? ? }

? ? public List<Student> queryStudent2(Student student){

? ? ? ? return? studentDao.queryBuilder().where(StudentDao.Properties.Name.eq(student.getName()),StudentDao.Properties.Age.gt(student.getAge())).list();

? ? }

? ? public List<Student> queryPage(int page,int count){

? ? ? ? return? studentDao.queryBuilder().offset(page*count).limit(count).list();

? ? }

public StudentDao query(PersonInfor studentDao){

StudentDao oldStudentDao = studentDao.queryBuilder().where(StudentDao.Properties.Id.eq(studentDao.getId())).build().unique();

? ? ? ? return oldStudentDao;

? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容