GreenDAO 是一款開源的面向 Android 的輕便、快捷的 ORM 框架,將 Java 對象映射到 SQLite 數(shù)據(jù)庫中,我們操作數(shù)據(jù)庫的時(shí)候,不在需要編寫復(fù)雜的 SQL語句, 在性能方面,GreenDAO 針對 Android 進(jìn)行了高度優(yōu)化, 最小的內(nèi)存開銷 、依賴體積小 同時(shí)還是支持?jǐn)?shù)據(jù)庫加密。關(guān)于GreenDao的基本的介紹,很多的博客都有寫,我這里就不做過多的講解了。這里簡要的介紹一下他的基本的用法,喜歡的朋友可以看看。
1.設(shè)置倉庫與插件---首先在(Project:build.gradle)添加,紅色的文字的部分
buildscript {
? ? repositories {
? ? ? ? google()
? ? ? ? jcenter()
? ? ? ? mavenCentral() // 添加這個(gè)倉庫? ? }
? ? dependencies {
? ? ? ? classpath 'com.android.tools.build:gradle:3.0.1'? ? ? ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // 添加插件路徑? ? ? ? // NOTE: Do not place your application dependencies here; they belong? ? ? ? // in the individual module build.gradle files? ? }
}
allprojects {
? ? repositories {
? ? ? ? google()
? ? ? ? jcenter()
? ? }
}
task clean(type: Delete) {
? ? delete rootProject.buildDir}
2.配置依賴 ( Module:app build.gradle )---同理添加,紅色的文字的部分
apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao' //添加插件的調(diào)用android {
? ? compileSdkVersion 26? ? defaultConfig {
? ? ? ? applicationId "com.example.administrator.mygreendao"? ? ? ? minSdkVersion 15? ? ? ? targetSdkVersion 26? ? ? ? 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? ? ? ? ? ? ? ? ? //數(shù)據(jù)庫版本號? ? ? ? targetGenDir "src/main/java"? ? //生成代碼的根路徑? ? ? ? daoPackage "com.example.administrator.mygreendao.db"? ? ? ? ? //在根路徑下生成類的包名? ? }}
dependencies {
? ? implementation fileTree(dir: 'libs', include: ['*.jar'])
? ? implementation 'com.android.support:appcompat-v7:26.1.0'? ? implementation 'com.android.support.constraint:constraint-layout:1.1.0'? ? testImplementation 'junit:junit:4.12'? ? androidTestImplementation 'com.android.support.test:runner:1.0.2'? ? androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'? ? implementation 'org.greenrobot:greendao:3.2.2' //添加庫的依賴
3, 我們寫一個(gè)簡單的實(shí)體類(User),測試一下
@Entitypublic classUser{
? ? @Id
? ? private long id;
? ? private String name;
? ? private int age;
? ? //此處省略了getter,setter 方法
}
4.點(diǎn)擊小錘子的圖標(biāo)編譯一下
這個(gè)時(shí)候可以看到左邊和我們的代碼區(qū)域都出現(xiàn)了變化
6.初始化 GreenDao ( 通常初始化代碼寫在我們的 Application 類中)
package com.example.administrator.mygreendao;import android.app.Application;import android.database.sqlite.SQLiteDatabase;import com.example.administrator.mygreendao.db.DaoMaster;import com.example.administrator.mygreendao.db.DaoSession;/** * Created by Administrator on 2018/5/22. * 初始化GreenDao */public class MyAppextends Application {private DaoSessiondaoSession;? ? @Override? ? public void onCreate() {super.onCreate();? ? ? ? initGreenDao();? ? }/**? ? * 初始化 GreenDao? ? */? ? private void initGreenDao() {//創(chuàng)建數(shù)據(jù)庫? ? ? ? DaoMaster.DevOpenHelper helper =new DaoMaster.DevOpenHelper(this, "test.db");//表名? ? ? ? SQLiteDatabase db = helper.getWritableDatabase();? ? ? ? DaoMaster daoMaster =new DaoMaster(db);? ? ? ? daoSession = daoMaster.newSession();? ? }/**? ? * 獲取DaoSession? ? */? ? public DaoSessiongetDaoSession() {return daoSession;? ? }}
7.使用
package com.example.administrator.mygreendao;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.example.administrator.mygreendao.db.DaoSession;import com.example.administrator.mygreendao.db.UserDao;import org.greenrobot.greendao.query.QueryBuilder;import java.util.List;/** * GreenDao的基本用法實(shí)踐 */public class MainActivityextends AppCompatActivity {private UserDaouserDao;? ? private Useruser;? ? @Override? ? protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);? ? ? ? setContentView(R.layout.activity_main);? ? ? ? init();? ? ? ? initDate();? ? ? ? initChaRu();? ? ? ? deleted();? ? ? ? update();? ? ? ? query();? ? ? ? query2();? ? ? ? query3();? ? ? ? query4();? ? }//查詢年齡大于10的用戶? ? private Listquery4() {? ? ? ? QueryBuilder builder =userDao.queryBuilder();? ? ? ? return builder.where(UserDao.Properties.Age.gt(10)).build().list();? ? }//根據(jù)id查詢? ? private Userquery2() {return userDao.loadByRowId(1);? ? }private Listquery3() {return userDao.queryRaw("where AGE>?", "10");//查詢年齡大于10的用戶? ? }//查詢所有記錄? ? private Listquery() {return userDao.loadAll();? ? }//更新記錄? ? private void update() {userDao.update(user);? ? }//獲取UserDao? ? private void init() {? ? ? ? MyApp myApp = (MyApp) getApplication();? ? ? ? DaoSession daoSession = myApp.getDaoSession();? ? ? ? userDao = daoSession.getUserDao();? ? }//增加? ? private void initDate() {user =new User();? ? ? ? user.setId(1);? ? ? ? user.setName("小明");? ? ? ? user.setAge(16);? ? ? ? userDao.insert(user);? ? }//插入和替換? ? private void initChaRu() {userDao.insertOrReplace(user);? ? }//刪除記錄? ? private void deleted() {userDao.delete(user);? ? }}
好啦這樣就基本完成啦
六、注解詳解
@Entity
表明這個(gè)實(shí)體類會在數(shù)據(jù)庫中生成一個(gè)與之相對應(yīng)的表
@Id
對應(yīng)數(shù)據(jù)表中的 Id 字段
@Index
使用@Index作為一個(gè)屬性來創(chuàng)建一個(gè)索引,默認(rèn)是使用字段名
@Property
設(shè)置一個(gè)非默認(rèn)關(guān)系映射所對應(yīng)的列名,默認(rèn)是使用字段名,例如:@Property(nameInDb = “userName”)
@NotNull
設(shè)置數(shù)據(jù)庫表當(dāng)前列不能為空
@Transient
添加此標(biāo)記后不會生成數(shù)據(jù)庫表的列
@Unique
表名該屬性在數(shù)據(jù)庫中只能有唯一值
@ToOne
表示一對一關(guān)系
關(guān)于更加詳細(xì)的博客可以
參考SpeedyStone博客
地址:https://blog.csdn.net/speedystone/article/details/72769793