引言
上篇博客中介绍了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的所有注解,以及用法,掌握了这些注解,就可以创建我们需要的数据表了,后面的关联注解比较难,要真正掌握需要结合实际的例子来进行学习。
参考内容:grenDao官方文档