GreenDao的初步探索

GreenDao的初步探索

開(kāi)發(fā)環(huán)境:AS3.1.4,GreenDao3.2.2

1,配置

項(xiàng)目的build.gradle中,

dependencies {

? ? classpath 'com.android.tools.build:gradle:3.1.4'

? ? classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'?

}


module的build.gradle中,

apply plugin:'com.android.application'

apply plugin:'org.greenrobot.greendao'

greendao {

? ? schemaVersion 1? ??//數(shù)據(jù)庫(kù)版本號(hào)

? ? daoPackage'com.example.mytest.gen'? ??//數(shù)據(jù)庫(kù)包名

? ? targetGenDir'src/main/java'??? ? //數(shù)據(jù)庫(kù)目錄

}

implementation 'org.greenrobot:greendao:3.2.2'

2,創(chuàng)建User類

新建一個(gè)User類

@Entity

public class User {

? ? @Id

? ? private Long id;?

? ? private String name;

}

@Entity:被Entry注解的JavaBean才能被GreenDao操控。

@ID:相當(dāng)于主鍵,最好用long型。

Build后,在上面填寫(xiě)的gen目錄下就有了三個(gè)文件夾。

3,創(chuàng)建數(shù)據(jù)庫(kù)管理類

public class DaoManager {

? ? private static final String DB_NAME = "db_test";

? ? private static final String TAG = DaoManager.class.getSimpleName();

? ? private static volatile DaoManager daoManager;? //多線程 使用單例模式

? ? private static DaoMaster daoMaster;

? ? private static DaoSession daoSession;

? ? private static DaoMaster.DevOpenHelper helper;

? ? private Context context;

? ? private static final DaoManager instance = new DaoManager();

? ? /*

? ? * 構(gòu)造方法* */

? ? public DaoManager() {

}

? ? /*

? ? * 使用單例模式 保證數(shù)據(jù)庫(kù)的安全* */

? ? public static DaoManager getInstance() {

? ? ? ? return instance;

}

? ? /*

? ? * 初始化daomanager

* */

? ? public void init(Context context) {

? ? ? ? this.context = context;

}

? ? /**

? ? * 判斷是否存在數(shù)據(jù)庫(kù),如果沒(méi)有則創(chuàng)建數(shù)據(jù)庫(kù)

? ? *

? ? * @return

? ? */

? ? public DaoMaster getDaoMaster() {

? ? ? ? if (daoMaster == null) {

? ? ? ? ? ? helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);

? ? ? ? ? ? daoMaster = new DaoMaster(helper.getWritableDatabase());

}

? ? ? ? return daoMaster;

}

? ? /**

? ? * 完成對(duì)數(shù)據(jù)庫(kù)的添加 修改 查詢 的操作 僅僅是一個(gè)接口

? ? *

? ? * @return

? ? */

? ? public DaoSession getDaoSession() {

? ? ? ? if (daoSession == null) {

? ? ? ? ? ? if (daoMaster == null) {

? ? ? ? ? ? ? ? daoMaster = getDaoMaster();

}

? ? ? ? ? ? daoSession = daoMaster.newSession();

}

? ? ? ? return daoSession;

}

? ? /*

? ? * 是否打印 數(shù)據(jù)庫(kù)的日志信息 默認(rèn)不開(kāi)啟* */

? ? public void setDebug(Boolean isDebug) {

? ? ? ? if (isDebug) {

? ? ? ? ? ? QueryBuilder.LOG_SQL = true;

? ? ? ? ? ? QueryBuilder.LOG_VALUES = true;

? ? ? ? } else {

? ? ? ? ? ? QueryBuilder.LOG_SQL = true;

? ? ? ? ? ? QueryBuilder.LOG_VALUES = true;

}

}

? ? /*

? ? * 關(guān)閉help

* */

? ? public void closeHelper() {

? ? ? ? if (helper != null) {

? ? ? ? ? ? helper.close();

? ? ? ? ? ? helper = null;

}

}

? ? /*

? ? * 關(guān)閉 會(huì)話* */

? ? public void closeSession() {

? ? ? ? if (daoSession != null) {

? ? ? ? ? ? daoSession.clear();

? ? ? ? ? ? daoSession = null;

}

}

? ? /*

? ? *? 關(guān)閉所有的操作 數(shù)據(jù)庫(kù)用完的時(shí)候必須關(guān)閉 節(jié)省資源*/

? ? public void closeConnection() {

? ? ? ? closeHelper();

? ? ? ? closeSession();

}

}

4,增刪改查

UserDao userDao = DaoManager.getInstance().getDaoSession().getUserDao();

插入:

userDao.insert(u);

userDao.insertOrReplace(u);

批量插入:

List list_u = new ArrayList<>();

userDao.insertInTx(list_u);

更新:

userDao.update(u);

批量更新:

userDao.updateInTx(list_u);

刪除:

userDao.deleteByKey("2");

userDao.delete(u);

刪除所有:

userDao.deleteAll();

查詢ID=1的數(shù)據(jù):

QueryBuilder userQueryBuilder = userDao.queryBuilder();

List list = userQueryBuilder.where(UserDao.Properties.Id.eq("1")).list();

查詢?nèi)浚?/p>

List users= userDao.loadAll();

//ID等于1并且name是張三的對(duì)象

User unique = userQueryBuilder.where(Properties.Id.eq(1)).where(Properties.Name.eq("zhangsan")).build().unique();

User zhangsan = userQueryBuilder.where(Properties.Id.eq(1), Properties.Name.eq("zhangsan")).build().unique();

//ID大于10的數(shù)據(jù)的個(gè)數(shù)

long count = userQueryBuilder.where(Properties.Id.ge(10)).buildCount().count();

//ID小于9的數(shù)據(jù)

List list_low9 = userQueryBuilder.where(Properties.Id.le(9)).build().list();

//ID在1-13之間,取5條數(shù)據(jù)

userQueryBuilder.where(Properties.Id.between(1, 13)).limit(5).build().list();




最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    wgl0419閱讀 6,600評(píng)論 1 9
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,892評(píng)論 2 45
  • 附上原文作者連接:作者:金誠(chéng) 一.榜單介紹 排行榜包括四大類: 單一框架:僅提供路由、網(wǎng)絡(luò)層、UI層、通信層或其他...
    這個(gè)美嘉不姓陳閱讀 2,328評(píng)論 1 35
  • 請(qǐng)?jiān)试S我借鑒前輩們的東西~~~~ 感激不盡~~~~~ 以下為Android 框架排行榜 么么噠~ Android...
    嗯_(tái)新閱讀 2,533評(píng)論 3 32
  • R:片段2《刻意練習(xí)》——即時(shí)反饋 傳道,授業(yè),解惑,老師和教練最大的用處是什么?也許對(duì)一般人來(lái)說(shuō)小學(xué)老師最大的作...
    草履蟲(chóng)2008閱讀 314評(píng)論 1 1

友情鏈接更多精彩內(nèi)容