GreenDao学习之一基础篇

概述

GreenDao是Android当中的高性能ORM框架。(其他的有OrmLite等)

项目地址:https://github.com/greenrobot/greenDAO

同时GreenDao还有一个子项目为GreenDao Code Generator:

GreenDao的核心类及其工作如下:

使用初始化

使用greendao添加greendao.jar;

(使用greendao-generator则需要添加greendao-generator.jar与freemarker.jar这一点在以后讲)

相关jar包可以通过maven中央仓库下载。

helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);//greendao会创建notes-db这个数据库db = helper.getWritableDatabase();daoMaster = new DaoMaster(db);daoSession = daoMaster.newSession();noteDao = daoSession.getNoteDao();使用示例:

Note note = new Note(null, noteText, comment, new Date());noteDao.insert(note);Log.d("DaoExample", "Inserted new note, ID: " + note.getId());noteDao.deleteByKey(id);

数据模型与代码生成一般情况下,你使用GreeDao需要创建两个项目,,一个是你的Android项目添加greendao.jar依赖,另外一个是普通的java se工程.添加greendao-generator.jar与freemarker.jar依赖。后者用于数据模型domain,dao,DaoMaster等代码的生成。

DoMaster与DaoSession及XxxDao以及实体类均会生成。DoMaster与DaoSession对应于你当前的数据模型。这两个类中每个数据模型生成的方法均不同

数据模型代码生成示例-使用greendao-generator:

Schema schema = new Schema(1, "de.greenrobot.daoexample");Entity note= schema.addEntity("Note");note.addIdProperty();note.addStringProperty("text").notNull();note.addStringProperty("comment");note.addDateProperty("date");new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");Schema代表你的数据库,Entity代表你要生成的数据表结构。向Entity添加属性相当于添加列结构。

注意:../DaoExample/src-gen路径必须存在否则会报错

模型化实体Entities

Schema:

Schema schema = new Schema(1, "de.greenrobot.daoexample");//第一个参数代表版本,第二个参数代表要生成代码的包名默认情况下Dao类与Test类是在一个包下,如果你想分开他们,可以这样:

schema.setDefaultJavaPackageTest("de.greenrobot.daoexample.test");schema.setDefaultJavaPackageDao("de.greenrobot.daoexample.dao");Schema对于Entity还有两个默认的标志Flags可以设置:

schema2.enableKeepSectionsByDefault();schema2.enableActiveEntitiesByDefault();EntitySchema可以用于添加Entity:

Entity user = schema.addEntity("User");为实体添加属性:

user.addIdProperty();user.addStringProperty("name");user.addStringProperty("password");user.addIntProperty("yearOfBirth");为实体添加主键

注意:greendao的主键支持目前并不完善,还处于开发中,但是我们可以使用下面的方式添加主键:

user.addIdProperty().primaryKey().autoIncrement();关于Java属性与对应的数据库表名列名命名的规则与区别Java中属性一般采用驼峰命名法。

你可以通过Entity.setTableName修改表名

表名/domain类名属性/列I属性/列II

JavaUsernamemyName

数据库USERNAMEMY_NAME

Inheritance, Interfaces, and Serializable对于继承:(不推荐)

myEntity.setSuperclass("MyCommonBehavior");推荐使用接口将一些公共的属性提取出来。

entityA.implementsInterface("C");entityB.implementsInterface("C");entityB.implementsSerializable();触发代码生成DaoGenerator daoGenerator = new DaoGenerator();daoGenerator.generateAll(schema, "../MyProject/src-gen");还可以指定第三个参数来将test代码分开。

Keep sections片段由于GreenDaoGenerator会在每次运行后覆盖原有的生成的实体代码,为了允许添加兵保留你自定义代码到你的实体当中,greendao使用"keep sections"来允许你添加,但是要先调用Schema的enableKeepSectionsByDefault()或者setHasKeepSections(true) .运行generator之后就会在生成的实体类当中生成如下注释,我们只需要往这些注释中添加自定义代码以后每次运行generator后都会保留这部分代码。// KEEP INCLUDES – put your custom includes here// KEEP INCLUDES END…// KEEP FIELDS – put your custom fields here// KEEP FIELDS END…// KEEP METHODS – put your custom methods here// KEEP METHODS END不要删除这些注释。SessionsDaoMaster and DaoSessiondaoMaster = new DaoMaster(db);daoSession = daoMaster.newSession();noteDao = daoSession.getNoteDao();注意数据库连接是属于DaoMaster的,每个Session都需要分配内存,对于实体,greendao采用对应的session缓存cache

Identity scope and session “cache”

greendao默认的行为是多个不同的查询返回同一个java objects,举例:从USER表中加载一个ID为42的对象,结果对于每一个查询都会返回同一个java对象。

另一个作用就是缓存实体。greendao是用weak reference在内存中保存实体,所以当再次加载时,greendao不会从数据库加载,而是直接返回该session缓存中的对象。

注意:一旦需要对其进行更改,及时你提交到了数据库,但是缓存中的对象数据仍然没有更新,这个时候需要你手动进行更新缓存中的对象。切记!!!

每个人在他的人生发轫之初,总有一段时光,

GreenDao学习之一基础篇

相关文章:

你感兴趣的文章:

标签云: