GreenDao的基本使用以及遇到的坑

GreenDao的基本使用以及遇到的坑

  1. 前言
    相比較我上一篇博客Ormlite數據庫來說,個人認爲GreenDao的易用性強大得多。爲什麼?接着看。
  2. 配置
    在相應的位置添加上以下下內容,相信大家都懂。
(1)
    apply plugin: 'org.greenrobot.greendao'

    dependencies {
        compile 'org.greenrobot:greendao:3.0.1'
        compile 'org.greenrobot:greendao-generator:3.0.0'
    }

    greendao {      //和buildTypes同位置
        schemaVersion 1
        daoPackage 'com.xxx.model.greendao.gen'
        targetGenDir 'src/main/java'
    }
    (2)
    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.1.0'
            classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'
        }
    }

    allprojects {
        repositories {
            jcenter()
        }
    }

3.建包
在2中配置了com.xxx.model.greendao.gen,
在com.xxx.model.greendao下建包entity用於存放bean
- class上註釋@Entity
- 表id上註釋@Id
- 無需記入的數據上註釋@Transient
- 然後就可以build-Make Project

@Entity
public class ErrorInfo {
    private String ErrorInfoID;
    @Id
    private String AddTime;
    private String ErrorSource;
    private String ErrorDetalisInfo;
    @Transient
    public List<XX> xxList;       
}

4.系統生成
Make Project之後系統自動生成了DaoMaster,DaoSession,GreenDaoManager和對應的dao
生成
5. 相關使用代碼

DaoSession session = GreenDaoManager.getInstance().getSession();
    addProjectDao = session.getS_AddProjectDao();
    addProjects = addProjectDao.loadAll();

    QueryBuilder<S_Personal> qb = session.getS_PersonalDao().queryBuilder();
    List<S_Personal> list = qb
            .where(qb.and(S_PersonalDao.Properties.PersonName.eq(editTextuser.getText().toString())
                    ,S_PersonalDao.Properties.PersonPassword.eq(editTextpwd.getText().toString())))
            .build().list();

    List<S_LoginInfo> s_loginInfos = s_loginInfoDao.queryBuilder().where(S_LoginInfoDao.Properties.PersonID.eq(list.get(0).getPersonID())).build().list();
    if (s_loginInfos.size() == 0) {
        final S_LoginInfo s_LoginInfo = new S_LoginInfo();
        s_LoginInfo.setPersonID(list.get(0).getPersonID());
        s_LoginInfo.setLoginTime(CurrentTimes.getCurrentTime());

        s_LoginInfo.setWhether("未上傳");
        s_loginInfoDao.insertOrReplace(s_LoginInfo);
    }
    開啓事務
    session.runInTx(new Runnable(){
    S_SubwayLineDao dao = session.getS_SubwayLineDao();
                    List<S_SubwayLine> lineList = dao.queryBuilder().build().list();
                    for (S_SubwayLine s_subwayLine : lineList) {
                        dao.deleteInTx(s_subwayLine);
                    }
                    session.clear();

                    for (SubwayLine subwayLine : list) {

                        for (Interval interval : subwayLine.getIntervals()) {
                            S_SubwayLine s_SubwayLine = new S_SubwayLine();
                            s_SubwayLine.setIntervalId(interval.getIntervalId());
                            s_SubwayLine.setName(subwayLine.getName());
                            s_SubwayLine
                                    .setIntervalName(interval.getIntervalName());
                            dao.insertOrReplaceInTx(s_SubwayLine);
                        }
                    }
    });

6.使用中出現的坑
1. 查詢加遍歷,出現第二條查詢不到的問題,處理方法如下,循環中queryBuilder();
S_ProjectDao s_projectDao = session.getS_ProjectDao();
for(){
QueryBuilder queryBuilder = s_projectDao.queryBuilder();
}
2. 子線程中數據庫開啓事務,導致概率性不更新UI的問題。建議能不開啓事務,就別開啓,或者更新UI重新設計
3.


7.介紹就到這裏了,相關代碼有空上傳。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章