Android Studio中 GreenDao 基本配置
阿新 • • 發佈:2019-01-10
一、需要在工程的build.gradle中新增依賴
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
//GreenDao3依賴
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
二、在專案(Module)的build.gradle中新增依賴
apply plugin: 'com.android.application'
//使用greendao
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.handsome.didi"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
//greendao配置
greendao {
//版本號,升級時可配置
schemaVersion 1
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs' )
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
//greendao依賴
compile 'org.greenrobot:greendao:3.2.0'
}
配置完成。
三、1、Bean實體的構造方法和get、set方法
2. DaoMaster、DaoSession、DAOS
註解:
@Entity 標識實體類,greenDAO會對映成sqlite的一個表,表名為實體類名的大寫形式
@Id 標識主鍵,該欄位的型別為long或Long型別,autoincrement設定是否自動增長
@Property 標識該屬性在表中對應的列名稱, nameInDb設定名稱
@Transient 標識該屬性將不會對映到表中,也就是沒有這列
@NotNull 設定表中當前列的值不可為空
@Convert 指定自定義型別(@linkPropertyConverter)
@Generated greenDAO執行所產生的建構函式或者方法,被此標註的程式碼可以變更或者下次執行時清除
@Index 使用@Index作為一個屬性來建立一個索引;定義多列索引(@link Entity#indexes())
@JoinEntity 定義表連線關係
@JoinProperty 定義名稱和引用名稱屬性關係
@Keep 註解的程式碼段在GreenDao下次執行時保持不變
1.註解實體類:預設禁止修改此類
2.註解其他程式碼段,預設禁止修改註解的程式碼段
@OrderBy 指定排序
@ToMany 定義與多個實體物件的關係
@ToOne 定義與另一個實體(一個實體物件)的關係
@Unique 向資料庫列添加了一個唯一的約束
- void attachEntity(T entity):
- long count():獲取資料庫中資料的數量
- // 資料刪除相關
- void delete(T entity):從資料庫中刪除給定的實體
- void deleteAll() :刪除資料庫中全部資料
- void deleteByKey(K key):從資料庫中刪除給定Key所對應的實體
- void deleteByKeyInTx(java.lang.Iterable<K> keys):使用事務操作刪除資料庫中給定的所有key所對應的實體
- void deleteByKeyInTx(K... keys):使用事務操作刪除資料庫中給定的所有key所對應的實體
- void deleteInTx(java.lang.Iterable<T> entities):使用事務操作刪除資料庫中給定實體集合中的實體
- void deleteInTx(T... entities):使用事務操作刪除資料庫中給定的實體
- // 資料插入相關
- long insert(T entity):將給定的實體插入資料庫
- void insertInTx(java.lang.Iterable<T> entities):使用事務操作,將給定的實體集合插入資料庫
- void insertInTx(java.lang.Iterable<T> entities, boolean setPrimaryKey):使用事務操作,將給定的實體集合插入資料庫,
- 並設定是否設定主鍵
- void insertInTx(T... entities):將給定的實體插入資料庫
- long insertOrReplace(T entity):將給定的實體插入資料庫,若此實體類存在,則覆蓋
- void insertOrReplaceInTx(java.lang.Iterable<T> entities):使用事務操作,將給定的實體插入資料庫,若此實體類存在,則覆蓋
- void insertOrReplaceInTx(java.lang.Iterable<T> entities, boolean setPrimaryKey):使用事務操作,將給定的實體插入資料庫,若此實體類存在,則覆蓋
- 並設定是否設定主鍵
- void insertOrReplaceInTx(T... entities):使用事務操作,將給定的實體插入資料庫,若此實體類存在,則覆蓋
- long insertWithoutSettingPk(T entity):將給定的實體插入資料庫,但不設定主鍵
- // 新增資料插入相關API
- void save(T entity):將給定的實體插入資料庫,若此實體類存在,則更新
- void saveInTx(java.lang.Iterable<T> entities):將給定的實體插入資料庫,若此實體類存在,則更新
- void saveInTx(T... entities):使用事務操作,將給定的實體插入資料庫,若此實體類存在,則更新
- // 載入相關
- T load(K key):載入給定主鍵的實體
- java.util.List<T> loadAll():載入資料庫中所有的實體
- protected java.util.List<T> loadAllAndCloseCursor(android.database.Cursor cursor) :從cursor中讀取、返回實體的列表,並關閉該cursor
- protected java.util.List<T> loadAllFromCursor(android.database.Cursor cursor):從cursor中讀取、返回實體的列表
- T loadByRowId(long rowId) :載入某一行並返回該行的實體
- protected T loadUnique(android.database.Cursor cursor) :從cursor中讀取、返回唯一實體
- protected T loadUniqueAndCloseCursor(android.database.Cursor cursor) :從cursor中讀取、返回唯一實體,並關閉該cursor
- //更新資料
- void update(T entity) :更新給定的實體
- protected void updateInsideSynchronized(T entity, DatabaseStatement stmt, boolean lock)
- protected void updateInsideSynchronized(T entity, android.database.sqlite.SQLiteStatement stmt, boolean lock)
- void updateInTx(java.lang.Iterable<T> entities) :使用事務操作,更新給定的實體
- void updateInTx(T... entities):使用事務操作,更新給定的實體