引用:
implementation "androidx.room:room-runtime:2.0.0-beta01"
annotationProcessor "androidx.room:room-compiler:2.0.0-beta01"
一.定义表(@Entity)
定义一张PersonBean表
1.每张表必须有至少一个字段使用@PrimaryKey标识(autoGenerate=true 自增长)
2.表中每一列使用@ColumnInfo标识,并且推荐显示使用name参数定义好表的列名
3.如果你的列名使用private修复,那么必须提供set get方法
4.@Ignore,表示一个属性不加入生成表的字段,只是临时使用
@Entity
public class PersonBean {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "version")
private String version;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "myadress")
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "MyRoomTable{" +
"id=" + id +
", version='" + version + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
二.定义数据库访问对象(@Dao)
全部通过注解定义,Query方法需要指定sql语句
@Dao
public interface PersonInfoDao {
@Insert
void insert(PersonBean personBean);
@Delete
void delete(PersonBean personBean);
@Update
void update(PersonBean personBean);
@Query(value = "select * from PersonBean where name like :name")
PersonBean findByName(String name);
@Query(value = "select * from PersonBean")
List findAll();
}
三.定义数据库
定义一个抽象类继承RoomDatabase,提供dao获取接口
1.entities指定包含的数据表
2.version指定表的版本,和版本升级有关
@Database(entities = {PersonBean.class, version = 1, exportSchema = false)
public abstract class PersonInfoDb extends RoomDatabase {
public abstract PersonInfoDao getPersonDao();
}
四.Build一下后通过apt生成辅助类
五.使用
PersonInfoDb personInfoDb = Room.databaseBuilder(getApplicationContext(), PersonInfoDb.class, "renzhenming").build();
PersonInfoDao personDao = personInfoDb.getPersonDao();
for (int i = 0; i < 100; i++) {
PersonBean p = new PersonBean();
p.setAddress("上海陆家嘴" + i + "号");
p.setName("张三" + i);
p.setVersion("" + i);
personDao.insert(p);
}
List p = personDao.findAll();
System.out.println("renzhenming: " + p);
六.定义一个表作为另一个表的外键
指定teacher中teacher_id为PersonBean id的外键,onDelete = CASCADE,onUpdate = CASCADE标识当PersonBean删除后者更新后,Teacher中对应id的数据也同步进行删除和更新
@Entity
public class PersonBean {
@PrimaryKey(autoGenerate = true)
private int id;
}
Entity(foreignKeys = @ForeignKey(entity = PersonBean.class,parentColumns = "id",
childColumns = "teacher_id",onDelete = CASCADE,onUpdate = CASCADE))
public class Teacher {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "teacher_id")
}
七.嵌套类
Teacher中嵌套了Father,通过prefix = "father"指定Father表中列的前缀,防止重复
@Entity
public class Teacher {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "teacher_id")
private int id;
@ColumnInfo(name = "teacher_name")
private String name;
@ColumnInfo(name = "teacher_age")
private int age;
@Embedded(prefix = "father")
private Father father;
}
@Entity
public class Father {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "age")
private int age;
}
八.数据库升级addMigrations(Migration)
MyDatabase instance = Room.databaseBuilder(context.getApplicationContext(), MyDatabase.class, "renzm_upgrade")
//可以强制在主线程运行数据库操作
.allowMainThreadQueries()
//强制升级
//.fallbackToDestructiveMigration()
.addMigrations(MIGRATION_1_2)
.build();
//进行数据库升级 version 1 to version 2
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
//在这里用sql脚本完成数据变化
database.execSQL("alter table myteacher add column flag integer not null default 1");
}
};
支持Rxjava
@Dao
public interface MyDao {
@Query("SELECT * from user where id = :id LIMIT 1")
public Flowable loadUserById(int id);
}