GreenDao介紹

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

如何選擇一個開源框架

  1. 性能
  2. 文檔的完整性
  3. 流行因素
  4. 使用是否容易,學(xué)習(xí)成本
  5. 擴(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案例,來看,簡單入門

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容