简介
google公司推出的一款类似GreenDao、OrmLite的数据库框架
添加依赖
dependencies {
implementation 'android.arch.persistence.room:runtime:2.2.0'
annotationProcessor 'android.arch.persistence.room:compiler:2.2.0'
//添加测试支持
implementation 'android.arch.persistence.room:testing:2.2.0'
}
具体实现
History.java
- @Entity 创建数据库表
- @PrimaryKey(autoGenerate = true) 主键 自增长
- @ColumnInfo 数据库表中的字段名
- @Ignore 这个字段不会在数据库中创建字段
- 这里是必须要创建set get 方法的
@Entity
public class History {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "type")
private String type;
@ColumnInfo(name = "result")
private String result;
@ColumnInfo(name = "time")
private String time;
//这个用来表示下面的字段不是数据库的字段,只是临时数据
@Ignore
private int i;
public History(String type, String result, String time) {
this.type = type;
this.result = result;
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getResult() {
return result;
}
public void setResult(String result) {