GreenDAO 3.0 使用方式
GreenDAO 3.0 使用方式#
greenDAO是一个对象关系映射(ORM)的框架,能够提供一个接口通过操作对象的方式去操作关系型数据库,
它能够让你操作数据库时更简单、更方便。如下图所示
官网地址:http://greenrobot.org/greendao/
github:https://github.com/greenrobot/greenDAO
greendao官方介绍:http://greenrobot.org/greendao/documentation/
GreenDao 优点:
- 性能高,号称Android最快的关系型数据库
- 内存占用小
- 库文件比较小,小于100K,编译时间低,而且可以避免65K方法限制
- 支持数据库加密,greendao支持SQLCipher进行数据库加密
GreenDao 3.0最大的变化就是采用注解的方式通过编译方式生成Java数据对象和DAO对象
使用方式
1.配置
gradle插件,在Project的build.gradle添加如下配置:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' //添加greendao用到的gradle插件
}
}
添加greenDao依赖,在model的build.gradle添加如下配置:
apply plugin: 'org.greenrobot.greendao' //应用greendao的gradle插件
...
greendao {
schemaVersion 2 //数据库版本
targetGenDir 'src/main/java' //生成的Dao包路径
daoPackage 'com.wangzhaoxia1.greendaodemo.greendao' //默认包名是程序包名
}
dependencies {
compile 'org.greenrobot:greendao:3.2.2' //添加greenDao依赖
compile 'net.zetetic:android-database-sqlcipher:3.5.7' //如果数据库需要加密,引入sqlcipher加密库
}
greendao的一些配置信息(可选):
schemaVersion:数据库schema版本,也可以理解为数据库版本号
targetGenDir:设置DaoMaster 、DaoSession、Dao的目录,默认路径是 ...\app\build\generated\source
daoPackage:设置DaoMaster 、DaoSession、Dao包名,默认是实体类的包名
targetGenDirTest:设置生成单元测试目录
generateTests:设置自动生成单元测试用例
2.定义实体
GreenDao 3.0使用注解来定义实体
@Entity //实体注解
public class Note {
@Id(autoincrement = true) //主键,自增
private Long id;
@NotNull
private String text;
private Date date;
private String comment;
}
配置好greenDAO,定义完实体类后,编译一下(Build->Make Project)将自动生成GreenDao框架的核心类:DaoMaster 、DaoSession、实体对应的Dao
核心类介绍:
DaoMaster
该类是greenDAO的入口;
该类持有数据库对象(SQLiteDatabase)并管理实体DAO类(比如NoteDao);
包含创建和删除数据表的静态方法;
其内部类OpenHelper和DevOpenHelper是SQLiteOpenHelper的具体实现,由它们创建SQLite数据库的schema。
DaoSession
管理所有可用的Dao对象,DaoSession提供了通用持久化方法insert,load,update,refresh,delete。
DAOs(Data access objects)
DAOs可以被持久访问,用于查询实体;
对于每一个实体类(JavaBean),greenDAO都会为其生成一个DAO(如Note类,对应NoteDao);
DAO有比DaoSession更多的持久化方法,比如:count, loadAll, insertInTx
greendao还会为实体类生成构造器和getter/setter方法
3.在Application中配置DaoSession
初始化数据库和greendao核心类,初始化一次就行,比如在Application类初始化
在App中配置DaoSession,作为数据统一操作的对象
public class MyApplication extends Application {
/** 是否加密标志,greendao采用SQLCipher进行数据库加密 */
public static final boolean ENCRYPTED = false;
/** 全局公用的DaoSession **/
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
//DevOpenHelper在升级数据库时会重建数据表,在实际项目中,如果我们需要保存旧版本的数据,
//需要自己实现OpenHelper,把旧版本数据迁移到新版本中。
DevOpenHelper helper = new DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
}
提醒:记得在AndroidManifest.xml中配置MyApplication.
到这里,实体类的声明、数据库操作入口都已经建立好了,接下来演示一下如何使用greendao对数据进行增删改查
4.使用greendao操作数据库
获取实体Dao
所有对数据的操作都可通过Dao执行,DaoSession也提供了一些对数据操作的方法,使用这些方法时不用获取Dao对象,因为方法内部会为我们做这些工作。
//获取noteDao,通过noteDao对数据进行操作
DaoSession daoSession = ((MyApplication) getApplication()).getDaoSession();
NoteDao noteDao = daoSession.getNoteDao();
添加记录
Note note = new Note();
note.setText("这是note 内容文本");
note.setComment("这是note的comment");
note.setDate(new Date());
noteDao.insert(note);
在创建Note对象时,我们没有传递id,在从插入返回之前,DAO会自动设置新id
删除记录
noteDao.deleteByKey(id);
更新记录
note.setText("This note has changed.");
noteDao.update(note);
修改完note对象的属性值后,调用Dao的update()