Android資料庫框架greenDao學習筆記 2
引言
上篇部落格中介紹了greenDao的整合方式,這篇部落格,我們介紹如何使用greenDao建立我們需要的資料表。補一張圖(來自官網),來理解greenDao,大家意會吧。
註解
先上一張圖,來對greenDao的註解有一個直觀的認識:
Schema
通過在Gradle檔案中進行配置,就無需再額外配置,它的配置選項主要有以下幾個:
- schemaVersion:資料庫最新的版本號
- daoPackage:生成Daos的目錄
- targetGenDir:儲存生成程式碼的路徑,一般不做配置,預設
build/generated/source/greendao
- generateTests:是否生成單元測試程式碼,值為ture 和false,預設是ture
-
targetGenDirTests:生成測試原始碼的路徑,預設
src/androidTest/java
配置樣例:
Entity
定義實體類,常需要用到的註解
@Entity
註解 @Entity
是用來將Java Object對映成為資料庫一張表的註解,用法如下(在資料庫中生成一張User表):
@Entity
public class User {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount; // not persisted
// getters and setters for id and user ...
}
註解 @Entity
支援更詳細的引數配置,如下所示:
@Entity(
// If you have more than one schema, you can tell greenDAO
// to which schema an entity belongs (pick any string as a name).
//資料庫物件集合,一般不做配置,如果使用gradle配置了Schema,這裡是不生效的
schema = "myschema",
// Flag to make an entity "active": Active entities have update ,
// delete, and refresh methods.
//是否啟用該實體,Active的實體會自動生成更新、刪除和重新整理的方法
active = true,
// Specifies the name of the table in the database.
// By default, the name is based on the entities class name.
//該實體對應的表名,預設為實體類名
nameInDb = "AWESOME_USERS",
// Define indexes spanning multiple columns here.
//索引
indexes = {
@Index(value = "name DESC", unique = true)
},
// Flag if the DAO should create the database table (default is true).
// Set this to false, if you have multiple entities mapping to one table,
// or the table creation is done outside of greenDAO.
//是否建立資料庫表,預設是true
createInDb = false,
// Whether an all properties constructor should be generated.
// A no-args constructor is always required.
//是否生成建構函式
generateConstructors = true,
// Whether getters and setters for properties should be generated if missing.
//是否生成get、set方法
generateGettersSetters = true
)
public class User {
...
}
欄位屬性(property)
@Id
選取一個Long或者long型的欄位作為實體的ID,它有一個引數 autoincrement
用來標註ID的Value是否自增長。用法示例如下:
@Entity
public class Assert {
@Id(autoincrement = true)
private long id;
}
@Property
用於定義欄位的屬性,配置非預設欄位名,只有一個引數 nameInDb
,用法如下:
@Entity
public class Assert {
@Id(autoincrement = true)
private long id;
@Index(name = "index",unique = true )
@Property(nameInDb = "NAME")
private String name;
}
@NotNull
標註一個欄位值不能為空,示例用法如下:
@Entity
public class Assert {
@Id(autoincrement = true)
private long id;
@Index(name = "index",unique = true )
@Property(nameInDb = "NAME")
@NonNull
private String name;
}
Transient
標記一個欄位不進行資料庫對映,用法示例:
@Entity
public class Assert {
@Id(autoincrement = true)
private long id;
@Index(name = "index",unique = true )
@Property(nameInDb = "NAME")
@NonNull
private String name;
@Transient
private String memo;
}
@Index
用於為資料表中某一欄位建立索引,有兩個引數 name
和 unique
需要進行配置,分表表示自定義索引名稱和強制要求所有的值唯一。示例用法如下:
@Entity
public class Assert {
@Id(autoincrement = true)
private long id;
@Index(name = "index",unique = true )
private String name;
}
@Unique
用於表示某一欄位值唯一,同時SQLite會隱式的為該欄位建立索引,示例用法如下:
@Entity
public class Assert {
@Id(autoincrement = true)
private long id;
@Index(name = "index",unique = true )
private String name;
@Unique
private String memo;
}
關聯註解(Relations)
資料庫表與表之間的關係常常需要表示,1對1、1對多以及多對多的關係,這時候就需要用到關聯註解來表示,下面著重來說一下。
@ToOne
用於標註與另一實體的關聯的關係,用於標註在一個欄位上去關聯對應的一個實體,示例用法如下:(表示一個訂單隻能關聯一個顧客)
@Entity
public class Order {
@Id private Long id;
private long customerId;
@ToOne(joinProperty = "customerId")
private Customer customer;
}
@Entity
public class Customer {
@Id private Long id;
}
@ToMany
用於標註一個欄位與多個實體關聯,表示1對多關係,示例用法如下(一個顧客有多個訂單):
@Entity
public class Customer {
@Id private Long id;
@ToMany(referencedJoinProperty = "customerId")
@OrderBy("date ASC")
private List<Order> orders;
}
@Entity
public class Order {
@Id private Long id;
private Date date;
private long customerId;
}
@JoinEntity
用於將某個欄位對映到另外一張表中,示例用法如下(產品和訂單的關係,是一種N:M的關係):
@Entity
public class Product {
@Id private Long id;
@ToMany
@JoinEntity(
entity = JoinProductsWithOrders.class,
sourceProperty = "productId",
targetProperty = "orderId"
)
private List<Order> ordersWithThisProduct;
}
@Entity
public class JoinProductsWithOrders {
@Id private Long id;
private Long productId;
private Long orderId;
}
@Entity
public class Order {
@Id private Long id;
}
樹形關係
舉例說明,如果利用註解來實現一種樹形關係,示例如下:
@Entity
public class TreeNode {
@Id private Long id;
private Long parentId;
@ToOne(joinProperty = "parentId")
private TreeNode parent;
@ToMany(referencedJoinProperty = "parentId")
private List<TreeNode> children;
}
雙向表
@Entity
public class Customer {
@Id private Long id;
@ToMany(referencedJoinProperty = "customerId")
@OrderBy("date ASC")
private List<Order> orders;
}
@Entity
public class Order {
@Id private Long id;
private Date date;
private long customerId;
@ToOne(joinProperty = "customerId")
private Customer customer;
}
總結
這篇部落格就講到這裡,基本涵蓋了greenDao的所有註解,以及用法,掌握了這些註解,就可以建立我們需要的資料表了,後面的關聯註解比較難,要真正掌握需要結合實際的例子來進行學習。