greenDAO是什么
greenDAO是一個開源的Android ORM,使SQLite數(shù)據(jù)庫的開發(fā)再次有趣膀估。它減輕開發(fā)人員處理低級數(shù)據(jù)庫需求谎亩,同時節(jié)省開發(fā)時間。 SQLite是一個令人敬畏的嵌入式關(guān)系數(shù)據(jù)庫轴或。不過,編寫SQL和解析查詢結(jié)果是相當乏味和耗時的任務(wù)仰禀。通過將Java對象映射到數(shù)據(jù)庫表(稱為ORM照雁,“對象/關(guān)系映射”),greenDAO可以將它們從這些映射中釋放出來答恶。這樣饺蚊,可以使用簡單的面向?qū)ο蟮腁PI來存儲,更新悬嗓,刪除和查詢Java對象污呼。
greenDAO的功能和優(yōu)勢
- 最高性能(可能是Android最快的ORM);并且開源
- 易于使用的功能強大的API涵蓋
- 最小的內(nèi)存消耗
- 數(shù)據(jù)庫加密,greenDAO支持SQLCipher烫扼,以保護用戶的數(shù)據(jù)安全
- 強大的社區(qū),官網(wǎng)首頁:http://greenrobot.org/greendao/ 碍庵,github首頁:https://github.com/greenrobot/greenDAO
尤其在性能方面映企,greenDAO 遠遠高于同類的其他ORM框架,測試結(jié)果參考至官網(wǎng):http://greenrobot.org/greendao/features/
如何將greenDAO添加到工程
參照搞其github首頁的readme静浴,首先是整個工程項目下的build.gradle的配置堰氓;然后是單獨的Module(如默認的:app)下的build.gradle的配置
// In your root build.gradle file:
buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
}
}
// In your app projects build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
dependencies {
compile 'org.greenrobot:greendao:3.2.2' // add library
}
遇到網(wǎng)速很慢的情況下,可以換阿里云服務(wù)器苹享。
Project build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
// jcenter()
// mavenCentral() // add repository
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}//阿里云服務(wù)器
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// jcenter()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}//阿里云服務(wù)器
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app module build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
// jcenter()
// mavenCentral() // add repository
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}//阿里云服務(wù)器
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// jcenter()
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}//阿里云服務(wù)器
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
實例講解
我們在新建的工程的app module下面先新建一個實體類UserEntity.java:
里面有name双絮,password浴麻,age這3項屬性,注意在public class的上方囤攀,我們加有@Entity注解標簽软免,表明這是一個實體類。然后我們make project焚挠,在build/generated/source下面greenDAO會自動幫我們生成3個文件
同時回過頭我們也發(fā)現(xiàn)我們剛剛的實體類也發(fā)生了變化:新增了getter膏萧,setter以及構(gòu)造函數(shù)
我們可以通過在Module的build.gradle中新增配置來設(shè)置greenDAO幫我們自動生成文件的路徑,而不是在build/generated/source下面蝌衔,因為這樣找起來比較麻煩:
greendao {
schemaVersion 1
daoPackage 'com.example.greendaodemo.greendao.gen'
targetGenDir 'src/main/java'
}
- schemaVersion---->指定數(shù)據(jù)庫schema版本號榛泛,遷移等操作會用到
- daoPackage-------->通過gradle插件生成的數(shù)據(jù)庫相關(guān)文件的包名,默認為你的entity所在的包名
- targetGenDir-------->這就是我們上面說到的自定義生成數(shù)據(jù)庫文件的目錄了噩斟,可以將生成的文件放到我們的java目錄中曹锨,而不是build中,這樣就不用額外的設(shè)置資源目錄了
再次make project你就會發(fā)現(xiàn)剃允,生成的目錄不一樣了沛简,是不是很爽?
關(guān)于greenDAO的介紹和基本配置入門就先介紹到這里硅急,請關(guān)注下一節(jié)內(nèi)容:greenDAO 3.X詳細解析(二)