标签:
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);
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添加属性相当于添加列结构。
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 user = schema.addEntity("User");为实体添加属性:
user.addIdProperty(); user.addStringProperty("name"); user.addStringProperty("password"); user.addIntProperty("yearOfBirth");
user.addIdProperty().primaryKey().autoIncrement();
表名/domain类名 | 属性/列I | 属性/列II | |
Java | User | name | myName |
数据库 | USER | NAME | MY_NAME |
myEntity.setSuperclass("MyCommonBehavior");推荐使用接口将一些公共的属性提取出来。
entityA.implementsInterface("C"); entityB.implementsInterface("C"); entityB.implementsSerializable();
DaoGenerator daoGenerator = new DaoGenerator(); daoGenerator.generateAll(schema, "../MyProject/src-gen");还可以指定第三个参数来将test代码分开。
// 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不要删除这些注释。
daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); noteDao = daoSession.getNoteDao();注意数据库连接是属于DaoMaster的,每个Session都需要分配内存,对于实体,greendao采用对应的session缓存cache
标签:
原文地址:http://blog.csdn.net/xbynet/article/details/45393483