GreenDAO使用手册

来源:互联网 发布:批量生成二维码软件 编辑:程序博客网 时间:2024/06/06 04:39

首页:http://greendao-orm.com/

GreenDAO的使用很像Java中常用的Hibernate框架


Pre-generated code and creating the table
Using the DaoMaster class you can aquire a convenience SQLiteOpenHelper:
new DaoMaster.DevOpenHelper(this"notes-db"null)

Inserting and deleting notes 
In the onCreate method we prepare a DAO object:

daoMaster = new DaoMaster(db);

daoSession = daoMaster.newSession();

noteDao = daoSession.getNoteDao();

Now have a look at the addNote method, how you insert a new note in the database:

Note note = new Note(null, noteText, comment, new Date());

noteDao.insert(note);

Log.d("DaoExample""Inserted new note, ID: " + note.getId());


Deleting a note is also straight forward; have a look at the onListItemClick method:

noteDao.deleteByKey(id);


Data model and code generation 
It contains a single class containing the data model definition in code:

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("../DaoExample/src-gen", schema);

0 0
原创粉丝点击