GreenDao使用方式

本文介绍了如何在项目中使用GreenDao,从配置gradle,到MyApplication的初始化,再到Bean类的设计,如@Entity注解的使用,以及@Id的设置。文章还提到了@Convert在处理复杂数据类型如子类或list时的作用,并指出通过构建项目,GreenDao会自动生成相关代码和Dao文件,简化开发流程。
摘要由CSDN通过智能技术生成

用过litepal,抛弃litepal是因为多层级时数据插入查询太慢了,greendao也没有进项大量数据测试,也是尝试阶段。
1.项目gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}

app.gradle

apply plugin: 'org.greenrobot.greendao'
android{
    greendao {
        //数据库版本号,升级时修改
        schemaVersion 1
        //生成的DAO,DaoMaster和DaoSession的包路径。默认与表实体所在的包路径相同
        daoPackage '你的包名'
        //生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)
        targetGenDir 'src/main/java'
    }
    }
dependencies {
    implementation 'org.greenrobot:greendao:3.2.0'
    implementation 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1'
    implementation 'com.google.code.gson:gson:2.6.2'
}

2.MyApplication要进行初始化

    public static MyApplication getApplication() {
        return application;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    private void initGreenDao() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "greenDbbao.db");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

3.Bean类
Person.class
@Entity是将类进行greendao自动生成标识
@Id 是表key ,long型,我习惯单独定义方便控制
@Convert是将子类或者list转换为String
Student_Convert 和StudentList_Convert要提前写好

package com.tayh.buttontest.greendao;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.gr
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值