一篇好文之Android數據庫 GreenDao的徹底解析

以前在開發過程當中,數據庫基本上會使用Litepal或者SQlite本身寫,最近換新環境,公司原先使用的數據庫就是GreenDao,在各類狀況的做用下,準備瞭解下GreenDao,順便寫一篇文章記錄下GreenDao的基本使用!你們在使用GreenDao的時候遇到什麼問題,歡迎在個人公衆號aserbao給我留言,無償服務!同時,歡迎你們來加入微信羣二維碼討論羣,一塊兒討論Android開發技術!羣二維碼定時在我公衆號更新!javascript


Android開發者彙集地

本文主要從以下幾個方面進行講解php

  1. 存儲的數據庫結構
  2. GreenDao的優缺點
  3. GreenDao的使用配置
  4. 使用GreenDao實現數據的增刪改查
  5. GreenDao的註解使用
  6. GreenDao的關係處理
  7. GreenDao的升級
  8. GreenDao數據庫加密
  9. 項目地址AserbaosAndroid
  10. 總結
  11. 參考博客
    簡書暫時不支持目錄,若是想看該文章帶目錄版本,請點擊跳轉該文章的CSDN地址

我們先看一波最終的效果圖:文章最後有項目地址;css

greendao

1. 存儲的數據庫結構

學習數據庫以前,咱們先得設計本身的數據庫,很少廢話,下面是我這次學習的數據庫結構,後面全部的數據請參考這個圖進行學習:html


GreenDao關係圖.jpg

2. GreenDao的介紹

簡單的GreenDao的介紹,嫌麻煩的能夠直接跳到GreenDao使用開始看。java

什麼是GreenDao?

GreenDAO是一個開源的Android ORM(「對象/關係映射」),經過ORM(稱爲「對象/關係映射」),在咱們數據庫開發過程當中節省了開發時間!android


GreenDao的原理.png

GreenDao的官方文檔

  1. GreenDao:適用於您的SQLite數據庫的Android ORM
  2. GreenDao的github地址
  3. GreenDao的Google討論區
  4. GreenDao 加密SQLCipher for Android官方說明地址
  5. GreenDao使用文檔

GreenDao的做用?

經過GreenDao,咱們能夠更快速的操做數據庫,咱們可使用簡單的面相對象的API來存儲,更新,刪除和查詢Java對象。git

GreenDao的優缺點?

  1. 高性能,下面是官方給出的關於GreenDao,OrmLite和ActiveAndroid三種ORM解決方案的數據統計圖:github


    GreenDao性能對比圖.png
  2. 易於使用的強大API,涵蓋關係和鏈接;sql

  3. 最小的內存消耗;數據庫

  4. 小庫大小(<100KB)以保持較低的構建時間並避免65k方法限制;

  5. 數據庫加密:greenDAO支持SQLCipher,以確保用戶的數據安全;

3. GreenDao的使用

GreenDao的核心類有三個:分別是DaoMaster,DaoSession,XXXDao,這三個類都會自動建立,無需本身編寫建立!

  • DaoMaster::DaoMaster保存數據庫對象(SQLiteDatabase)並管理特定模式的DAO類(而不是對象)。它有靜態方法來建立表或刪除它們。它的內部類OpenHelper和DevOpenHelper是SQLiteOpenHelper實現,它們在SQLite數據庫中建立模式。
  • DaoSession:管理特定模式的全部可用DAO對象,您可使用其中一個getter方法獲取該對象。DaoSession還提供了一些通用的持久性方法,如實體的插入,加載,更新,刷新和刪除。
  • XXXDao:數據訪問對象(DAO)持久存在並查詢實體。對於每一個實體,greenDAO生成DAO。它具備比DaoSession更多的持久性方法,例如:count,loadAll和insertInTx。
  • Entities :可持久化對象。一般, 實體對象表明一個數據庫行使用標準 Java 屬性(如一個POJO 或 JavaBean )。


    GreenDao核心.png

1. 導入Gradle插件和Dao代碼生成

要在Android項目中使用GreenDao,您須要添加GreenDao Gradle插件並添加GreenDao庫:

  1. 導入插件
// 在 Project的build.gradle 文件中添加: buildscript { repositories { jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin } } 
  1. 配置相關依賴
// 在 Moudle:app的 build.gradle 文件中添加: apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' // apply plugin dependencies { implementation 'org.greenrobot:greendao:3.2.2' // add library } 
  1. 配置數據庫相關信息
greendao {
    schemaVersion 1 //數據庫版本號 daoPackage 'com.aserbao.aserbaosandroid.functions.database.greenDao.db' // 設置DaoMaster、DaoSession、Dao 包名 targetGenDir 'src.main.java'//設置DaoMaster、DaoSession、Dao目錄,請注意,這裏路徑用.不要用/ generateTests false //設置爲true以自動生成單元測試。 targetGenDirTests 'src/main/java' //應存儲生成的單元測試的基本目錄。默認爲 src / androidTest / java。 } 

配置完成,在Android Studio中使用Build> Make Project,重寫build項目,GreenDao集成完成!

2. 建立存儲對象實體類

使用GreenDao存儲數據只須要在存儲數據類前面聲明@Entity註解就讓GreenDao爲其生成必要的代碼:

@Entity
public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 ……getter and setter and constructor method…… } 

3. GreenDao初始化

咱們能夠在Application中維持一個全局的會話。咱們在Applicaiton進行數據庫的初始化操做:

/** * 初始化GreenDao,直接在Application中進行初始化操做 */ private void initGreenDao() { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db"); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); } private DaoSession daoSession; public DaoSession getDaoSession() { return daoSession; } 

初始化完成以後從新rebuild一下項目會發如今設置的targetGenDir的目錄生成三個類文件,這個是GreenDao自動生成的!說明數據庫已經鏈接好了,我們接下來只須要進行數據庫的增刪改查操做就好了。Let's Go!

4. 使用GreenDao實現增刪改查

1. 增

insert() 插入數據

@Override public void insertData(Thing s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); for (int i = 0; i < 1000; i++) { Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); } } 

insertOrReplace()數據存在則替換,數據不存在則插入

@Override public void insertData(Thing s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); for (int i = 0; i < 1000; i++) { Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insertOrReplace(student);//插入或替換 } } 

2. 刪

刪除有兩種方式:delete()和deleteAll();分別表示刪除單個和刪除全部。

@Override public void deleteData(Student s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); daoSession.delete(s); } 
@Override public void deleteAll() { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); daoSession.deleteAll(Student.class); } 

3. 改

經過update來進行修改:

@Override public void updataData(Student s) { DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); daoSession.update(s); } 

4. 查

查詢的方法有:

  • loadAll():查詢全部數據。
  • queryRaw():根據條件查詢。
  • queryBuilder() : 方便查詢的建立,後面詳細講解。
public List queryAll(){ List<Student> students = daoSession.loadAll(Student.class); return students; } 
@Override public void queryData(String s) { List<Student> students = daoSession.queryRaw(Student.class, " where id = ?", s); mDataBaseAdapter.addNewStudentData(students); } 

4. QueryBuilder的使用

編寫SQL可能很困難而且容易出現錯誤,這些錯誤僅在運行時纔會被注意到。該QueryBuilder的類可讓你創建你的實體,而不SQL自定義查詢,並有助於在編譯時已檢測錯誤。

咱們先講下QueryBuilder的常見方法:

  • where(WhereCondition cond, WhereCondition... condMore): 查詢條件,參數爲查詢的條件!
  • or(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore): 嵌套條件或者,用法同or。
  • and(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore): 嵌套條件且,用法同and。
  • join(Property sourceProperty, Class<J> destinationEntityClass):多表查詢,後面會講。
    輸出結果有四種方式,選擇其中一種最適合的便可,list()返回值是List,而其餘三種返回值均實現Closeable,須要注意的不使用數據時遊標的關閉操做:
  • list ()全部實體都加載到內存中。結果一般是一個沒有魔法的 ArrayList。最容易使用。
  • listLazy ()實體按需加載到內存中。首次訪問列表中的元素後,將加載並緩存該元素以供未來使用。必須關閉。
  • listLazyUncached ()實體的「虛擬」列表:對列表元素的任何訪問都會致使從數據庫加載其數據。必須關閉。
  • listIterator ()讓咱們經過按需加載數據(懶惰)來迭代結果。數據未緩存。必須關閉。
  • orderAsc() 按某個屬性升序排;
  • orderDesc() 按某個屬性降序排;

GreenDao中SQL語句的縮寫,咱們也瞭解下,源碼在Property中,使用的時候能夠本身點進去查詢便可:

  • eq():"equal ('=?')" 等於;
  • notEq() :"not equal ('<>?')" 不等於;
  • like():" LIKE ?" 值等於;
  • between():" BETWEEN ? AND ?" 取中間範圍;
  • in():" IN (" in命令;
  • notIn():" NOT IN (" not in 命令;
  • gt():">?" 大於;
  • lt():"<? " 小於;
  • ge():">=?" 大於等於;
  • le():"<=? " 小於等於;
  • isNull():" IS NULL" 爲空;
  • isNotNull():" IS NOT NULL" 不爲空;

1. 使用QueryBuilder進行查詢操做

1. 簡單條件查詢

查詢當前Student表的全部的數據:

public List queryAllList(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); List<Student> list = qb.list(); // 查出全部的數據 return list; } 

查詢Name爲「一」的全部Student:

public List queryListByMessage(String name){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); QueryBuilder<Student> studentQueryBuilder = qb.where(StudentDao.Properties.Name.eq("一")).orderAsc(StudentDao.Properties.Name); List<Student> studentList = studentQueryBuilder.list(); //查出當前對應的數據 return list; } 

2. 原始查詢

經過原始的SQL查詢語句進行查詢!其實上面有提到QueryBuilder的目的就是方便快捷的編寫SQL查詢語句,避免咱們本身在編寫過程當中出錯!簡單介紹下經過QueryBuilder編寫數據庫,方式方法以下 :

public List queryListBySqL(){ // 查詢ID大於5的全部學生 DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); Query<Student> query = daoSession.queryBuilder(Student.class).where( new WhereCondition.StringCondition("_ID IN " + "(SELECT _ID FROM STUDENT WHERE _ID > 5)") ).build(); List<Student> list = query.list(); return list; } 

3. 嵌套條件查詢

查詢Id大於5小於10,且Name值爲"一"的數據:

public List queryList(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); qb = daoSession.queryBuilder(Student.class); List<Student> list2 = qb.where(StudentDao.Properties.Name.eq("一"), qb.and(StudentDao.Properties.Id.gt(5), StudentDao.Properties.Id.le(50))).list(); return list2; } 

取10條Id大於1的數據,且偏移2條

public List queryListByOther(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); //搜索條件爲Id值大於1,即結果爲[2,3,4,5,6,7,8,9,10,11]; // offset(2)表示日後偏移2個,結果爲[4,5,6,7,8,9,10,11,12,13]; List<Student> list = qb.where(StudentDao.Properties.Id.gt(1)).limit(10).offset(2).list(); return list; } 

4. 屢次執行查找

使用QueryBuilder構建查詢後,能夠重用 Query對象以便稍後執行查詢。這比始終建立新的Query對象更有效。若是查詢參數沒有更改,您能夠再次調用list / unique方法。能夠經過setParameter方法來修改條件參數值:

public List queryListByMoreTime(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> qb = daoSession.queryBuilder(Student.class); //搜索條件爲Id值大於1,即結果爲[2,3,4,5,6,7,8,9,10,11]; // offset(2)表示日後偏移2個,結果爲[4,5,6,7,8,9,10,11,12,13]; Query<Student> query = qb.where(StudentDao.Properties.Id.gt(1)).limit(10).offset(2).build(); List<Student> list = query.list(); //經過SetParameter來修改上面的查詢條件,好比咱們將上面條件修改取10條Id值大於5,日後偏移兩位的數據,方法以下! query.setParameter(0,5); List<Student> list1 = query.list(); return list1; } 

5. 在多個線程中使用QueryBuilder

若是在多個線程中使用查詢,則必須調用 forCurrentThread ()以獲取當前線程的Query實例。Query的對象實例綁定到構建查詢的擁有線程。

這使您能夠安全地在Query對象上設置參數,而其餘線程不會干擾。若是其餘線程嘗試在查詢上設置參數或執行綁定到另外一個線程的查詢,則會拋出異常。像這樣,您不須要同步語句。實際上,您應該避免鎖定,由於若是併發事務使用相同的Query對象,這可能會致使死鎖。

每次調用forCurrentThread ()時, 參數都會在使用其構建器構建查詢時設置爲初始參數。

2. 使用QueryBuilder進行批量刪除操做

使用QueryBuilder進行批量刪除操做,不會刪除單個實體,但會刪除符合某些條件的全部實體。要執行批量刪除,請建立QueryBuilder,調用其 buildDelete ()方法,而後執行返回的 DeleteQuery。

例子:刪除數據庫中id大於5的全部其餘數據

public boolean deleteItem(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); QueryBuilder<Student> where = daoSession.queryBuilder(Student.class).where(StudentDao.Properties.Id.gt(5)); DeleteQuery<Student> deleteQuery = where.buildDelete(); deleteQuery.executeDeleteWithoutDetachingEntities(); return false; } 

5. 註解講解

從GreenDao 3 使用註解來定義模型和實體,前面也講過,經過註解的使用能夠快速構建數據庫表,包括設置主鍵,自增,值是否惟一等等等……

下面咱們來看下註解的簡單使用:

@Entity
public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 ……getter and setter and constructor method…… } 

1. @Entity註解

@Entity是GreenDao必不可少的註解,只有在實體類中使用了@Entity註解GreenDao纔會建立對應的表。固然咱們也可使用@Entity配置一些細節:

  • schema:若是你有多個架構,你能夠告訴GreenDao當前屬於哪一個架構。
  • active:標記一個實體處於活躍狀態,活動實體有更新、刪除和刷新方法。
  • nameInDb:在數據中使用的別名,默認使用的是實體的類名。
  • indexes:標記若是DAO應該建立數據庫表(默認爲true),若是您有多個實體映射到一個表,或者表的建立是在greenDAO以外進行的,那麼將其設置爲false。
  • createInDb:標記建立數據庫表。
  • generateGettersSetters:若是缺乏,是否應生成屬性的getter和setter方法。
@Entity( schema = "myschema", active = true, nameInDb = "AWESOME_USERS", indexes = { @Index(value = "message DESC", unique = true) }, createInDb = false, generateConstructors = true, generateGettersSetters = true ) public class Student{ …… } 

2. 基礎屬性註解(@Id,@Property,@NotNull,@Transient)

@Id
@Id註解選擇 long / Long屬性做爲實體ID。在數據庫方面,它是主鍵。參數autoincrement = true 表示自增,id不給賦值或者爲賦值爲null便可(這裏須要注意,若是要實現自增,id必須是Long,爲long不行!)。

@Entity public class Student { @Id(autoincrement = true) Long id; …… } 

@Property
容許您定義屬性映射到的非默認列名。若是不存在,GreenDAO將以SQL-ish方式使用字段名稱(大寫,下劃線而不是camel狀況,例如 name將成爲 NAME)。注意:您當前只能使用內聯常量來指定列名。

@Entity public class Student { @Id(autoincrement = true) Long id; @Property (nameInDb="name") //設置了,數據庫中的表格屬性名爲"name",若是不設置,數據庫中表格屬性名爲"NAME" String name; …… } 

@NotNull :設置數據庫表當前列不能爲空 。

@Transient :添加次標記以後不會生成數據庫表的列。標記要從持久性中排除的屬性。將它們用於臨時狀態等。或者,您也可使用Java中的transient關鍵字。

3. 索引註解

  • @Index:使用@Index做爲一個屬性來建立一個索引,經過name設置索引別名,也能夠經過unique給索引添加約束。
  • @Unique:向索引添加UNIQUE約束,強制全部值都是惟一的。
@Entity public class Student { @Id(autoincrement = true) Long id; @Property(nameInDb="name") @Index(unique = true) String name; …… } 

注意: 上面這種狀況,約定name爲惟一值,向數據庫中經過insert方法繼續添加已存在的name數據,會拋異常:

10-08 20:59:46.274 31939-31939/com.example.aserbao.aserbaosandroid E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aserbao.aserbaosandroid, PID: 31939 android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: STUDENT.name (Sqlite code 2067), (OS error - 2:No such file or directory) …… 

若使用insertOrReplace()方法添加數據,當前數據庫中不會有重複的數據,可是重複的這條數據的id會被修改!若項目中有用到id字段進行排序的話,這一點須要特別注意。

4. 關係註解

關係型註解GreenDao中主要就兩個:

  • @ToOne:定義與另外一個實體(一個實體對象)的關係
  • @ToMany:定義與多個實體對象的關係
    至於如何使用,咱們立刻就講。

6. 一對一,一對多,多對多關係表的建立

日常項目中,咱們常常會使用到多表關聯,如文章開頭所說的數據庫表結構設置的那樣!接下來咱們來說如何經過GreenDao實現多表關聯。

1. 一對一

一個學生對應一個身份證號:
作法:

  1. 咱們在Student中設置一個註解@ToOne(joinProperty = "name")
  2. 在建立Student的時候,將對應的數據傳遞給IdCard;
    代碼部分:

學生Student代碼:

@Entity public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 @ToOne(joinProperty = "name") IdCard student; ……getter and setter …… } 

身份證IdCard代碼:

@Entity public class IdCard { @Id String userName;//用戶名 @Unique String idNo;//身份證號 ……getter and setter …… } 

insert一組數據:

public void addStudent(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); //插入對應的IdCard數據 IdCard idCard = new IdCard(); idCard.setUserName(userName); idCard.setIdNo(RandomValue.getRandomID()); daoSession.insert(idCard); } 

ok,數據能夠了!如今數據庫表插入完成了。

2. 一對多

一我的擁有多個信用卡
作法:

  1. 在咱們在Student中設置@ToMany(referencedJoinProperty = "id");
  2. 咱們在CreditCard中設置編寫對應的id主鍵;

Student的代碼:

@Entity public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 @ToMany(referencedJoinProperty = "id") // 這個id是對應在CreditCard中的id List<CreditCard> creditCardsList; ……getter and setter …… } 

CreditCard的代碼:

@Entity public class CreditCard { @Id Long id; Long userId; String userName;//持有者名字 String cardNum;//卡號 String whichBank;//哪一個銀行的 int cardType;//卡等級,分類 0 ~ 5 ……getter and setter …… } 

添加數據代碼:

public void addStudent(){ DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession(); Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); //插入對應的CreditCard數據 for (int j = 0; j < random.nextInt(5) + 1 ; j++) { CreditCard creditCard = new CreditCard(); creditCard.setUserId(id); creditCard.setUserName(userName); creditCard.setCardNum(String.valueOf(random.nextInt(899999999) + 100000000) + String.valueOf(random.nextInt(899999999) + 100000000)); creditCard.setWhichBank(RandomValue.getBankName()); creditCard.setCardType(random.nextInt(10)); daoSession.insert(creditCard); } } 

3. 多對多

一個學生有多個老師,老師有多個學生。
作法:

  1. 咱們須要建立一個學生老師管理器(StudentAndTeacherBean),用來對應學生和老師的ID;

  2. 咱們須要在學生對象中,添加註解:

    @ToMany
    @JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId")
    List<Teacher> teacherList;

  3. 咱們須要在老師對象中,添加註解:@ToMany

@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "teacherId",targetProperty = "studentId") List<Student> studentList; 

StudentAndTeacherBean代碼:

@Entity public class StudentAndTeacherBean { @Id(autoincrement = true) Long id; Long studentId;//學生ID Long teacherId;//老師ID ……getter and setter …… } 

Student 代碼:

@Entity public class Student { @Id(autoincrement = true) Long id; @Unique int studentNo;//學號 int age; //年齡 String telPhone;//手機號 String sex; //性別 String name;//姓名 String address;//家庭住址 String schoolName;//學校名字 String grade;//幾年級 @ToMany @JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId") List<Teacher> teacherList; ……getter and setter …… } 

Teacher代碼:

@Entity public class Teacher { @Id(autoincrement = true) Long id; @Unique int teacherNo;//職工號 int age; //年齡 String sex; //性別 String telPhone; String name;//姓名 String schoolName;//學校名字 String subject;//科目 @ToMany @JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "teacherId",targetProperty = "studentId") List<Student> studentList; ……getter and setter …… } 

數據添加:

public void addData(){ Student student = new Student(); student.setStudentNo(i); int age = mRandom.nextInt(10) + 10; student.setAge(age); student.setTelPhone(RandomValue.getTel()); String chineseName = RandomValue.getChineseName(); student.setName(chineseName); if (i % 2 == 0) { student.setSex("男"); } else { student.setSex("女"); } student.setAddress(RandomValue.getRoad()); student.setGrade(String.valueOf(age % 10) + "年紀"); student.setSchoolName(RandomValue.getSchoolName()); daoSession.insert(student); Collections.shuffle(teacherList); for (int j = 0; j < mRandom.nextInt(8) + 1; j++) { if(j < teacherList.size()){ Teacher teacher = teacherList.get(j); StudentAndTeacherBean teacherBean = new StudentAndTeacherBean(student.getId(), teacher.getId()); daoSession.insert(teacherBean); } } } 

好了,成功;

7. 數據庫的升級

GreenDao的OpenHelper下有個 onUpgrade(Database db, int oldVersion, int newVersion)方法,當設置的數據庫版本改變時,在數據庫初始化的時候就會回調到這個方法,咱們能夠經過繼承OpenHelper重寫onUpgrade方法來實現數據庫更新操做:

GreenDao的升級思路:

  1. 建立臨時表TMP_,複製原來的數據庫到臨時表中;
  2. 刪除以前的原表;
  3. 建立新表;
  4. 將臨時表中的數據複製到新表中,最後將TMP_表刪除掉;

ok,思路就是這樣, 總共兩個類: 一個MyDaoMaster(OpenHelper繼承類),一個MigrationHelper(數據庫操做類) 下面是代碼編寫:

修改Application中的DaoMaster的建立:

MyDaoMaster helper = new MyDaoMaster(this, "aserbaos.db"); // DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db"); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); 

MyDaoMaster代碼:

public class MyDaoMaster extends OpenHelper { private static final String TAG = "MyDaoMaster"; public MyDaoMaster(Context context, String name) { super(context, name); } public MyDaoMaster(Context context, String name, SQLiteDatabase.CursorFactory factory) { super(context, name, factory); } @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { super.onUpgrade(db, oldVersion, newVersion); MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() { @Override public void onCreateAllTables(Database db, boolean ifNotExists) { DaoMaster.createAllTables(db, ifNotExists); } @Override public void onDropAllTables(Database db, boolean ifExists) { DaoMaster.dropAllTables(db, ifExists); } },ThingDao.class); Log.e(TAG, "onUpgrade: " + oldVersion + " newVersion = " + newVersion); } } 

MigrationHelper 代碼:

public final class MigrationHelper { public static boolean DEBUG = false; private static String TAG = "MigrationHelper"; private static final String SQLITE_MASTER = "sqlite_master"; private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master"; private static WeakReference<ReCreateAllTableListener> weakListener; public interface ReCreateAllTableListener{ void onCreateAllTables(Database db, boolean ifNotExists); void onDropAllTables(Database db, boolean ifExists); } public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) { printLog("【The Old Database Version】" + db.getVersion()); Database database = new StandardDatabase(db); migrate(database, daoClasses); } public static void migrate(SQLiteDatabase db, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) { weakListener = new WeakReference<>(listener); migrate(db, daoClasses); } public static void migrate(Database database, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) { weakListener = new WeakReference<>(listener); migrate(database, daoClasses); } public static void migrate(Database database, Class<? extends AbstractDao<?, ?>>... daoClasses) { printLog("【Generate temp table】start"); generateTempTables(database, daoClasses); printLog("【Generate temp table】complete"); ReCreateAllTableListener listener = null; if (weakListener != null) { listener = weakListener.get(); } if (listener != null) { listener.onDropAllTables(database, true); printLog("【Drop all table by listener】"); listener.onCreateAllTables(database, false); printLog("【Create all table by listener】"); } else { dropAllTables(database, true, daoClasses); createAllTables(database, false, daoClasses); } printLog("【Restore data】start"); restoreData(database, daoClasses); printLog("【Restore data】complete"); } private static void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { for (int i = 0; i < daoClasses.length; i++) { String tempTableName = null; DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]); String tableName = daoConfig.tablename; if (!isTableExists(db, false, tableName)) { printLog("【New Table】" + tableName); continue; } try { tempTableName = daoConfig.tablename.concat("_TEMP"); StringBuilder dropTableStringBuilder = new StringBuilder(); dropTableStringBuilder.append("DROP TABLE IF EXISTS ").append(tempTableName).append(";"); db.execSQL(dropTableStringBuilder.toString()); StringBuilder insertTableStringBuilder = new StringBuilder(); insertTableStringBuilder.append("CREATE TEMPORARY TABLE ").append(tempTableName); insertTableStringBuilder.append(" AS SELECT * FROM ").append(tableName).append(";"); db.execSQL(insertTableStringBuilder.toString()); printLog("【Table】" + tableName +"\n ---Columns-->"+getColumnsStr(daoConfig)); printLog("【Generate temp table】" + tempTableName); } catch (SQLException e) { Log.e(TAG, "【Failed to generate temp table】" + tempTableName, e); } } } private static boolean isTableExists(Database db, boolean isTemp, String tableName) { if (db == null || TextUtils.isEmpty(tableName)) { return false; } String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER; String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?"; Cursor cursor=null; int count = 0; try { cursor = db.rawQuery(sql, new String[]{"table", tableName}); if (cursor == null || !cursor.moveToFirst()) { return false; } count = cursor.getInt(0); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) cursor.close(); } return count > 0; } private static String getColumnsStr(DaoConfig daoConfig) { if (daoConfig == null) { return "no columns"; } StringBuilder builder = new StringBuilder(); for (int i = 0; i < daoConfig.allColumns.length; i++) { builder.append(daoConfig.allColumns[i]); builder.append(","); } if (builder.length() > 0) { builder.deleteCharAt(builder.length() - 1); } return builder.toString(); } private static void dropAllTables(Database db, boolean ifExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) { reflectMethod(db, "dropTable", ifExists, daoClasses); printLog("【Drop all table by reflect】"); } private static void createAllTables(Database db, boolean ifNotExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) { reflectMethod(db, "createTable", ifNotExists, daoClasses); printLog("【Create all table by reflect】"); } /** * dao class already define the sql exec method, so just invoke it */ private static void reflectMethod(Database db, String methodName, boolean isExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) { if (daoClasses.length < 1) { return; } try { for (Class cls : daoClasses) { Method method = cls.getDeclaredMethod(methodName, Database.class, boolean.class); method.invoke(null, db, isExists); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private static void restoreData(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { for (int i = 0; i < daoClasses.length; i++) { DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]); String tableName = daoConfig.tablename; String tempTableName = daoConfig.tablename.concat("_TEMP"); if (!isTableExists(db, true, tempTableName)) { continue; } try { // get all columns from tempTable, take careful to use the columns list List<TableInfo> newTableInfos = TableInfo.getTableInfo(db, tableName); List<TableInfo> tempTableInfos = TableInfo.getTableInfo(db, tempTableName); ArrayList<String> selectColumns = new ArrayList<>(newTableInfos.size()); ArrayList<String> intoColumns = new ArrayList<>(newTableInfos.size()); for (TableInfo tableInfo : tempTableInfos) { if (newTableInfos.contains(tableInfo)) { String column = '`' + tableInfo.name + '`'; intoColumns.add(column); selectColumns.add(column); } } // NOT NULL columns list for (TableInfo tableInfo : newTableInfos) { if (tableInfo.notnull && !tempTableInfos.contains(tableInfo)) { String column = '`' + tableInfo.name + '`'; intoColumns.add(column); String value; if (tableInfo.dfltValue != null) { value = "'" + tableInfo.dfltValue + "' AS "; } else { value = "'' AS "; } selectColumns.add(value + column); } } if (intoColumns.size() != 0) { StringBuilder insertTableStringBuilder = new StringBuilder(); insertTableStringBuilder.append("REPLACE INTO ").append(tableName).append(" ("); insertTableStringBuilder.append(TextUtils.join(",", intoColumns)); insertTableStringBuilder.append(") SELECT "); insertTableStringBuilder.append(TextUtils.join(",", selectColumns)); insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";"); db.execSQL(insertTableStringBuilder.toString()); printLog("【Restore data】 to " + tableName); } StringBuilder dropTableStringBuilder = new StringBuilder(); dropTableStringBuilder.append("DROP TABLE ").append(tempTableName); db.execSQL(dropTableStringBuilder.toString()); printLog("【Drop temp table】" + tempTableName); } catch (SQLException e) { Log.e(TAG, "【Failed to restore data from temp table 】" + tempTableName, e); } } } private static List<String> getColumns(Database db, String tableName) { List<String> columns = null; Cursor cursor = null; try { cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 0", null); if (null != cursor && cursor.getColumnCount() > 0) { columns = Arrays.asList(cursor.getColumnNames()); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) cursor.close(); if (null == columns) columns = new ArrayList<>(); } return columns; } private static void printLog(String info){ if(DEBUG){ Log.d(TAG, info); } } private static class TableInfo { int cid; String name; String type; boolean notnull; String dfltValue; boolean pk; @Override public boolean equals(Object o) { return this == o || o != null && getClass() == o.getClass() && name.equals(((TableInfo) o).name); } @Override public String toString() { return "TableInfo{" + "cid=" + cid + ", name='" + name + '\'' + ", type='" + type + '\'' + ", notnull=" + notnull + ", dfltValue='" + dfltValue + '\'' + ", pk=" + pk + '}'; } private static List<TableInfo> getTableInfo(Database db, String tableName) { String sql = "PRAGMA table_info(" + tableName + ")"; printLog(sql); Cursor cursor = db.rawQuery(sql, null); if (cursor == null) return new ArrayList<>(); TableInfo tableInfo; List<TableInfo> tableInfos = new ArrayList<>(); while (cursor.moveToNext()) { tableInfo = new TableInfo(); tableInfo.cid = cursor.getInt(0); tableInfo.name = cursor.getString(1); tableInfo.type = cursor.getString(2); tableInfo.notnull = cursor.getInt(3) == 1; tableInfo.dfltValue = cursor.getString(4); tableInfo.pk = cursor.getInt(5) == 1; tableInfos.add(tableInfo); // printLog(tableName + ":" + tableInfo); } cursor.close(); return tableInfos; } } } 

8. GreenDao數據庫加密

開發中對於存儲於數據庫中的敏感數據,咱們能夠經過對數據庫加密來進行保護。GreenDao能夠經過SQLCipher來進行加密處理。下面咱們簡單講解下加密過程:

步驟:

  1. 導入加密庫文件:
implementation 'net.zetetic:android-database-sqlcipher:3.5.6' 
  1. 修改DaoSession的生成方式:
// MyDaoMaster helper = new MyDaoMaster(this, "aserbaos.db"); //數據庫升級寫法 DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "aserbao.db"); //SQLiteDatabase db = helper.getWritableDatabase(); //不加密的寫法 Database db = helper.getEncryptedWritableDb("aserbao"); //數據庫加密密碼爲「aserbao"的寫法 DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); 

9. 項目地址

當前文章全部代碼在AserbaosAndroid/app/src/main/java/com/aserbao/aserbaosandroid/functions/database/greenDao/relation目錄下;(不過就我這脾氣,可能在從此整理代碼的過程當中會修改!不過請放心,修改後會在github上進行說明的)

AserbaosAndroid
aserbao的我的Android總結項目,但願這個項目能成爲最全面的Android開發學習項目,這是個美好的願景,項目中還有不少未涉及到的地方,有不少沒有講到的點,但願看到這個項目的朋友,若是你在開發中遇到什麼問題,在這個項目中沒有找到對應的解決辦法,但願你可以提出來,給我留言或者在項目github地址提issues,我有時間就會更新項目沒有涉及到的部分!項目會一直維護下去。固然,我但願是Aserbao'sAndroid 能爲全部Android開發者提供到幫助!也指望更多Android開發者能參與進來,只要你熟悉Android某一塊,均可以將你的代碼pull上分支!

10 總結

這篇文章寫到這裏,零零碎碎花了差很少兩週時間,從十月八號開始到今天正式準備發佈,也算是對GreenDao數據庫的進一步認識!如文章開頭所說,我Android開發之初,使用的是本身編寫SQLite來實現數據庫存儲,到後來使用第三方存儲LitePal,最近,項目早期就使用了GreenDao,因此就又學習了一番GreenDao。對於開發者來講,我以爲不管是這三種中的哪種,其實只要掌握一種我以爲就足夠了!固然若是你有時間,能夠多學習幾種,多學無害嘛!最後,一如既往的說一下:若是你是Android開發者,你在開發路上遇到任何問題,歡迎來個人公衆號給我留言,我們一塊兒討論,加入Android開發討論小組,一塊兒進步!文章歡迎轉載,不過請註明出處。

11. 參考博客

Android ORM 框架:GreenDao 使用詳解
Android數據存儲之GreenDao 3.0 詳解
拆輪子系列之GreenDao框架原理分析

相關文章
相關標籤/搜索