Android GreenDao使用

  1. gradle添加依賴
```groovy
// In your root build.gradle file:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0' // 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
}
  1. 配置Module根目錄下的build.gradle文件
android {
  ......
  // 數(shù)據(jù)庫的版本及Dao相關(guān)設(shè)置
  greendao {
    // 數(shù)據(jù)庫schema版本,也可以理解為數(shù)據(jù)庫版本號
    schemaVersion 1000
    //  設(shè)置DaoMaster 、DaoSession、Dao包名
    daoPackage  'com.mazaiting.greendaotest.db.dao'
    //  設(shè)置DaoMaster 、DaoSession、Dao目錄
    targetGenDir 'src/main/java'
    //  設(shè)置自動生成單元測試用例
    generateTests
  }
}
  1. 新建實體類
package com.mazaiting.greendaotest.db.entry;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
/**
 * Created by mazaiting on 2017/9/7.
 */
@Entity
public class NewFriend {
  // 設(shè)置自增長,不能設(shè)置為int
  @Id(autoincrement = true)
  private long id;
  private String name;
  private int age;

}
  1. 重新編譯工程(build->Rebuild Project)。
    編譯完成后,在com.mazaiting.greendaotest.db.dao下生成如下文件:
測試1.png
  1. 使用GreenDao添加數(shù)據(jù)
  /**
   * 使用GreenDao
   */
  private void userGreenDao() {
    DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "test.db");
    DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
    DaoSession daoSession = daoMaster.newSession();

    NewFriend newFriend = new NewFriend();
    newFriend.setName("mazaiting");
    newFriend.setAge(22);
    daoSession.insert(newFriend);
  }

運行后,在"/data/data/com.mazaiting.greendaotest/databases/"路徑下生成test.db數(shù)據(jù)庫,庫中生成相對應(yīng)的實體表名(NEW_FRIEND),并將數(shù)據(jù)插入了其中。

結(jié)果.png
  1. 工具類
package com.mazaiting.greendaotest.db.dao;

import android.content.Context;

/**
 * Created by mazaiting on 2017/9/7.
 */

public class DbManager {

  private static final String DB_NAME = "test.db";

  private static DbManager mDbManager = null;
  private static DaoSession mDaoSession = null;

  private DbManager(Context context){
    DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME);
    DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
    mDaoSession = daoMaster.newSession();
    // 在控制臺打印出sql語句和查詢的值
    QueryBuilder.LOG_SQL = true;
    QueryBuilder.LOG_VALUES = true;
  }

  public static DbManager getInstance(Context context){
    if (null == mDbManager){
      synchronized (DbManager.class){
        if (null == mDbManager){
          mDbManager = new DbManager(context);
        }
      }
    }
    return mDbManager;
  }

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

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

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