GreenDao 3.0 基本使用


GreenDao是一款Android上的ORM框架(对象/关系映射),在数据库开发中能帮我们从枯燥的CRUD中节约大量的时间,这里针对GreenDao3.0以后的版本做一个简单的使用介绍。

1、引入

GreenDao Github地址
引入Gradle插件,
在根目录的build.gradle中,加入GreenDao的插件依赖地址:

    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.jakewharton:butterknife-gradle-plugin:10.2.1"
        
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // greendao 插件依赖
    }

在包含entity注解的实体类所在module的build.gradle中,加入使用插件:

    apply plugin: 'org.greenrobot.greendao'

在base module或者你的数据库管理类所在的module的build.gradle中加入基本依赖:

    api 'org.greenrobot:greendao:3.2.2'

GreenDao的基本配置:

greendao {
    //数据库的schema版本,也可以理解为数据库版本号,如果版本升级,数据库表字段发生变化,必须要升级
    schemaVersion 1
    //设置DaoMaster、DaoSession、Dao包名,也就是要放置这些类的包的全路径。可以不设置,使用默认值
    daoPackage 'tsou.com.simple.greendaoforkotlin.greendao'
    //设置DaoMaster、DaoSession、Dao目录 , 可以不设置,使用默认值
    targetGenDir 'src/main/java'
}

如果使用混淆,那么需要加入以下配置:

-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties { *; }

# If you DO use SQLCipher:
-keep class org.greenrobot.greendao.database.SqlCipherEncryptedHelper { *; }

# If you do NOT use SQLCipher:
-dontwarn net.sqlcipher.database.**
# If you do NOT use RxJava:
-dontwarn rx.**

这里引入的是3.2.2版本,这里提一句apiimplementation区别只是影响编译期的依赖传递,我是在base模块引入的,为了让应用模块都能正常使用,所以使用的是api来引入。
上述依赖都添加完成之后,写一个作为表的entity的case:

@Entity
public class Chapter {

    public Chapter(String name) {
        this.name = name;
    }

    /*
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值