GreenDao是一個將對象映射到SQLite數據庫中的輕量且快速的ORM數據庫框架,本文將講解如何集成GreenDao3.0環境,如何使用GreenDao自動生成代碼及註解的使用。
GreenDao官網
GreenDao GitHubjava
GreenDao相較於ORMLite等其餘數據庫框架有如下優點:
1. 一個精簡的庫
2. 性能最大化
3. 內存開銷最小化
4. 易於使用的 APIs
5. 對 Android 進行高度優化
而GreenDao3.0的版本主要使用註解方式定義實體類,經過gradle插件生成相應的代碼。相較於3.0以前的版本集成步驟更爲便捷,使用起來也更爲簡單。本文使用的版本爲3.2.0。android
GitHub中提示添加maven倉庫,可是AndroidStudio項目已經默認包含了jcenter倉庫,而jcenter倉庫就是maven倉庫的一個分支,所以不須要再添加倉庫,直接添加classPath便可。git
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}複製代碼
在app下build.gradle文件中咱們須要聲明GreenDao插件,及配置GreenDao生成dao路徑等信息,聲明GreenDao依賴。github
apply plugin: 'org.greenrobot.greendao'
......
greendao {
schemaVersion 1 //數據庫版本號
daoPackage 'com.holmeslei.greendaodemo.database' //設置時生成代碼的目錄
targetGenDir 'src/main/java' //設置DaoMaster、DaoSession、Dao目錄
//targetGenDirTest:設置生成單元測試目錄
//generateTests:設置自動生成單元測試用例
}
dependencies {
......
compile 'org.greenrobot:greendao:3.2.0'
}複製代碼
集成好使用環境後須要初始化實體,首先編寫實體類。這裏演示使用公司Company與僱員Employer實體例子。使用@Entity註解。莫急,註解機制在以後小節有詳細講解。數據庫
@Entity
public class Company {
@Id(autoincrement = true) //自增
private Long id; //主鍵
private String companyName; //公司名稱
private String industry; //行業
@ToMany(referencedJoinProperty = "companyId") //設置外鍵companyId
private List<Employee> employeeList; //公司與僱員創建一對多關係
}複製代碼
@Entity
public class Employee {
@Id(autoincrement = true)
private Long id; //主鍵
private long companyId; //指向Company主鍵
private String employeeName; //僱員姓名
private int salary; //薪水
}複製代碼
實體類與註解添加完畢後編譯項目,就會爲全部帶註解的實體生成Dao文件,及DaoMaster與DaoSession。若未在build.gradle中配置過則默認生成目錄爲build/generated/source。若配置過則生成在配置的目錄下。
app
3.0以後最大的不一樣就是使用的註解來配置實體類屬性,便捷且靈活。下面來看一下各種註解:框架
@Entity(
schema = "myschema",
active = true,
nameInDb = "AWESOME_USERS",
indexes = {
@Index(value = "name DESC", unique = true)
},
createInDb = false
)
public class Company {
......
}複製代碼
@Id(autoincrement = true)
設置自增加。@Property (nameInDb="name")
。@Entity
public class Company {
@Id(autoincrement = true)
private Long id;
@Index(unique = true)
private String industry;
}
@Entity
public class Employee {
@Id(autoincrement = true)
private Long id;
@Unique
private String employeeName;
}複製代碼
@Entity
public class Company {
@Id (autoincrement = true)
private Long id;
private long customerId;
@ToOne(joinProperty = "employeeId")
private Employee employee;
}
@Entity
public class Employee {
@Id(autoincrement = true)
private Long id;
private String employeeName;
}複製代碼
//第一種:Employee實體經過外鍵companyId指向Company實體的主鍵Id創建一對多關聯
@Entity
public class Company {
@Id(autoincrement = true)
private Long id;
private String companyName;
private String industry;
@ToMany(referencedJoinProperty = "companyId")
private List<Employee> employeeList;
}
@Entity
public class Employee {
@Id(autoincrement = true)
private Long id;
private long companyId;
private String employeeName;
private int salary;
}
//第二種:Employee以companyId爲外鍵,與Company非主鍵不爲空的鍵employeeTag創建一對多關聯。
@Entity
public class Company {
@Id(autoincrement = true)
private Long id;
private String companyName;
private String industry;
@ToMany(joinProperties = {
@JoinProperty(name = "employeeTag", referencedName = "companyId")
})
private List<Employee> employeeList;
}
@Entity
public class Employee {
@Id(autoincrement = true)
private Long id;
private String employeeName;
private int salary;
@NotNull
private String companyId;
}
//第三種:經過第三者實體類創建關聯,用的較少
@Entity
public class Employee {
@Id(autoincrement = true)
private Long id;
private String employeeName;
private int salary;
@ToMany
@JoinEntity(
entity = JoinEmployeeToCompany.class,
sourceProperty = "employeeId",
targetProperty = "companyId"
)
private List<Company> companyList;
}
@Entity
public class JoinEmployeeToCompany {
@Id(autoincrement = true)
private Long id;
private Long employeeId;
private Long companyId;
}
@Entity
public class Company {
@Id(autoincrement = true)
private Long id;
private String companyName;
private String industry;
}複製代碼
到此,這一篇關於GreenDao3.0的集成與註解就講解完畢了,歡迎參看下一篇關於如何使用GreenDao3.0對數據庫進行增刪改查。
技術渣一枚,有寫的不對的地方歡迎大神們留言指正,有什麼疑惑或者不懂的地方也能夠在我Github上GreenDaoDemo項目的Issues中提出,我會及時解答。附上GreenDaoDemo的地址:
GreenDaoDemomaven