概述

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();

Entity

Schema可以用于添加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
Java User name myName
数据库 USER NAME MY_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
不要删除这些注释。

Sessions

DaoMaster and DaoSession

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

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android(安卓)NDK几点回调方式(device id & signature)
  3. Android自定义View
  4. Android开发:TabActivity中onKeyDown无法响应的解决方法
  5. 【Android(安卓)开发教程】Notification通知
  6. Android调用camera错误setParameters failed深层解析
  7. android典型代码系列(二十一)------根据文件后缀名获得对应的MIM
  8. Android实践 -- 监听应用程序的安装、卸载
  9. Android处理图像数据转换的各种方法

随机推荐

  1. [android] Bundle savedInstanceState的
  2. 使用ViewPager实现android软件使用向导的
  3. Android(安卓)最好用的模拟器 Genymotion
  4. Android@Home,会否一统家庭自动化产业标准
  5. 小羊驼和你一起学习cocos2d-x之六(lua、a
  6. Android(安卓)TextView等控件不为人知的
  7. 1.android开发AsyncTask异步发送HTTP请求
  8. [原创]HierarchyView的实现原理和Android
  9. Android(安卓)TextView设置多样式文本,跑
  10. Android中使用WindowManager在界面布局上