天天看點

Android GreenDao3.2配置及使用詳解1.首先在Androidstudio 項目的build.grad 檔案中引入一下檔案 2.在module的build.gradle檔案中添加greenDAO的插件,并引入相關類庫 3 .在項目包下建立實體類 4.建立dbmanager類初始化資料庫5.擷取Dao6.在dbmanager中實作增删該查 ,添加資料

1.首先在Androidstudio 項目的build.grad 檔案中引入一下檔案

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
           

2.在module的build.gradle檔案中添加greenDAO的插件,并引入相關類庫
      

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
apply plugin: 'android-apt'
android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.mvp.observer"
        minSdkVersion 15
        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
        targetGenDir 'src/main/java'
    }
}

dependencies {
    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'
    testCompile 'junit:junit:4.12'
    compile 'org.greenrobot:greendao:3.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.1'

}
           

3 .在項目包下建立實體類      

@Entity
public class User {

    @Id
    private Long id;
    @Property(nameInDb = "USERNAME")
    private String username;
    @Property(nameInDb = "NICKNAME")
    private String nickname;
    @Generated(hash = 523935516)
    public User(Long id, String username, String nickname) {
        this.id = id;
        this.username = username;
        this.nickname = nickname;
    }
    @Generated(hash = 586692638)
    public User() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUsername() {
        return this.username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getNickname() {
        return this.nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
}
           

@Entity表示這個實體類會在資料庫中生成對應的表,

@Id表示該字段是id,注意該字段的資料類型為包裝類型Long

@Property則表示該屬性将作為表的一個字段,其中nameInDb看名字就知道這個屬性在資料庫中對應的資料名稱

運作将項目進行編譯,編譯成功之後系統會幫助我們生成相應的構造方法和get/set方法,并且還會在我們的包下生成DaoMaster和DaoSession。那麼這裡常用的注解除了這幾個之外,還有一個較常用的就是@Transient,該注解表示這個屬性将不會作為資料表中的一個字段。就是這麼簡單。另外還有一些比如@NotNull表示該字段不可以為空,@Unique表示該字段唯一。這裡的注解還是挺多的,小夥伴們有興趣可以自行研究

4.建立dbmanager類初始化資料庫

DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "lenve.db", null);  
DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());  
DaoSession daoSession = daoMaster.newSession(); 

           

5.擷取Dao

6.在dbmanager中實作增删該查 ,添加資料

User user = new User(null, "zhangsan" + random.nextInt(9999),"張三");  
userDao.insert(user); 
\\其他大家檢視Api實作即可
           

項目下載下傳位址:https://github.com/androidDongdong/MvpAndObserver