关于GreenDao3.0的使用

来源:互联网 发布:linux tomcat 宕机 编辑:程序博客网 时间:2024/06/05 11:30

首先我们要考虑,为什么要使用GreenDao


根据这个图,我们可以简单的判断下自己到底需要什么,如果想知道更多的细节,请查询: http://greenrobot.org/android/android-orm-performance-2016/ 

说完为什么使用,我们就可以开始学习如何使用GreenDao


一、安装并配置环境

首先,我搭配的是Android Studio(AS)环境使用的GreenDao3.2.2,GreenDao的安装很简单,在AS里配置两个文件, AS就可以开始使用GreenDao了,并不需要下载GreenDao。
关于配置两个文件,分别是项目下的两个build.gradle
在项目主目录下的build.gradle,如下
buildscript {    repositories {        jcenter()        mavenCentral()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.3.3'        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    }}allprojects {    repositories {        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}

在app目录下的build.gradle,添加,其中加粗位置需要注意
apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao'android {    compileSdkVersion 25    buildToolsVersion "26.0.0"    defaultConfig {        applicationId "com.android.test001"        minSdkVersion 9        targetSdkVersion 25        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    greendao {        schemaVersion 1 //数据库版本号        daoPackage 'com.usher.greendao_demo.greendao.gen' //自动生成的工具类的包名        targetGenDir 'src/main/java' //路径    }}dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    testCompile 'junit:junit:4.12'    compile 'org.greenrobot:greendao:3.2.2'}

添加完之后AS会提醒同步,同步完成之后,就可以使用GreenDao了

二、开始使用

首先我们要建立一个Entity类,也就是实体类,每一个实体类就相当于数据库中的一个表,而类中的每个属性就相当于表的一个列;而在GreenDao3.0以上,我们可以使用注解的方式告诉AS这是一个特殊的类,是针对GreenDao建表使用的
@Entitypublic class Patient {    @Id(autoincrement = true)    private Long idPatient;    private String firstName;    private String middleName;    private String lastName;}

有几个属性非常重要:
@Entity 提示这是针对GreenDao
@Id 提示下一行必须是Long,并且是主键
   (autoincrement = true)主键自增长

创建好类后,点击Android studio上的Build----make project,系统就会自动生成三个文件DaoMaster, DaoSession和关于你自建类的PatientDao
然后就可以进行一些基本操作

由于GreenDao是相对于一个类进行建表的,所以对表进行添加记录,删除记录,更新记录,查询记录,这些操作在代码上看都是相对于类进行的操作
比如插入一条记录
    public void insertUser(Patient patient) {        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());        DaoSession daoSession = daoMaster.newSession();        PatientDao patientDao = daoSession.getPatientDao();        patientDao.insert(patient);    }
其余的比如删除记录,更新记录都类似,都是使用PatientDao进行操作

三、进阶使用


在我们正常的使用中,一般都是多表在一起进行搭配使用,这就要考虑到一一对应,一对多,多对多

比如一一对应,一个人只有一个头,一对多,一个学生有好几个科目成绩,一群老师和一群学生是多对多的

最简单的一一对应,
 @ToOne(joinProperty = ”属性名“)

这就是简单的一一对应,在使用joinProperty时,后面的属性名最好是long或者int型
@Entitypublic class People{    @Id(autoincrement = true)    private Long id;    private String name;    private long idHead;    @ToOne(joinProperty = "idHead")    private Head head;}
@Entitypublic class Head {    @Id(autoincrement = true)    private Long id;}
也就是说,在People中定义了一个外键,并通过@ToOne告诉GreenDao关于这个外键我要指向的类,一样进行重新make project,系统会自动生成代码

一对多(简单分为学生Student和成绩Score),使用@ToMany分为两种情况,
1.Score的主键在Student中,并当做外键使用
    @ToMany(referencedJoinProperty = “scoreId”)

2.Student中并不存在Score的主键,我想自己在Score中定义一个外键scoreSn,然后在Studeng中定义一个对应的studentSn,这样外键可以使用String型
    @ToMany(joinProperties = {            @JoinProperty(name = "scoreSn", referencedName = "studentSn")    })

原创粉丝点击