介紹
- 據(jù)說是Android中最快的的Orm革答。
GreenDao 優(yōu)點
- 性能高,號稱Android最快的關(guān)系型數(shù)據(jù)庫
- 內(nèi)存占用小
- 庫文件比較小参淫,小于100K,編譯時間低愧杯,而且可以避免65K方法限制
- 支持數(shù)據(jù)庫加密 greendao支持SQLCipher進行數(shù)據(jù)庫加密 有關(guān)SQLCipher可以參考這篇博客Android數(shù)據(jù)存儲之Sqlite采用SQLCipher數(shù)據(jù)庫加密實戰(zhàn)
- 簡潔易用的API
GreenDao 3.0基本使用
在.gradle添加依賴
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
apply plugin: 'org.greenrobot.greendao'
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
}
需要配置數(shù)據(jù)庫的基本屬性
greendao {
schemaVersion 1
targetGenDir 'src/main/java'
}
- schemaVersion:數(shù)據(jù)庫版本號
- daoPackage:設(shè)置DaoMaster, DaoSession, Dao包名
- targetGenDir: 設(shè)置DaoMaster, DaoSession, Dao目錄
- targetGenDirTest: 設(shè)置生成單元測試目錄
- generateTests 設(shè)置自動生成單元測試用例
創(chuàng)建實體
@Entity()
public class User {
@Id
private Long id;
private String name;
private int age;
//省去了get/set方法
}
實體@Entity注解:
- schema:表示GreenDao當前實體屬于哪個schema
- active: 標記一個實體處于活動狀態(tài)涎才,活動實體有更新,刪除和刷新方法
- nameInDb: 在數(shù)據(jù)中使用的別名力九,默認使用的是實名的類別
- indexes: 定義索引耍铜,可以跨越多個列
- createInDb: 標記創(chuàng)建數(shù)據(jù)庫表
基礎(chǔ)屬性注解:
- @Id: 主鍵 Long型,可以通過@Id(autoincrement = true)設(shè)置自增長
- @Property: 設(shè)置一個非默認關(guān)系映射所對應的列名跌前,默認是的使用字段名 舉例:@Property (nameInDb="name")
- @NotNul:設(shè)置數(shù)據(jù)庫表當前列不能為空
- @Transient :添加次標記之后不會生成數(shù)據(jù)庫表的列
索引注解:
- @Index:使用@Index作為一個屬性來創(chuàng)建一個索引棕兼,通過name設(shè)置索引別名,也可以通過unique給索引添加約束
- @Unique:向數(shù)據(jù)庫列添加了一個唯一的約束
關(guān)系注解:
- @ToOne:定義與另一個實體(一個實體對象)的關(guān)系
- @ToMany:定義與多個實體對象的關(guān)系
編譯生成DaoMaster, DaoSession, Dao
- 創(chuàng)建一個數(shù)據(jù)庫管理者單例
public class DBManager {
private final static String dbName = "test_db";
private static DBManager mInstance;
private DaoMaster.DevOpenHelper openHelper;
private Context context;
public DBManager(Context context) {
this.context = context;
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
/**
* 獲取單例引用
*
* @param context
* @return
*/
public static DBManager getInstance(Context context) {
if (mInstance == null) {
synchronized (DBManager.class) {
if (mInstance == null) {
mInstance = new DBManager(context);
}
}
}
return mInstance;
}
}
- 獲取可讀可寫數(shù)據(jù)庫
/**
* 獲取可讀數(shù)據(jù)庫
*/
private SQLiteDatabase getReadableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
return openHelper.getReadableDatabase();
}
/**
* 獲取可寫數(shù)據(jù)庫
*/
private SQLiteDatabase getWritableDatabase() {
if (openHelper == null) {
openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
}
return openHelper.getWritableDatabase();
}
/**
* 插入一條記錄
*
* @param user
*/
public void insertUser(User user) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.insert(user);
}
/**
* 插入用戶集合
*
* @param users
*/
public void insertUserList(List<User> users) {
if (users == null || users.isEmpty()) {
return;
}
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.insertInTx(users);
}
/**
* 刪除一條記錄
*
* @param user
*/
public void deleteUser(User user) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.delete(user);
}
/**
* 更新一條記錄
*
* @param user
*/
public void updateUser(User user) {
DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
userDao.update(user);
}
/**
* 查詢用戶列表
*/
public List<User> queryUserList() {
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
QueryBuilder<User> qb = userDao.queryBuilder();
return qb.list();
}
/**
* 查詢用戶列表
*/
public List<User> queryUserList(int age) {
DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
UserDao userDao = daoSession.getUserDao();
QueryBuilder<User> qb = userDao.queryBuilder();
qb.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age);
return qb.list();
}
外鍵使用(@ToOne, @ToMany)
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
@NotNull
private String name;
private int age;
private Long blogId;
@ToOne(joinProperty = "blogId")
private Blog blog;
}
- joinProperties這個參數(shù)是referencedJoinProperty 參數(shù)的升級版抵乓。在referencedJoinProperty參數(shù)中我們發(fā)現(xiàn)倆個實體關(guān)聯(lián)的外鍵是CustomerId與id伴挚,但是如果我們的需求是外鍵不能通過id來定義,需要用自己自定義屬性來定義灾炭,第一種方法就沒法用了茎芋,而joinProperties就是為了解決這個需求的。