Android ORM-GreenDao學習之一基礎篇
來源:程序員人生 發布時間:2015-06-19 08:49:18 閱讀次數:3984次
概述
GreenDao是Android當中的高性能ORM框架。(其他的有OrmLite等)
項目地址:https://github.com/greenrobot/greenDAO
同時GreenDao還有1個子項目為GreenDao Code Generator:
GreenDao的核心類及其工作以下:
使用初始化
使用greendao添加greendao.jar;
(使用greendao-generator則需要添加greendao-generator.jar與freemarker.jar這1點在以后講)
相干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);
數據模型與代碼生成
1般情況下,你使用GreeDao需要創建兩個項目,1個是你的Android項目添加greendao.jar依賴,另外1個是普通的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");//第1個參數代表版本,第2個參數代表要生成代碼的包名
默許情況下Dao類與Test類是在1個包下,如果你想分開他們,可以這樣:
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中屬性1般采取駝峰命名法。
你可以通過Entity.setTableName修改表名
|
表名/domain類名 |
屬性/列I |
屬性/列II |
Java |
User |
name |
myName |
數據庫 |
USER |
NAME |
MY_NAME |
Inheritance, Interfaces, and Serializable
對繼承:(不推薦)
myEntity.setSuperclass("MyCommonBehavior");
推薦使用接口將1些公共的屬性提取出來。
entityA.implementsInterface("C");
entityB.implementsInterface("C");
entityB.implementsSerializable();
觸發代碼生成
DaoGenerator daoGenerator = new DaoGenerator();
daoGenerator.generateAll(schema, "../MyProject/src-gen");
還可以指定第3個參數來將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默許的行動是多個不同的查詢返回同1個java objects,舉例:從USER表中加載1個ID為42的對象,結果對每個查詢都會返回同1個java對象。
另外一個作用就是
緩存實體。greendao是用weak reference在內存中保存實體,所以當再次加載時,greendao不會從
數據庫加載,而是直接返回該session緩存中的對象。
注意:1旦需要對其進行更改,及時你提交到了數據庫,但是緩存中的對象數據依然沒有更新,這個時候需要你手動進行更新緩存中的對象。切記!!!
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈