一.GreenDao的概述以及特點:
基于對象關系的映射方式來操作數據庫的框架倒淫,提供一個接口通過操作對象的方式操作數據庫
適用于 Android 的ORM 框架洲敢,現在市面上主流的框架有 OrmLite、SugarORM劈愚、Active Android瞳遍、Realm 與 GreenDAO。
GreenDAO是一種Android數據庫ORM(對象映射關系(Object Relation Mapping))框架菌羽,與OrmLite掠械、ActiveOrm、LitePal等數據庫相比注祖,單位時間內可以插入猾蒂、更新和查詢更多的數據,而且提供了大量的靈活通用接口是晨。
1肚菠,通常我們在使用GreenDao的時候,我們只需定義數據模型罩缴,GreenDao框架將創(chuàng)建數據對象(實體)和DAO(數據訪問對象)蚊逢,能夠節(jié)省部分代碼层扶。
2,不向性能妥協烙荷,使用了GreenDao镜会,大多數實體可以以每秒幾千個實體的速率進行插入,更新和加載终抽。
3戳表,GreenDao支持加密數據庫來保護敏感數據。
4昼伴,微小的依賴庫匾旭,GreenDao的關鍵依賴庫大小不超過100kb.
5,如果需要亩码,實體是可以被激活的季率。而活動實體可以透明的解析關系(我們要做的只是調用getter即可),并且有更新描沟、刪除和刷新方法飒泻,以便訪問持久性功能。
6吏廉,GreenDao允許您將協議緩沖區(qū)(protobuf)對象直接保存到數據庫中泞遗。如果您通過protobuf通話到您的服務器,則不需要另一個映射席覆。常規(guī)實體的所有持久性操作都可以用于protobuf對象史辙。所以,相信這是GreenDao的獨特之處佩伤。
7聊倔,自動生成代碼,我們無需關注實體類以及Dao,因為GreenDao已經幫我們生成了生巡。
8耙蔑,開源 有興趣的同學可以查看源碼,深入去了解機制孤荣。
二.GreenDao配置依賴:
官網:
https://github.com/greenrobot/greenDAO/
配置:
//1.工程配置:添加插件 更好支持GreenDao
buildscript {
? ? repositories {
? ? ? ? jcenter()
? ? ? ? mavenCentral() // 添加的代碼
? ? }
? ? dependencies {
? ? ? ? classpath 'com.android.tools.build:gradle:2.3.3'
? ? ? ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
? ? }
}
//2.項目配置:添加插件
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
//3.項目配置:添加依賴
dependencies {
? ? //greendao
? ? implementation 'org.greenrobot:greendao:3.2.2' // add library
}
//4.初始化GreenDao配置
greendao{
? ? ? ? schemaVersion 1 //數據庫版本號
? ? ? ? daoPackage 'com.example.lizhengjun.dao'? //數據庫全路徑
? ? ? ? targetGenDir 'src/main/java'? //存放位置
? ? }
schemaVersion--> 指定數據庫schema版本號甸陌,遷移等操作會用到;
daoPackage --> dao的包名,包名默認是entity所在的包盐股;
targetGenDir --> 生成數據庫文件的目錄;
三.GreenDao的使用思路:
//1钱豁、配置文件中的設置
// 在根工程中完成內容配置:
buildscript {
? ? repositories {
? ? ? ? jcenter()
? ? ? ? mavenCentral() // 添加的代碼
? ? }
? ? dependencies {
? ? ? ? classpath 'com.android.tools.build:gradle:2.3.3'
? ? ? ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
? ? }
}
//在子工程中完成內容配置:
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
dependencies {
? ? compile 'org.greenrobot:greendao:3.2.2' // add library
}
2、設置Green中的DaoMaster/DaoSession/實體類Dao生成路徑
//自定義生成路徑 daoPackage 'a.b.c'
greendao {
? ? schemaVersion 1
? ? daoPackage 'com.example.lizhengjun.dao'
? ? targetGenDir 'src/main/java'
}
3疯汁、設置實體類
@Entity
public class Student {
? ? @Id(autoincrement = true)
? ? private Long id;
? ? @Property
? ? @NotNull
? ? private String name;
? ? @Property
? ? private int age;
}
4牲尺、文件生成
Build - > ReBuild Project
5、在Application類中完成內容配置(或者使用工具類)
Application有自己的生命周期幌蚊,OnCreate方法必須首先被調用
①本類對象的獲取
②DaoMaster谤碳、DaoSession對象的獲取
③提供方法凛澎,獲取DaoSession對象
需要注意:必須在AndroidManifest.xml文件完成配置
<application
? ? ? ? android:name=".App">
</application>
6、獲取實體類Dao對象
XXXDao xxxdao =? App.getInstance().getDaoSession().getXXXDao();
xxxdao.增刪改查();
四.GreenDao的注解:
@Entity 標識實體類估蹄,greenDAO會映射成sqlite的一個表,表名為實體類名的大寫形式
@Id 標識主鍵沫换,該字段的類型為long或Long類型臭蚁,autoincrement設置是否自動增長
@Property? ? ? 標識該屬性在表中對應的列名稱, nameInDb設置名稱
@Transient? ? ? 標識該屬性將不會映射到表中,也就是沒有這列
@NotNull? ? ? ? 設置表中當前列的值不可為空
@Convert? ? ? ? 指定自定義類型(@linkPropertyConverter)
@Generated 運行所產生的構造函數或者方法讯赏,被此標注的代碼可以變更或者下次運行時清除
@Index? ? 使用@Index作為一個屬性來創(chuàng)建一個索引司忱;
@JoinEntity? ? 定義表連接關系
@JoinProperty? ? ? ? 定義名稱和引用名稱屬性關系
@Keep? ? 注解的代碼段在GreenDao下次運行時保持不變
@OrderBy? ? ? ? 指定排序方式
@ToMany? ? ? ? 定義與多個實體對象的關系
@ToOne? 定義與另一個實體(一個實體對象)的關系
@Unique 向數據庫列添加了一個唯一的約束
/**
* @Entity
* @Id(autoincrement = true)? 標志主鍵
* @NotNull 標志這個字段不能是null
* @Property(nameInDb = "User")
* @Transient 表示不存儲在數據庫中
* @Index(unique = true)
* @Unique 用于標志列的值的唯一性肄满。
*/
五.GreenDao對外提供的核心類簡介:
1,DaoMaster
DaoMaster保存數據庫對象(SQLiteDatabase)并管理特定模式的Dao類。它具有靜態(tài)方法來創(chuàng)建表或將他們刪除扒怖。其內部類OpenHelper和DevOpenHelper是在SQLite數據庫中創(chuàng)建模式的SQLiteOpenHelper實現。
2狐蜕,DaoSession
管理特定模式的所有可用Dao對象煞聪,您可以使用其中一個getter方法獲取。DaoSession還為實體提供了一些通用的持久性方法膊夹,如插入衬浑,加載,更新放刨,刷新和刪除工秩。最后,DaoSession對象也跟蹤一個身份范圍进统。
3助币,Dao層
數據訪問對象(Dao)持續(xù)存在并查詢實體。對于每個實體螟碎,GreenDao生成一個Dao,它比DaoSession有更多的持久化方法眉菱,例如:count,loadAll和insertInTx。
4抚芦,實體
持久對象倍谜,通常實體是使用標準Java屬性(如POJO或JavaBean)來表示數據庫的對象。
1叉抡、DevOpenHelper:創(chuàng)建SQLite數據庫的SQLiteOpenHelper的具體實現尔崔。
2、DaoMaster:GreenDao的頂級對象褥民,作為數據庫對象季春、用于創(chuàng)建表和刪除表。
3消返、DaoSession:管理所有的Dao對象载弄,Dao對象中存在著增刪改查等API耘拇。
六.GreenDao的使用:
1,DaoMaster
DaoMaster保存數據庫對象(SQLiteDatabase)并管理特定模式的Dao類宇攻。它具有靜態(tài)方法來創(chuàng)建表或將他們刪除惫叛。其內部類OpenHelper和DevOpenHelper是在SQLite數據庫中創(chuàng)建模式的SQLiteOpenHelper實現。
2逞刷,DaoSession
管理特定模式的所有可用Dao對象嘉涌,您可以使用其中一個getter方法獲取。DaoSession還為實體提供了一些通用的持久性方法夸浅,如插入仑最,加載,更新帆喇,刷新和刪除警医。最后,DaoSession對象也跟蹤一個身份范圍坯钦。
3预皇,Dao層
數據訪問對象(Dao)持續(xù)存在并查詢實體。對于每個實體婉刀,GreenDao生成一個Dao,它比DaoSession有更多的持久化方法深啤,例如:count,loadAll和insertInTx。
4路星,實體
持久對象溯街,通常實體是使用標準Java屬性(如POJO或JavaBean)來表示數據庫的對象。
1洋丐、DevOpenHelper:創(chuàng)建SQLite數據庫的SQLiteOpenHelper的具體實現呈昔。
2、DaoMaster:GreenDao的頂級對象友绝,作為數據庫對象堤尾、用于創(chuàng)建表和刪除表。
3迁客、DaoSession:管理所有的Dao對象郭宝,Dao對象中存在著增刪改查等API。
七.完整使用步驟(工具類)
public class MyApp extends Application {
? ? private static MyApp myApp;
? ? @Override
? ? public void onCreate() {
? ? ? ? super.onCreate();
? ? ? ? myApp = this;
? ? }
? ? public static MyApp getMyApp() {
? ? ? ? return myApp;
? ? }
}
? ? <application
? ? ? ? android:name=".MyApp"
? ? ? ? ...>
? ? </application>
public class MyDatabaseHelper {
? ? public StudentDao studentDao;//Dao操作類
? ? private static MyDatabaseHelper myDatabaseHelper;
? ? //創(chuàng)建生成數據庫
? ? private MyDatabaseHelper(){
? ? ? ? //1.創(chuàng)建數據庫
? ? ? ? DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApp.getMyApp(), "student.db");
? ? ? ? //2.獲取讀寫對象
? ? ? ? DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
? ? ? ? //3.獲取管理器類
? ? ? ? DaoSession daoSession = daoMaster.newSession();
? ? ? ? //4.獲取表對象
? ? ? ? studentDao = daoSession.getStudentDao();
? ? }
? ? public static MyDatabaseHelper getMyDatabaseHelper() {
? ? ? ? if(myDatabaseHelper == null){
? ? ? ? ? ? synchronized (MyDatabaseHelper.class){
? ? ? ? ? ? ? ? if (myDatabaseHelper ==null){
? ? ? ? ? ? ? ? ? ? myDatabaseHelper = new MyDatabaseHelper();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return myDatabaseHelper;
? ? }
掷漱。粘室。。增刪改查操作卜范。衔统。。
}
八.GreenDao的增刪改查
1、增 insert
2锦爵、刪 deleteByKey
3舱殿、改 update
4、查 loadAll
? ? //插入
? ? public void insertAll(List<Student> list){
? ? ? ? studentDao.insertInTx(list);
? ? }
? ? public void insert(Student student){
? ? ? ? studentDao.insert(student);
? ? }
? ? public void insert(Student student){
? ? ? ? studentDao.insertOrReplace(student);
? ? }
? ? //刪除
? ? public void deleteAll(){
? ? ? ? studentDao.deleteAll();
? ? }
? ? public void delete(Student student){
? ? ? ? studentDao.delete(student);
? ? }
? ? //更改
? ? public void updateAll(List<Student> list){
? ? ? ? studentDao.updateInTx(list);
? ? }
? ? public void update(Student student){
? ? ? ? studentDao.update(student);
? ? }
? ? //查詢
? ? public List<Student> queryAll(){
? ? ? ? return? studentDao.queryBuilder().list();
? ? }
? ? public List<Student> queryStudent(Student student){
? ? ? ? return? studentDao.queryBuilder().where(StudentDao.Properties.Name.eq(student.getName())).list();
? ? }
? ? public List<Student> queryStudent2(Student student){
? ? ? ? return? studentDao.queryBuilder().where(StudentDao.Properties.Name.eq(student.getName()),StudentDao.Properties.Age.gt(student.getAge())).list();
? ? }
? ? public List<Student> queryPage(int page,int count){
? ? ? ? return? studentDao.queryBuilder().offset(page*count).limit(count).list();
? ? }
public StudentDao query(PersonInfor studentDao){
StudentDao oldStudentDao = studentDao.queryBuilder().where(StudentDao.Properties.Id.eq(studentDao.getId())).build().unique();
? ? ? ? return oldStudentDao险掀;
? ? }