一篇好文之Android數據庫 GreenDao的使用完全解析
8.1 2018.10.23 20:12* 字數 4514 閱讀 26120評論 26喜歡 90讚賞 1
數據庫GreenDao.jpg
之前在開發過程中,數據庫基本上會使用Litepal或者SQlite自己寫,最近換新環境,公司原先使用的數據庫就是GreenDao,在各種情況的作用下,準備瞭解下GreenDao,順便寫一篇文章記錄下GreenDao的基本使用!大家在Android 開發過程中遇到什麼問題,歡迎在我的公衆號「aserbao」給我留言!
Android開發者聚集地
本文主要從如下幾個方面進行講解
- 存儲的數據庫結構
- GreenDao的優缺點
- GreenDao的使用配置
- 使用GreenDao實現數據的增刪改查
- GreenDao的註解使用
- GreenDao的關係處理
- GreenDao的升級
- GreenDao數據庫加密
- 項目地址AserbaosAndroid
- 總結
- 參考博客
- 簡書暫時不支持目錄,如果想看該文章帶目錄版本,請點擊跳轉該文章的CSDN地址
咱們先看一波最終的效果圖:文章最後有項目地址;
greendao
1. 存儲的數據庫結構
學習數據庫之前,我們先得設計自己的數據庫,不多廢話,下面是我此次學習的數據庫結構,後面所有的數據請參考這個圖進行學習:
GreenDao關係圖.jpg
2. GreenDao的介紹
簡單的GreenDao的介紹,嫌麻煩的可以直接跳到GreenDao使用開始看。
什麼是GreenDao?
GreenDAO是一個開源的Android ORM(“對象/關係映射”),通過ORM(稱爲“對象/關係映射”),在我們數據庫開發過程中節省了開發時間!
GreenDao的原理.png
GreenDao的官方文檔
- GreenDao:適用於您的SQLite數據庫的Android ORM
- GreenDao的github地址
- GreenDao的Google討論區
- GreenDao 加密SQLCipher for Android官方說明地址
- GreenDao使用文檔
GreenDao的作用?
通過GreenDao,我們可以更快速的操作數據庫,我們可以使用簡單的面相對象的API來存儲,更新,刪除和查詢Java對象。
GreenDao的優缺點?
-
高性能,下面是官方給出的關於GreenDao,OrmLite和ActiveAndroid三種ORM解決方案的數據統計圖:
GreenDao性能對比圖.png
-
易於使用的強大API,涵蓋關係和連接;
-
最小的內存消耗;
-
小庫大小(<100KB)以保持較低的構建時間並避免65k方法限制;
-
數據庫加密: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庫:
- 導入插件
// 在 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
}
}
- 配置相關依賴
// 在 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
}
- 配置數據庫相關信息
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. 一對一
一個學生對應一個身份證號:
做法:
- 我們在Student中設置一個註解@ToOne(joinProperty = "name")
- 在創建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. 一對多
一個人擁有多個信用卡
做法:
- 在我們在Student中設置@ToMany(referencedJoinProperty = "studentId");
- 我們在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 = "studentId) // 這個studentId是對應在CreditCard中的studentId
List<CreditCard> creditCardsList;
……getter and setter ……
}
CreditCard的代碼:
@Entity
public class CreditCard {
@Id
Long id;
Long studentId;
Long teacherId;
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. 多對多
一個學生有多個老師,老師有多個學生。
做法:
-
我們需要創建一個學生老師管理器(StudentAndTeacherBean),用來對應學生和老師的ID;
-
我們需要在學生對象中,添加註解:
@ToMany
@JoinEntity(entity = StudentAndTeacherBean.class,sourceProperty = "studentId",targetProperty = "teacherId")
List<Teacher> teacherList; -
我們需要在老師對象中,添加註解:@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的升級思路:
- 創建臨時表TMP_,複製原來的數據庫到臨時表中;
- 刪除之前的原表;
- 創建新表;
- 將臨時表中的數據複製到新表中,最後將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來進行加密處理。下面我們簡單講解下加密過程:
步驟:
- 導入加密庫文件:
implementation 'net.zetetic:android-database-sqlcipher:3.5.6'
- 修改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開發討論小組,一起進步!文章轉載請註明出處。
Android開發者聚集地
11. 參考博客
Android ORM 框架:GreenDao 使用詳解
Android數據存儲之GreenDao 3.0 詳解
拆輪子系列之GreenDao框架原理分析
12.修改記錄
- CreditCard中不能只使用一個useId來做關聯,因爲我這裏Teacher和Student都和CreditCard是一對多關係,所以我們需要建兩個對應關係字段。爲了分辨添加了studentId和teacherId。
@Entity
public class CreditCard {
@Id
Long id;
Long studentId;
Long teacherId;
String userName;//持有者名字
String cardNum;//卡號
String whichBank;//哪個銀行的
int cardType;//卡等級,分類 0 ~ 5
}
- Student中的@ToMany(referencedJoinProperty =“id”)這個id對應的是CreditCard中的studentId,不是自增Id。(問題由@山豆幾_提出,感謝)
修改後的代碼應該是:
@ToMany(referencedJoinProperty = "studentId")
List<CreditCard> creditCardsList;