Android Room的学习之基本使用
Android Room的学习之关联表
Android Room的学习之数据库升级
简介
Room是一个对象关系映射(ORM)库。
Room抽象了SQLite的使用,可以在充分利用SQLite的同时访问流畅的数据库。
Room的组成
Database
Entity
DAO
Database:app持久化状态,并作为与应用持久关联数据的底层连接的主要访问点。
使用Database注解的类需要满足的条件:
Entity:用来注解实体类,并利用该类的所有字段作为表的列来创建表。
Dao:提供访问数据库的方法。
1.添加依赖
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
// optional - RxJava support for Room
api "android.arch.persistence.room:rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "android.arch.persistence.room:guava:$room_version"
// Test helpers
testImplementation "android.arch.persistence.room:testing:$room_version"
Android x 使用
def room_version = "2.2.1"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
2.例子
创建实体类 - Student.java
@Entity
public class Student {
@PrimaryKey(autoGenerate = true)
public int sId;
@ColumnInfo(name = "name")
public String name;
@ColumnInfo(name = "age")
public int age;
public int getId() {
return sId;
}
public void setId(int sId) {
this.sId = sId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
然后是StudentDao.java
@Dao
public interface StudentDao {
@Query("SELECT * FROM Student")
List<Student> getAll();
@Query("SELECT * FROM Student WHERE sId IN (:userIds)")
List<Student> loadAllByIds(int[] userIds);
@Query("SELECT * FROM Student WHERE age=:age")
List<Student> getStudentByAge(int age);
@Query("SELECT * FROM Student WHERE age=:age LIMIT :max")
List<Student> getStudentByAge(int max, int... age);
@Query("SELECT * FROM Student WHERE sId =:sId")
Student getStudent(int sId);
@Insert
void insertAll(Student... users);
@Update
void updateStudent(Student... users);
@Delete
void delete(Student user);
}
最后是 AppDatabase.java
@Database(entities = {Student.class}, version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static final String DB_NAME = "StudentDatabase.db";
private static volatile AppDatabase instance;
static synchronized AppDatabase getInstance(Context context) {
if (instance == null) {
instance = create(context);
}
return instance;
}
private static AppDatabase create(final Context context) {
return Room.databaseBuilder(
context,
AppDatabase.class,
DB_NAME).build();
}
public abstract StudentDao studentDao();
}
重点 重点 重点: 所有数据库的操作都需要放在异步线程 最好与RxJava合用。
添加:
StudentDao mStudentDao = AppDatabase.getInstance(this).studentDao();
Student mStudent = new Student();
mStudent.setId(id);
mStudent.setAge(age);
mStudent.setName(name);
mStudentDao.insertAll(mStudent);
更新:
StudentDao mStudentDao = AppDatabase.getInstance(this).studentDao();
Student mStudent = new Student();
mStudent.setId(1);
mStudent.setAge(562);
mStudent.setName("张三222");
mStudentDao.updateStudent(mStudent);
删除:
StudentDao mStudentDao = AppDatabase.getInstance(this).studentDao();
Student mStudent = new Student();
mStudent.setId(1);
mStudent.setAge(56);
mStudent.setName("张三");
mStudentDao.delete(mStudent);
查找:
StudentDao mStudentDao = AppDatabase.getInstance(this).studentDao();
List<Student> list = mStudentDao.getAll()//查找全部
Student mStudent = mStudentDao.getStudent(7)//根据年龄查找 返回对象
List<Student> list = mStudentDao.getStudentByAge(60)//根据年龄查找 list
List<Student> list = mStudentDao.getStudentByAge(60, 70)//根据年龄区间 查找 list
至此,简单的Room 数据库就完成了