Android Jetpack之Room的简单应用
1. jetpack简介
Jetpack 是一个丰富的组件库,它的组件库按类别分为 4 类,分别是架构(Architecture)、界面(UI)、行为(behavior)和基础(foundation)。每个组件都可以单独使用,也可以配合在一起使用。每个组件都给用户提供了一个标准,能够帮助开发者遵循最佳做法,减少样板代码并编写可在各种 Android 版本和设备中一致运行的代码,让开发者能够集中精力编写重要的业务代码。
2. Room简单介绍
Room是Jetpack中架构(Architecture)中其中的一个组件。在Android开发中使用的数据库是sqlLite。但是Sqlite代码写起来繁琐且容易出错,所以开源社区里逐渐出现了各种ORM(Object Relational Mapping)库。这些开源ORM库都是为了方便Sqlite的使用,包括数据库的创建,升级,增删改查等。常见的ORM有ORMLite,GreenDAO等。Google也意识到了推出自家ORM的必要性,于是有了Room。
一: Room包含了三个组件:
- Entity:实体类,对应的是数据库的一张表结构。需要使用注解 @Entity 标记。
- Dao:包含访问一系列访问数据库的方法。需要使用注解 @Dao 标记。
- Database:数据库持有者,作为与应用持久化相关数据的底层连接的主要接入点。需要使用注解 @Database 标记。
二:Room中常用注解说明:
- @Entity:这是一个Model类,对应于数据库中的一张表。Entity类是Sqlite表结构在Java类的映射。
- @Dao:(Data Access Objects)数据访问对象,顾名思义,我们可以通过它来访问数据。一个Entity代表着一张表,而每张表都需要一个Dao对象,以方便对这张表进行各种操作(增删改查)
- @Database:数据库对象,一般写成单例模式
三:Dao中常用的注解:
- @Query查询
- @Insert插入
- @Update更新
- @Delete删除
四:Entity中的常用注解:
- 属性:tableName:设置表名字。默认是类的名字。
- @PrimaryKey标签用于指定该字段作为表的主键。
- @ColumnInfo标签可用于设置该字段存储在数据库表中的名字并指定字段的类型。
- @Ignore标签用来告诉系统忽略该字段或者方法
3. Room的简单应用
- Room在项目中的引入,在app的build.gradle中加入room的相关依赖:
implementation "androidx.room:room-runtime:2.2.5"
annotationProcessor "androidx.room:room-compiler:2.2.5"
- 创建学生实体类
@Entity(tableName = "student")
public class Student {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER)
public int id;
@ColumnInfo(name = "name", typeAffinity = ColumnInfo.TEXT)
public String name;
@ColumnInfo(name = "age", typeAffinity = ColumnInfo.TEXT)
public String age;
public Student(int id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
@Ignore
public Student(String name, String age) {
this.name = name;
this.age = age;
}
}
- 创建DAO接口
@Dao
public interface StudentDao {
//增加
@Insert
void insertStudent(Student student);
//删除
@Delete
void deleteStudent(Student student);
//更新
@Update
void updateStudent(Student student);
//查询
@Query("SELECT * FROM student")
List<Student> getStudentList();
@Query("SELECT * FROM student WHERE id = :id")
Student getStudentById(int id);
}
- 创建数据库,MyDatabase继承RoomDatabase类
@Database(entities = {
Student.class}, version = 1)
public abstract class MyDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "student";
private static MyDatabase databaseInstance;
public static synchronized MyDatabase getInstance(Context context) {
if (databaseInstance == null) {
databaseInstance = Room
.databaseBuilder(context.getApplicationContext(), MyDatabase.class, DATABASE_NAME)
.build();
}
return databaseInstance;
}
public abstract StudentDao studentDao();
}
- 初始化数据库
private void initDataBase() {
myDatabase = MyDatabase.getInstance(MainActivity.this);
new QueryStudentTask().execute();
}
- 现在先简单设计一下页面布局,页面布局主要用到ListView
(1)主页面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_add"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="16dp"
android:background="@color/colorAccent"
android:onClick="addStudent"
android:text="添加学生"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/lvStudent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/btn_add"
android:layout_marginTop="40dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
(2)item_student.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp">
<TextView
android:id="@+id/tvId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
(3)增加学生和更新学生弹出框 dialog_layout_student.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:layout_weight="1"/>
<EditText
android:id="@+id/etAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年龄"
android:layout_weight="1"/>
</LinearLayout>
效果截图:
7. 我们主要实现的就是对学生的增删改查
这里我们我们用到AsyncTask,AsyncTask是安卓多线程的使用,AsyncTask抽象类如下:
public abstract class AsyncTask<Params, Progress, Result> {
...
}
// 类中参数为3种泛型类型
// 整体作用:控制AsyncTask子类执行线程任务时各个阶段的返回类型
// 具体说明:
// a. Params:开始异步任务执行时传入的参数类型,对应excute()中传递的参数
// b. Progress:异步任务执行过程中,返回下载进度值的类型
// c. Result:异步任务执行完成后,返回的结果类型,与doInBackground()的返回值类型保持一致
// 注:
// a. 使用时并不是所有类型都被使用
// b. 若无被使用,可用java.lang.Void类型代替
// c. 若有不同业务,需额外再写1个AsyncTask的子类
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
其中onPostExecute这个方法是执行doInBackground()之后进行的
(1)增加学生
/**
* 插入学生信息
*/
private class InsertStudentTask extends AsyncTask<Void, Void, Void> {
String name;
String age;
public InsertStudentTask(final String name, final String age) {
this.name = name;
this.age = age;
}
@Override
protected Void doInBackground(Void... arg0) {
myDatabase.studentDao().insertStudent(new Student(name, age));
studentList.clear();
studentList.addAll(myDatabase.studentDao().getStudentList());
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
}
这里主要的是
myDatabase.studentDao().insertStudent(new Student(name, age));
这是数据库对象通过studentDao接口中的insertStudent()方法进行插入数据。
(2)删除学生
/**
* 删除学生信息
*/
private class DeleteStudentTask extends AsyncTask<Void, Void, Void> {
Student student;
public DeleteStudentTask(Student student) {
this.student = student;
}
@Override
protected Void doInBackground(Void... arg0) {
myDatabase.studentDao().deleteStudent(student);
studentList.clear();
studentList.addAll(myDatabase.studentDao().getStudentList());
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
}
这是数据库对象通过studentDao接口中的deleteStudent()方法进行删除数据。
(3)更新学生
/**
* 更新学生信息
*/
private class UpdateStudentTask extends AsyncTask<Void, Void, Void> {
int id;
String name;
String age;
public UpdateStudentTask(final int id, final String name, final String age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
protected Void doInBackground(Void... arg0) {
myDatabase.studentDao().updateStudent(new Student(id, name, age));
studentList.clear();
studentList.addAll(myDatabase.studentDao().getStudentList());
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
studentAdapter.notifyDataSetChanged();
}
}
这是数据库对象通过studentDao接口中的updataStudent()方法进行更新数据。
项目执行之后的效果:
添加后:
更新学生信息:
删除效果:
作者:吴自煊