Android GreenDao 集成

2019-05-05  本文已影响0人  炫子_260f

初始配置

1. project.gradle
buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
2. modle.gradel
apply plugin: 'org.greenrobot.greendao'
 
dependencies {
    implementation 'org.greenrobot:greendao:3.2.2' // add library
}
//生成目录与版本,升级的时候,schemaVersion +1
//自动生成目录 com.greendao.gen
greendao {
    schemaVersion 1
    daoPackage 'com.greendao.gen'
    targetGenDir 'src/main/java'
}

以上就配置好了GreenDao

使用

@Entity
public class ContactsEntity {
    @Id(autoincrement = true)
    public Long id;
    @Unique
    public String username;
    public String alias;
    public String conRemark;
    public String nickname;
    public String pyInitial;
    public String quanPin;
    public int showHead;
    public String conRemarkPYFull;
    public String conRemarkPYShort;
    public int type;
    public int verifyFlag;
    public int imgflag;
    public String reserved1;
    public String reserved2;
    public String md5;
}

Dao文件编写好后, 在这个文件上 Ctrl + F9 就可以自动生成实体类了

参考

github
官方 doc
一篇好文之Android数据库 GreenDao的完全解析
Android实战——GreenDao3.2的使用,爱不释手
GreenDao 3.2.0 的基本使用

上一篇 下一篇

猜你喜欢

热点阅读