Android学习Android 实用小技巧

【Android】GreenDao建表(一)

2020-06-06  本文已影响0人  代码充电宝

(1)概述

image.pngimage.png image.pngimage.png

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

(2)项目配置

// 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
}

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;
    }
}
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
上一篇 下一篇

猜你喜欢

热点阅读