GreenDao
**GitHub地址: **https://github.com/greenrobot/greenDAO/
**文檔地址: **http://greenrobot.org/greendao/documentation/
市面上面主流的數(shù)據(jù)庫
- GreenDao
- Reaml
- Ormlite
- Litepal
- SugarORM
Orm的優(yōu)勢
orm對象關(guān)系映射,把數(shù)據(jù)映射成對象
- 讓業(yè)務(wù)代碼訪問對象,而不是數(shù)據(jù)庫
- 隱藏了面向?qū)ο蟮倪壿媠ql的查詢詳情
- 無需處理數(shù)據(jù)庫的實現(xiàn)
ORM相比android自身提供的api
- 需拼裝sql
- 需要自己寫操作數(shù)據(jù)庫的常規(guī)的代碼
- 沒有實現(xiàn)級聯(lián)查詢
- 不能把數(shù)據(jù)映射成對象
GreenDao
如何選擇一個開源框架
- 性能
- 文檔的完整性
- 流行因素
- 使用是否容易,學(xué)習(xí)成本
- 擴(kuò)展性如何
greendao綜合來看,比較滿足
AndroidStudio接入
文檔地址:http://greenrobot.org/greendao/documentation/introduction/
//項目的build.grade
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
}
//appmodule配置 Android模塊下面
/*針對greenDao的一些配置*/
greendao {
schemaVersion 1 //數(shù)據(jù)庫版本號
daoPackage 'com.rao.demo.activity.home.greendao.dao' //自動生成的工具類的包名
targetGenDir 'src/main/java' //路徑
}
//添加依賴
compile 'org.greenrobot:greendao:3.2.2'
gradle配置,基礎(chǔ)屬性
文檔地址:http://greenrobot.org/greendao/documentation/modelling-entities/
// In the build.gradle file of your app project:
android {
....
}
greendao {
schemaVersion 2 //定義版本
daoPackage //生成相文件存放路徑
targetGenDir //生產(chǎn)資源存放路徑
generateTests //是否生成單元測試
...
}
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "USERNAME")
private String name;
@NotNull
private int repos;
@Transient
private int tempUsageCount;
...
}
Session操作實體的類
地址:http://greenrobot.org/greendao/documentation/sessions/
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
查詢的操作
地址:http://greenrobot.org/greendao/documentation/queries/
List<User> joes = userDao.queryBuilder()
.where(Properties.FirstName.eq("Joe"))
.orderAsc(Properties.LastName)
.list();
根據(jù)文檔和dmeo案例,來看,簡單入門