GreenDao3.2的簡單使用


Android -- GreenDao3.2的簡單使用
http://www.cnblogs.com/wjtaigwh/p/6394288.html

https://github.com/greenrobot/greendao

 

GreenDao是使用ORM(Object RelationShop Mapping)對象關系映射,就是通過GreenDao將數據庫和Bean關聯起來有以下優點:

  • 存取速度快

  • 支持數據庫加密

  • 輕量級

  • 激活實體

  • 支持緩存

  • 代碼自動生成

首先要在Project中的build中添加如下代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
buildscript {
     repositories {
         jcenter()
     }
     dependencies {
         classpath  'com.android.tools.build:gradle:2.2.3'
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
         classpath  'org.greenrobot:greendao-gradle-plugin:3.2.1'
     }
}
 
allprojects {
     repositories {
         jcenter()
     }
}
 
task clean(type: Delete) {
     delete rootProject.buildDir
}

再在Module中的build添加引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
apply plugin:  'com.android.application'
//使用greendao
apply plugin:  'org.greenrobot.greendao'
android {
     compileSdkVersion  25
     buildToolsVersion  "25.0.2"
 
     defaultConfig {
         applicationId  "com.qianmo.greendaotest"
         minSdkVersion  15
         targetSdkVersion  25
         versionCode  1
         versionName  "1.0"
 
         testInstrumentationRunner  "android.support.test.runner.AndroidJUnitRunner"
 
     }
     //greendao配置
     greendao {
         //版本號,升級時可配置
         schemaVersion  1
//        daoPackage 'com.qianmo.greendaotest.gen'
//        targetGenDir 'src/main/java'
     }
 
     buildTypes {
         release {
             minifyEnabled  false
             proguardFiles getDefaultProguardFile( 'proguard-android.txt' ),  'proguard-rules.pro'
         }
     }
}
 
dependencies {
     compile fileTree(dir:  'libs' , include: [ '*.jar' ])
     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:25.1.1'
     compile  'com.android.support:design:25.1.1'
     compile  'org.greenrobot:greendao:3.2.0'  //greendao依賴
 
     testCompile  'junit:junit:4.12'
}

  這樣就配置成功了,接着是簡單的使用。

 

  • @Entity:告訴GreenDao該對象為實體,只有被@Entity注釋的Bean類才能被dao類操作
  • @Id:對象的Id,使用Long類型作為EntityId,否則會報錯。(autoincrement = true)表示主鍵會自增,如果false就會使用舊值
  • @Property:可以自定義字段名,注意外鍵不能使用該屬性
  • @NotNull:屬性不能為空
  • @Transient:使用該注釋的屬性不會被存入數據庫的字段中
  • @Unique:該屬性值必須在數據庫中是唯一值
  • @Generated:編譯后自動生成的構造函數、方法等的注釋,提示構造函數、方法等不能被修改
public  class  BaseApplication  extends  Application {
     private  static  DaoSession daoSession;
 
     @Override
     public  void  onCreate() {
         super .onCreate();
 
         //配置數據庫
         setupDatabase();
     }
 
     /**
      * 配置數據庫
      */
     private  void  setupDatabase() {
         //創建數據庫shop.db
         DaoMaster.DevOpenHelper helper =  new  DaoMaster.DevOpenHelper( this "shop.db" null );
         //獲取可寫數據庫
         SQLiteDatabase db = helper.getWritableDatabase();
         //獲取數據庫對象
         DaoMaster daoMaster =  new  DaoMaster(db);
         //獲取dao對象管理者
         daoSession = daoMaster.newSession();
     }
 
     public  static  DaoSession getDaoInstant() {
         return  daoSession;
     }
 
}

這里我們需要創建一個Application,在代碼中初始化數據庫的基本數據,這里要講解這下面這三個類

  • DevOpenHelper:創建SQLite數據庫的SQLiteOpenHelper的具體實現

  • DaoMaster:GreenDao的頂級對象,作為數據庫對象、用於創建表和刪除表

  • DaoSession:管理所有的Dao對象,Dao對象中存在着增刪改查等API

這里注意一下我們要編譯一下我們的工程(ctrl+F9),因為上面三個類是運行時創建的,還有相應的Shop中的set和get方法及構造函數

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM