Android ORM-GreenDao学习之一基础篇

概述

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

同时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
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
不要删除这些注释。

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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值