GreenDao学习笔记(引入GreenDao并创建数据库)

近期正在学习GreenDao,总结下。

Github地址:https://github.com/greenrobot/greenDAO

配置GreenDao

  1. 在工程的build.gradle中添加classpath。
buildscript {
    
    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
//此处添加greendao
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

       2. 在module的build.gradle中添加apply plugin并在dependencies中添加包。添加greendao的配置

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "demo.ldy.mvpdemo"
        minSdkVersion 20
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//引入greendao包
    implementation 'org.greenrobot:greendao:3.2.0'
}
greendao{
    schemaVersion=1
    daoPackage 'demo.ldy.mvpdemo.retrofitmvpmovies.dao.gen'
    targetGenDir 'src/main/java'
}

     schemaVersion 为数据库版本
    daoPackage 为dao所在的包名,(在project中可以是空的,会自动创建。也可以自己手动创建)
    targetGenDir 为自动生成的数据库文件的目录

   配置完成后点击sync,至此greendao已经配置完成。

创建数据库

  • 创建实体类
@Entity
public class User {
    @Id (autoincrement = true)
    private Long id ;
    private String name;
    private String age;
}

  然后make project自动生成数据库实体类

GreenDao学习笔记(引入GreenDao并创建数据库)_第1张图片

@Id注解表明Long类型的id为数据库表的主键。其中id的类型是Long不是long。

autoincrement = true 表示id是自增的。

项目目录:

GreenDao学习笔记(引入GreenDao并创建数据库)_第2张图片

包gen及包下DaoMaster、DaoSession、UserDao 三个类为自动生成。

  • 创建数据库管理类
public class GreenDaoManager {

    private DaoMaster.DevOpenHelper helper;
    private DaoSession daoSession;
    private DaoMaster daoMaster;
    private static GreenDaoManager greenDaoManager;
    private String DBNAME = "TEST_DB";

    public GreenDaoManager(){

    }


    public void init(Context context){
        helper = new DaoMaster.DevOpenHelper(context,DBNAME);
        daoMaster = new DaoMaster(helper.getWritableDb());
        daoSession = daoMaster.newSession();
    }

    public static GreenDaoManager getInstance(){
        if (greenDaoManager == null){
            greenDaoManager = new GreenDaoManager();
        }
        return greenDaoManager;
    }

    public DaoMaster.DevOpenHelper getDevOpenHelper(){
        return helper;
    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

    public  DaoSession getDaoSession() {
        return daoSession;
    }

    public void closeDB(){
        if (helper != null){
            helper.close();
        }
    }
}

在Application中初始化。

public class MyApplication extends Application {

    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        GreenDaoManager.getInstance().init(this);
    }
}

数据库创建完成,剩下的就是数据库操作了。

你可能感兴趣的:(GreenDao学习笔记(引入GreenDao并创建数据库))