初识 greenDAO

1,080 阅读5分钟

    入职新公司因为公司业务需求被和谐到前端开发了,零前端基础的我也只能转变心态拥抱变化了,但是我不会因此而停下深入学习Android开发的步伐,我会继续前行。最近利用晚上临睡前的一两个小时在写一个记事本的小项目,其实项目是次要的,主要的是想练习一下最近比较热门的Android开发技术。因为有大量的数据库操作,所以我选择了一个第三方的开发框架——greenDAO.

    greenDAO官网给出的定义:greenDAO is an open source Android ORM making development for SQLite databases fun again. It relieves developers from dealing with low-level database requirements while saving development time. 大体意思就是说greenDAO是一个开源的用于简便操作SQLite数据库的安卓对象关系映射框架。如下图所示,greenDAO就是连接SQLite和Java对象的桥梁,它能简便的将对象映射到SQLite 数据库中。接下来阐述greenDAO的使用流程。


第一步:配置build.gradle文件

    greenDAO在github上的地址为 :https://github.com/greenrobot/greenDAO  ,当前版本是3.1.0,所以我目前的项目就是基于这个版本的。

    1、配置 Android 工程的 build.gradle(Project)文件,如图:


如图所示,红色箭头所指就是添加的代码,

    2、配置 Android 工程的 build.gradle(Module)文件,如图:


 如图所示,红色箭头所指部分就是要添加的内容,在此解释一下下面这段代码

greendao {
schemaVersion 1
   daoPackage 'com.meimei.note.greendao'
   targetGenDir 'src/main/java'
}

首先简单的介绍下通过gradle插件生成数据库代码的步骤:每次在make project之前,它会扫描项目中所有的@Entity文件(greenDAO中数据库的实体类),根据实体类生成DaoSession、DaoMaster以及所有实体类的dao类,生成的文件默认目录为:build/generated/source/greendao,若不想修改生成的路径,可以将此路径设置为资源目录。我们也可以自定义这个路径,而上面这一段代码就是自定义路径的代码。


  schemaVersion---->指定数据库schema版本号,迁移等操作会用到

    daoPackage-------->通过gradle插件生成的数据库相关文件的包名,默认为你的entity所在的包名

    targetGenDir-------->这就是我们上面说到的自定义生成数据库文件的目录了,可以将生成的文件放到我们的java目录中,而不是build中,这样就不用额外的设置资源目录了

由下图可以看到当我配置完gradle,并定义了一个叫MeiMeiNote的实体类之后点击Make Project按钮,就会在之前定义的目录com.meimei.note.greendao下生成相应的代码。


第二步:编写实体类,并生成相应的DAO 文件。

package com.meimei.note.model;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
* Created by pengdongyuan on 16/7/27.
*/

@Entity
public class MeiMeiNote {
@Id
   private long id;
   private String noteContent;
   private String noteTitle;
   private long noteTime;

}

这就是实体类的代码,跟JavaBean其实是一样的东西,但也有不同点,可以看到代码中有两个注解:

@Entity:将我们的java普通类变为一个能够被greenDAO识别的数据库类型的实体类

@Id:通过这个注解标记的字段必须是Long类型的,这个字段在数据库中表示它就是主键,并且它默认就是自增的

其他就是一些自定义的对象属性。编写完实体类之后我们点击Make Project按钮,稍后片刻我们会发现MeiMeiNote.class文件中的代码变成这样了:

package com.meimei.note.model;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
* Created by pengdongyuan on 16/7/27.
*/

@Entity
public class MeiMeiNote {
@Id
   private long id;
   private String noteContent;
   private String noteTitle;
   private long noteTime;

   public long getNoteTime() {
return this.noteTime;
   }

public void setNoteTime(long noteTime) {
this.noteTime = noteTime;
   }

public String getNoteTitle() {
return this.noteTitle;
   }

public void setNoteTitle(String noteTitle) {
this.noteTitle = noteTitle;
   }

public String getNoteContent() {
return this.noteContent;
   }

public void setNoteContent(String noteContent) {
this.noteContent = noteContent;
   }

public long getId() {
return this.id;
   }

public void setId(long id) {
this.id = id;
   }

@Generated(hash = 326616812)
public MeiMeiNote(long id, String noteContent, String noteTitle,
long noteTime) {
this.id = id;
       this.noteContent = noteContent;
       this.noteTitle = noteTitle;
       this.noteTime = noteTime;
   }

@Generated(hash = 997219563)
public MeiMeiNote() {
}

}

还有就是com.meimei.note.greendao目录下生成了相应的DAO文件,如图:

至此,实体类编写完毕,并生成了相应的代码。其中一个DaoMaster就代表着一个数据库的连接;DaoSession可以让我们使用一些Entity的基本操作和获取Dao操作类,DaoSession可以创建多个,每一个都是属于同一个数据库连接的。

第三步:操作数据库

编写数据库操作的工具类:

package com.meimei.note.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.meimei.note.greendao.DaoMaster;
import com.meimei.note.greendao.DaoSession;
import com.meimei.note.greendao.MeiMeiNoteDao;
import com.meimei.note.model.MeiMeiNote;

/**
* Created by pengdongyuan491 on 16/8/20.
*/
public class DBUtil {
private static final String DB_NAME = "meimeinote_db";
   private Context context;
   private static DBUtil mInstance;
   private DaoMaster.DevOpenHelper openHelper;

   private DBUtil(Context context) {
this.context = context;
       this.openHelper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
   }

public static DBUtil getInstance(Context context) {
if (mInstance == null) {
synchronized (DBUtil.class) {
if (mInstance == null) {
mInstance = new DBUtil(context);
               }
}
}
return mInstance;
   }

/**
    * 获取可读数据库
    */
   private SQLiteDatabase getReadableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
       }
SQLiteDatabase db = openHelper.getReadableDatabase();
       return db;
   }

private SQLiteDatabase getWritableDataBase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
       }
SQLiteDatabase db = openHelper.getWritableDatabase();
       return db;
   }

/*
   插入笔记
    */
   public void insertNote(MeiMeiNote meiMeiNote) {
DaoMaster daoMaster = new DaoMaster(getWritableDataBase());
       DaoSession daoSession = daoMaster.newSession();
       MeiMeiNoteDao meiMeiNoteDao = daoSession.getMeiMeiNoteDao();
       meiMeiNoteDao.insert(meiMeiNote);
   }

/*
   删除笔记
    */
   public void deleteNote(MeiMeiNote meiMeiNote) {
DaoMaster daoMaster = new DaoMaster(getWritableDataBase());
       DaoSession daoSession = daoMaster.newSession();
       MeiMeiNoteDao meiMeiNoteDao = daoSession.getMeiMeiNoteDao();
       meiMeiNoteDao.delete(meiMeiNote);
   }

/*
   更新笔记
    */
   public void updateNote(MeiMeiNote meiMeiNote) {
DaoMaster daoMaster = new DaoMaster(getWritableDataBase());
       DaoSession daoSession = daoMaster.newSession();
       MeiMeiNoteDao meiMeiNoteDao = daoSession.getMeiMeiNoteDao();
       meiMeiNoteDao.update(meiMeiNote);
   }

}

greenDAO 里面定义了数据库的增删改查操作,这里简单封装了一下增删改的方法,查询的方法我还没有去探究,至此关于greenDAO的基本使用方法讲述完毕。