美文网首页Android学习Android 实用小技巧
【Android】GreenDao建表(一)

【Android】GreenDao建表(一)

作者: 代码充电宝 | 来源:发表于2020-06-06 09:06 被阅读0次

    (1)概述

    • 官网地址:https://greenrobot.org/greendao/
    • 在我们所知的所有ORM中,greenDAO是最快的
    • 下图的比较主要针对Android greenDAO,OrmLite和ActiveAndroid的3种最流行的ORM框架
    image.pngimage.png
    • greenDAO支持加密数据库以保护敏感数据
    • 数据表中数据可以直接转换成Java类,Java类也可以直接储存数据库<br />
    image.pngimage.png

    <a name="EbaJc"></a>

    (2)项目配置

    • root下的build.gradle
    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
    buildscript {
        
        repositories {
            google()
            jcenter()
            mavenCentral() // add
            
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.6.3'
            classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add
            
    
            // 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
    }
    
    • app下的build.gradle
    • 此处额外添加了一个数据库可视化的工具android:debug-db
    • 数据库版本

    schemaVersion: 版本号,可用于数据库升级
    daoPackage: 自动生成实体的子路径
    targetGenDir: 自动生成实体的父路径

    apply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao' // add
    
    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.3"
    
        defaultConfig {
            applicationId "com.toycloud.greendao"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    
        greendao {
            //版本号,升级时可配置
            schemaVersion 1
            daoPackage 'com.toycloud.greendao.db'
            targetGenDir 'src/main/java'
        }
    
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
        implementation 'org.greenrobot:greendao:3.3.0' // add
        debugImplementation 'com.amitshekhar.android:debug-db:1.0.6' // add
    }
    
    

    <a name="qPVSE"></a>

    (3)建表

    • 编写实体类

    1、@Entity表示生成一个数据库
    2、@Id表示数据表的主键
    3、set/get文件不用写,自动生成

    package com.toycloud.greendao;
    
    import org.greenrobot.greendao.annotation.Entity;
    import org.greenrobot.greendao.annotation.Id;
    import org.greenrobot.greendao.annotation.Generated;
    
    @Entity
    public class Student {
    
        @Id
        private String mId;
        private String mName;
        private int mGender;
    
        @Generated(hash = 442482468)
        public Student(String mId, String mName, int mGender) {
            this.mId = mId;
            this.mName = mName;
            this.mGender = mGender;
        }
    
        @Generated(hash = 1556870573)
        public Student() {
        }
    
        public String getMId() {
            return this.mId;
        }
    
        public void setMId(String mId) {
            this.mId = mId;
        }
    
        public String getMName() {
            return this.mName;
        }
    
        public void setMName(String mName) {
            this.mName = mName;
        }
    
        public int getMGender() {
            return this.mGender;
        }
    
        public void setMGender(int mGender) {
            this.mGender = mGender;
        }
    }
    
    • 重写编译后,com.toycloud.greendao.db路径下生成三个文件
    image.pngimage.png
    • 浏览器中查看可视化数据库,此时还没有建表
    image.pngimage.png
    • 数据库初始化
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "demo.db", null);
            SQLiteDatabase db = helper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(db);
            DaoSession daoSession = daoMaster.newSession();
            StudentDao studentDao = daoSession.getStudentDao();
    
    • 此时发现数据表已经创建
    image.pngimage.png

    相关文章

      网友评论

        本文标题:【Android】GreenDao建表(一)

        本文链接:https://www.haomeiwen.com/subject/bqcnzhtx.html