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自动生成数据库实体类

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

autoincrement = true 表示id是自增的。

项目目录:

包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);
    }
}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值