greenDAO 3.0簡明教程
配置環(huán)境
在項(xiàng)目gradle中:
-
一:配置倉庫
repositories { jcenter() mavenCentral() // 添加mavenCentral倉庫 } -
二:配置依賴
dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' // 添加greendao依賴 }
在模塊gradle中配置
-
一:配置首行:
apply plugin: 'org.greenrobot.greendao' -
二:配置依賴:
dependencies { compile 'org.greenrobot:greendao:3.2.0' <-- add library }
make project一下
對象介紹

DaoMatser
- 原文:
The entry point for using greenDAO. DaoMaster holds the database object (SQLiteDatabase) and manages DAO classes (not objects) for a specific schema. It has static methods to create the tables or drop them. Its inner classes OpenHelper and DevOpenHelper are SQLiteOpenHelper implementations that create the schema in the SQLite database.
- 大意:
用greenDAO的時候這個實(shí)體是重點(diǎn)。使用這個類可以用一個具體的模式持有和管理一個DAO類(而不是對象)。它有一個靜態(tài)的方法可以用來創(chuàng)建表和刪除它們。它是
OpenHelper和DevOpenHelper的內(nèi)部類,同時也是SQLiteOpenHelper的實(shí)現(xiàn)類,可以用來在SQLite數(shù)據(jù)庫中創(chuàng)建一個模式
DaoSession
- 原文:
Manages all available DAO objects for a specific schema, which you can acquire using one of the getter methods. DaoSession provides also some generic persistence methods like insert, load, update, refresh and delete for entities. Lastly, a DaoSession objects also keeps track of an identity scope. For more details, have a look at the session documentation.
- 大意:
用你獲得的任一getter類型的方法可以用一個具體的模式進(jìn)行管理所有可用的DAO對象。DAOSession也為實(shí)體類提供一些通常持續(xù)性的方法例如
insert,load,update,refresh和delete。最后,一個DaoSession對象也可以對一個實(shí)體作用域范圍進(jìn)行持續(xù)追蹤。關(guān)于具體的細(xì)節(jié),可以查看會話文檔。
前期準(zhǔn)備
-
編寫實(shí)體類,在數(shù)據(jù)庫中代表列的各個屬性值:
@Entity // 表明這是一個實(shí)體類 public class User { @Id // id序號,用來唯一標(biāo)示 private Long id; @NotNull // 字段屬性 private String name; private int age; } -
完成之后,需要執(zhí)行
Build->Make Project,會自動生成相應(yīng)的方法。@Entity public class User { @Id private long id; @NotNull private String name; private int age; @Generated(hash = 1198735498) public User(long id, @NotNull String name, int age) { this.id = id; this.name = name; this.age = age; } @Generated(hash = 586692638) public User() { } public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } }- 其中
@Generated部分是自動生成的。
- 其中
-
下面代碼只需執(zhí)行一次,建議在你的
Application類中執(zhí)行。helper = new DaoMaster.DevOpenHelper(this, "user.db", null); // 數(shù)據(jù)庫名稱,不帶后綴也行 db = helper.getWritableDatabase(); daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); // 本類中提供一個靜態(tài)方法獲取daoSession對象 -
在需要操作數(shù)據(jù)庫的類中執(zhí)行如下代碼:
mUserDao = MyApplication.getDaoSession().getUserDao();
實(shí)踐操作
-
插入數(shù)據(jù)
User user = new User(); user.setId(2); user.setAge(24); user.setName("張三"); mUserDao.insert(user); -
刪除數(shù)據(jù)
-
根據(jù)id進(jìn)行刪除,參數(shù)類型為
long類型:mUserDao.deleteByKey(Long.valueOf(1)); -
刪除所有數(shù)據(jù):
mUserDao.deleteAll();
-
-
修改數(shù)據(jù)
-
使用查詢到的唯一數(shù)據(jù),通過實(shí)體類的set方法進(jìn)行修改,然后更新即可。
mUserDao.update(user);
-
-
查詢數(shù)據(jù)
-
查詢唯一數(shù)據(jù)(當(dāng)數(shù)據(jù)結(jié)果不等于1時會報錯):
User unique = mUserDao.queryBuilder().where(UserDao.Properties.Age.eq(24)).unique(); -
查詢結(jié)果集:
List<User> list = mUserDao.queryBuilder().where(UserDao.Properties.Name.eq("haha")).list();
-
高級用法
- 未完待續(xù)。。。