GreenDao的使用
一、首先说一下ORM的定义?
对象-关系映射(OBJECT/RELATION MAPPING,简称ORM),是随着面向对象的软件开发方法发展而产生的。面向对象的开发方法是当今企业级应用开发环境中的主流开发方法,关系数据库是企业级应用环境中永久存放数据的主流数据存储系统。对象和关系数据是业务实体的两种表现形式,业务实体在内存中表现为对象,在数据库中表现为关系数据。内存中的对象之间存在关联和继承关系,而在数据库中,关系数据无法直接表达多对多关联和继承关系。因此,对象-关系映射(ORM)系统一般以中间件的形式存在,主要实现程序对象到关系数据库数据的映射。
二、ORM的优点
1.让业务代码访问对象,而不是数据库表
2.隐藏了面向对象的逻辑SQL查询详情
3.无需处理数据库实现
三、目前主流的ORM框架
OrmLite、SugarORM、LitePal、GreenDao
四、为什么选择GreenDao
怎么选择开源框架呢,从下面这几点来看:性能、文档健全性、流行性,多少人在用、使用简单、拓展性。
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1127/2069.html GreenDao官网中文翻译
https://github.com/greenrobot/greenDAO
Github地址
五、AndroidStudio 整体配置预览
本配置是居于GreenDao 3.2.0
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "gang.com.greendaodemo"
minSdkVersion 22
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
greendao {
schemaVersion 1
daoPackage 'gang.com.greendaodemo.greendao'
targetGenDir 'src/main/java'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'org.greenrobot:greendao:3.2.0'
}
六、源代码使用
下面重要是相关类的封装,以及一些查询语句的使用,源代码基于3.2.0
源代码地址:https://git.oschina.net/xiaogangzai/GreenDemo.git
源代码都有注释哦,很详细。需要的小伙伴请查看