Android(安卓)官方数据库Room --- 配置
源文档链接
工程配置
implementation "android.arch.persistence.room:runtime:1.0.0"annotationProcessor "android.arch.persistence.room:compiler:1.0.0"// RxJava support for Room (use 1.1.0-alpha3 for latest alpha)implementation "android.arch.persistence.room:rxjava2:1.0.0"
表定义
定义Entity
默认情况,Room会为使用了@Entity
注解的类中的每个属性建列,可以使用@Ignore
忽略某个属性
@Entityclass User { @PrimaryKey public int id; public String firstName; public String lastName; @Ignore Bitmap picture;}
对于属性,Room必须能够使用,可以直接设置为public
,后者提供setter/getter
方法,使用set/get时,是使用的JavaBean中的约定
注意
实体类可以有无参构造器,也可以使用有参构造器(此时,对应的参数和属性的名字和类型要一一对应)
使用PrimariKey
每个Entity必须至少定义一个PrimaryKey
的属性;想要使用Room自动定义ID时,可以使用PrimaryKey
的autoGenerate
属性;如果使用复合主键,可以使用Entity
的primaryKey
属性
如:
@Entity(primaryKeys = {"firstName", "lastName"})class User { public String firstName; public String lastName; @Ignore Bitmap picture;}
默认情况,Room使用类名作为表名,可以使用Entity
的tableName
进行定义
@Entity(tableName = "users")class User { ...}
注意
SQLite中的表名不区分大小写
和tableName
类似,Room使用属性的名字作为表中的列名,可以使用@ColumnInfo
的name
进行定义
@Entity(tableName = "users")class User { @PrimaryKey public int id; @ColumnInfo(name = "first_name") public String firstName; @ColumnInfo(name = "last_name") public String lastName; @Ignore Bitmap picture;}
注释索引和唯一性
如果需要添加索引加快查询速度,可以使用
Entity
中的indices
属性,指定索引(复合索引)@Entity(indices = {@Index("name"), @Index(value = {"last_name", "address"})})class User { @PrimaryKey public int id; public String firstName; public String address; @ColumnInfo(name = "last_name") public String lastName; @Ignore Bitmap picture;}
为单个属性(多个属性)设置唯一性
@Entity(indices = {@Index(value = {"first_name", "last_name"}, unique = true)})class User { @PrimaryKey public int id; @ColumnInfo(name = "first_name") public String firstName; @ColumnInfo(name = "last_name") public String lastName; @Ignore Bitmap picture;}
定义对象关系
SQLite是关系型数据库,可以指定对象之间的关系。Room禁止实体对象之间的互相引用。
虽然不可以直接引用,Room允许使用外键
User和Book通过ForeignKey
建立关系
@Entity(foreignKeys = @ForeignKey(entity = User.class, parentColumns = "id", childColumns = "user_id"))class Book { @PrimaryKey public int bookId; public String title; @ColumnInfo(name = "user_id") public int userId;}
针对上述代码,可以使用ForeginKey
中的onDelete = CASCADE
来告诉SQLite,当删除一个User时,那么这个User对应的所有Book也会同时删除
注意
SQLite处理@Insert(onConflict = Replace)
会有一系列REMOVE
和REPLACE
操作,而不是一个UPDATE
操作。这个方法中的REPLACE
操作中的冲突值可能影响外键,SQLite documentation
创建嵌套对象
当对象内部使用引用对象作为属性时,可以使用@Embedded
来对该对象进行分解,再一次存入到对应的列中
class Address { public String street; public String state; public String city; @ColumnInfo(name = "post_code") public int postCode;}@Entityclass User { @PrimaryKey public int id; public String firstName; @Embedded public Address address;}
上述的代码建成的user表中,会有6个字段 – id,firstName,street,state,city,post_code
注意
Embedded
属性可以包括其他的嵌入属性
如果实体中相同类型的属性具有多个,可以使用prefix
属性来标明唯一性。Room会在对应的属性前加入prefix
的值
更多相关文章
- Android自动化工具Monkeyrunner使用(六) —— 根据ID查找对象
- Android(安卓)三角标签(自定义Textview控件)
- andorid 自定义seekbar
- Android(安卓)使用netty框架实现socket通信
- Android(安卓)自定义显示图片Dialog
- android 时间戳与日期格式的互相转换 以及 自定义选择器
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用