前言:
????????Greendao是一款用于數(shù)據(jù)庫創(chuàng)建與管理的框架,由于原生SQLite語言比較復(fù)雜繁瑣,使得不少程序員不得不去學(xué)習(xí)SQLite原生語言,但是學(xué)習(xí)成本高尤误,效率低下,所以不少公司致力于開發(fā)一款簡單的數(shù)據(jù)庫管理框架结缚,較為著名的就有Greendao和ORMLite损晤,但是就數(shù)據(jù)分析來看,Greendao的效率是高于ORMLite及其他框架的红竭,是目前該行業(yè)的領(lǐng)先者尤勋。也因為Greendao的使用方法簡便,且效率高使得其成為目前使用最為廣泛的數(shù)據(jù)庫管理框架德崭,這也是廣大程序員的福音斥黑。
優(yōu)點:
存取速度快
支持數(shù)據(jù)庫加密
輕量級
激活實體
支持緩存
代碼自動生成
使用:
github 鏈接:https://github.com/greenrobot/greenDAO
1. 環(huán)境配置:
在項目的build.gradle文件下進入如下配置 (注釋的地方就是要添加的東西)
在App的build.gradle文件下進行如下配置 (注釋的地方就是要添加的東西)
2.新建實體類用@Entity注解,實體類中的屬性即為數(shù)據(jù)庫中對應(yīng)的字段,最后build項目即會生成相應(yīng)的代碼
? ?build之前實體類如下:
?build之前實體類如下:
? ??????比build之前多了構(gòu)造方法和set,get方法
? ? ? ? 另外自動生成了DaoMaster,DaoSession,HistoryDataDao三個類
? ??注解解釋
????1.@Entity:告訴GreenDao該對象為實體,只有被@Entity注釋的Bean類才能被dao類操作
????2.@Id:對象的Id眉厨,使用Long類型作為EntityId锌奴,否則會報錯。(autoincrement = true)表示主鍵會自增憾股,如果false就會使用舊值
????3.@Property:可以自定義字段名鹿蜀,注意外鍵不能使用該屬性
????4.@NotNull:屬性不能為空
????5.@Transient:使用該注釋的屬性不會被存入數(shù)據(jù)庫的字段中
????6.@Unique:該屬性值必須在數(shù)據(jù)庫中是唯一值
????7.@Generated:編譯后自動生成的構(gòu)造函數(shù)、方法等的注釋服球,提示構(gòu)造函數(shù)茴恰、方法等不能被修改
3. 創(chuàng)建數(shù)據(jù)庫
可以發(fā)現(xiàn),GreenDao已經(jīng)將我們的數(shù)據(jù)庫創(chuàng)建縮成幾句話斩熊,代碼會自動將實體類對象創(chuàng)建成表往枣,不再是傳統(tǒng)的手寫SQL語句。這里的數(shù)據(jù)庫創(chuàng)建只需要在MyApplication中執(zhí)行一次即可粉渠,這里對幾個類進行解釋DevOpenHelper:創(chuàng)建SQLite數(shù)據(jù)庫的SQLiteOpenHelper的具體實現(xiàn)DaoMaster:GreenDao的頂級對象分冈,作為數(shù)據(jù)庫對象、用于創(chuàng)建表和刪除表DaoSession:管理所有的Dao對象霸株,Dao對象中存在著增刪改查等API
由于我們已經(jīng)創(chuàng)建好了DaoSession和HistoryData的實體類對象雕沉,編譯后會自動生成我們的HistoryDataDao對象,可通過DaoSession獲得HistoryDataDao historyDataDao = daoSession.getHistoryDataDao();
這里的Dao(Data Access Object)是指數(shù)據(jù)訪問接口,即提供了數(shù)據(jù)庫操作一些API接口去件,可通過dao進行增刪改查操作
代碼復(fù)制如下:
public class DaoManager {
? ?private static DaoManagerinstance;
? ? private static DaoMasterdaoMaster;
? ? public static DaoManagergetInstance() {
????if (instance ==null) {
????????instance =new DaoManager();
? ? ? ? }
????return instance;
? ? }
public DaoManager(){
????????//創(chuàng)建數(shù)據(jù)庫
? ? ? ? DaoMaster.OpenHelper helper =new DaoMaster.DevOpenHelper(MyApplication.getContext(),"read",null);
? ? ? ? //獲取數(shù)據(jù)庫可讀
? ? ? ? daoMaster =new DaoMaster(helper.getWritableDatabase());
? ? }
public DaoSessiongetSession(){
????????return daoMaster.newSession();
? ? }
}
4. 數(shù)據(jù)庫的增刪改查
? ? 第一:插入多條數(shù)據(jù)
????????????BookBeansDao bookBeansDao = DaoManager.getInstance().getSession().getBookBeansDao();
? ? ? ? ? ? ? ? for (int i =0; i <10; i++) {
????????????????????BookBeans bookBeans =new BookBeans();
? ????????????????? bookBeans.setTitle(i+"");
? ? ????????????????bookBeans.setContent(i+"");
? ????????????????? bookBeansDao.insertInTx(bookBeans);
????????????????}
常用API
//這是最簡單的插入語句坡椒,新增一行數(shù)據(jù)扰路,返回值為行號
public long insert(T entity)
//傳遞一個數(shù)組,新增多行數(shù)據(jù)
public void insertInTx(T... entities)
//傳遞一個集合倔叼,新增多行數(shù)據(jù)
public void insertInTx(Iterable<T> entities)
//傳遞一個集合汗唱,新增多行數(shù)據(jù),setPrimaryKey:是否設(shè)置主鍵
public void insertInTx(Iterable<T> entities, boolean setPrimaryKey)
//將給定的實體插入數(shù)據(jù)庫缀雳,若此實體類存在渡嚣,則覆蓋
public long insertOrReplace(T entity)
//使用事務(wù)操作,將給定的實體插入數(shù)據(jù)庫肥印,若此實體類存在,則覆蓋
public void insertOrReplaceInTx(T... entities)
//使用事務(wù)操作绝葡,將給定的實體插入數(shù)據(jù)庫深碱,若此實體類存在,則覆蓋
public void insertOrReplaceInTx(Iterable<T> entities)
//使用事務(wù)操作藏畅,將給定的實體插入數(shù)據(jù)庫敷硅,若此實體類存在,則覆蓋愉阎,并設(shè)置是否設(shè)定主鍵
public void insertOrReplaceInTx(Iterable<T> entities, boolean setPrimaryKey)
第二:刪除
DaoManager.getInstance().getSession().getBookBeansDao().deleteByKeyInTx((long) 1);
常用API
//根據(jù)實體绞蹦,刪除一條數(shù)據(jù)(默認根據(jù)主鍵刪除數(shù)據(jù))
public void delete(T entity)
//刪除所有數(shù)據(jù)
public void deleteAll()
//根據(jù)key刪除數(shù)據(jù)
public void deleteByKey(K key)
//刪除多條數(shù)據(jù)
public void deleteInTx(T... entities)
//根據(jù)主鍵數(shù)組,刪除多條數(shù)據(jù)
public void deleteByKeyInTx(K... keys)
//根據(jù)集合榜旦,刪除多條數(shù)據(jù)
public void deleteByKeyInTx(Iterable<K> keys)
第三:修改
DaoManager.getInstance().getSession().getBookBeansDao().update(bookBeans);
常用API?
//根據(jù)實體更新?
public void update(T entity)
//批量更新
public void updateInTx(T... entities)
// 在一次事物中提交全部實體類
public void updateInTx(Iterable<T> entities)
第四:查詢所有數(shù)據(jù)
? ? ? ? ? DaoManager.getInstance().getSession().getBookBeansDao().loadAll();
常用API??
常用API//根據(jù)主鍵來查詢一條數(shù)據(jù)
public T load(K key)
//根據(jù)行號來查詢一條數(shù)據(jù)幽七,行號從1開始
public T loadByRowId(long rowId)
//查詢表中所有的數(shù)據(jù)
public List<T> loadAll()
QueryBuilder查詢常用方法
常用API???
// 條件,AND 連接
public QueryBuilder<T> where(WhereCondition cond, WhereCondition... condMore)
// 條件溅呢,OR 連接
public QueryBuilder<T> whereOr(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore)
//去重
public QueryBuilder<T> distinct()
//分頁
public QueryBuilder<T> limit(int limit)
//偏移結(jié)果起始位澡屡,配合limit(int)使用
public QueryBuilder<T> offset(int offset)
//排序,升序
public QueryBuilder<T> orderAsc(Property... properties)
//排序咐旧,降序
public QueryBuilder<T> orderDesc(Property... properties)
// 排序驶鹉,自定義
public QueryBuilder<T> orderCustom(Property property, String customOrderForProperty)
// 排序,SQL 語句
public QueryBuilder<T> orderRaw(String rawOrder)
//本地化字符串排序铣墨,用于加密數(shù)據(jù)庫無效
public QueryBuilder<T> preferLocalizedStringOrder()
//自定義字符串排序室埋,默認不區(qū)分大小寫
public QueryBuilder<T> stringOrderCollation(String stringOrderCollation)
判斷條件
//等于? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? eq()
//不等于? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????????????????????notEq()
//值等于? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? like()
//取中間范圍? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?between()
//in命令? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? in()
//not in 命令????????????????????????????????????????????notIn()
//大于? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? gt()
//小于????????????????????????????????????????????????????????lt()
//大于等于? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ge()
//小于等于? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? le()
//為空? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?isNull()
//不為空? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isNotNull()
查詢所有
// 查詢所有
Listlist= studentDao.queryBuilder().list();? ? ??
Log.e(TAG,"onCreate: "+list.size());??
// And條件查詢?
List<Student> list = studentDao.queryBuilder() .where(StudentDao.Properties.MId.eq(2)) .where(StudentDao.Properties.ClassName.eq("一年級")) .list();?
Log.e(TAG, "onCreate: "+list.size());??
// Or條件查詢?
List<Student> list = studentDao.queryBuilder() .whereOr(StudentDao.Properties.ClassName.eq("一年級"), StudentDao.Properties.MId.eq(2)) .list(); Log.e(TAG, "onCreate: "+list.size());?
偏移查詢? ? ? ?
// 查詢10條數(shù)據(jù)、偏移5條數(shù)據(jù)
List<Student> list = studentDao.queryBuilder() .offset(5) .limit(10) .list();?
Log.e(TAG, "onCreate: "+list.size());?
// 模糊查詢(%級% 代表“級”前面和后面可能存在字符伊约,也可以不存在) // 一% 表示以一開頭的字符串
List<Student> list = studentDao.queryBuilder() .where(StudentDao.Properties.ClassName.like("%級%")) .list();
?Log.e(TAG, "onCreate: "+list.size());?
// 返回按照年齡倒序排序?
List<Student> list = studentDao.queryBuilder() .orderDesc(StudentDao.Properties.MAge) .list();?
Log.e(TAG, "onCreate: "+list.size());??
// in操作符?
List<Student> list = studentDao.queryBuilder() .where(StudentDao.Properties.MId.in(1,2,3)) .list();?
Log.e(TAG, "onCreate: "+list.size());??
// sql語句查詢
List<Student> list = studentDao.queryBuilder() .where(new??WhereCondition.StringCondition(StudentDao.Properties.ClassName.columnName+"=?","一年級")) .list();?
Log.e(TAG, "onCreate: "+list.size());?