Android Room的简单使用

597 阅读3分钟

Room简单使用

Room是一个持久性数据库,Room持久层库在SQLite上提供了一个抽象层,以便充分利用SQLite的强大功能同时,能够流畅地访问数据库,具有以下优势:

  • 针对SQL查询的编译时验证
  • 可最大限度减少重复和容易出错的样板代码的方便注解
  • 简化了数据库迁移路径

Room的三个部分

  • 数据库:用于保存数据库,并作为应用持久性数据底层连接的主要访问
  • 数据实体:用于表示数据库中的表
  • 数据访问对象(DAO):用于增删改查的数据方法

添加依赖

dependencies {
    def room_version = "2.4.2"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"

    // optional - RxJava3 support for Room
    implementation "androidx.room:room-rxjava3:$room_version"

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // optional - Paging 3 Integration
    implementation "androidx.room:room-paging:2.5.0-alpha01"
}

创建数据实体-即数据表

  • @Entity(tableName = "表名"):设置表名
  • @PrimaryKey(autoGenerate = false):当设置为true,让sqlite生成唯一的ID(即添加一个主键)
  • @Entity(primaryKeys = {"username", "age"}):添加多个主键
  • @Ignore:忽略某个字段
//指定表名,默认情况下表名为类的名字,可使用tableName指定表名
@Entity
public class User {
    //指定id为主键,并自增
    @PrimaryKey(autoGenerate = true)
    public int id;

    //表示这个字段为一列,默认情况下类名为列名,也可以使用name属性指定列名
    @ColumnInfo(name = "username")
    public String username;

    @ColumnInfo(name = "age")
    public int age;
    
    //忽略这个字段
    @Ignore
    Bitmap image;
}

注意:SQLite的表名和列名不区分大小写

创建数据访问对象(DAO)-即访问数据库的方法

  • @DAO:指定数据访问对象
  • @Insert:插入数据
  • @Delete:删除数据
  • @Update:更新数据
  • @Query:查询数据
//指定数据访问对象DAO
@Dao
public interface UserDao {

    //插入数据
    @Insert
    void insert(User user);

    //删除数据
    @Delete
    void delete(User user);

    //更新数据
    @Update
    void update(User user);
    
    //获取所有用户数据
    @Query("select * from user")
    List<User> getUserAll();

    //查找指定的数据
    @Query("select * from user where username = :username and age = :age")
    List<User> getUserInfo(String username, int age);
}

创建数据库类

//指定数据实体类和版本号
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    //提供返回一个数据访问对象的方法
    public abstract UserDao userDao();
}

创建并使用

AppDatabase database = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "test.db").build();
UserDao userDao = database.userDao();
List<User> userDataAll = userDao.getUserAll();
for (User user : userDataAll) {
    Log.e("TAG", "id: " + user.id);
    Log.e("TAG", "username: " + user.username);
    Log.e("TAG", "age: " + user.age);
}

案例实现

布局实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/insert_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入插入的名字"/>

        <EditText
            android:id="@+id/insert_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入插入的年龄"/>

        <Button
            android:id="@+id/insert_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="插入数据"/>

    </LinearLayout>

    <Button
        android:id="@+id/select_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询数据"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/delete_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入删除的id"/>

        <Button
            android:id="@+id/delete_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="删除数据"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/update_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入修改的id"/>

        <EditText
            android:id="@+id/update_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入修改的名字"/>

        <EditText
            android:id="@+id/update_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入修改的年龄"/>

        <Button
            android:id="@+id/update_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="删除数据"/>

    </LinearLayout>



</LinearLayout>

实体类实现

@Entity
public class User {

    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo(name = "name")
    public String name;

    @ColumnInfo(name = "age")
    public int age;
}

数据访问对象实现

@Database(entities = {User.class}, version = 1)
public abstract class MyDataBase extends RoomDatabase {

    public abstract UserDao userDao();
}

数据库实现

//指定数据实体类和版本号
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    //提供返回一个数据访问对象的方法
    public abstract UserDao userDao();
}

功能代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText insertName, insertAge;
    private Button insertVal;
    private Button selectVal;
    private EditText deleteId;
    private Button deleteVal;
    private EditText updateId, updateName, updateAge;
    private Button updateVal;

    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView(){
        insertName = findViewById(R.id.insert_name);
        insertAge = findViewById(R.id.insert_age);
        insertVal = findViewById(R.id.insert_value);
        selectVal = findViewById(R.id.select_value);
        deleteId = findViewById(R.id.delete_id);
        deleteVal = findViewById(R.id.delete_value);
        updateId = findViewById(R.id.update_id);
        updateName = findViewById(R.id.update_name);
        updateAge = findViewById(R.id.update_age);
        updateVal = findViewById(R.id.update_value);

        insertVal.setOnClickListener(this);
        selectVal.setOnClickListener(this);
        deleteVal.setOnClickListener(this);
        updateVal.setOnClickListener(this);

        MyDataBase myDataBase = Room.databaseBuilder(getApplicationContext(), MyDataBase.class, "MyDataBase.db").build();
        userDao = myDataBase.userDao();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.insert_value:
                setInsertVal();
                break;
            case R.id.select_value:
                getSelectAll();
                break;
            case R.id.delete_value:
                deletedVal();
                break;
            case R.id.update_value:
                updateVal();
                break;
        }
    }

    private void setInsertVal(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String name = insertName.getText().toString();
                int age = Integer.parseInt(insertAge.getText().toString());

                User user = new User();
                user.name = name;
                user.age = age;

                userDao.insertVal(user);
            }
        }).start();
    }

    private void getSelectAll(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<User> userList = userDao.selectAll();
                for (User user: userList) {
                    Log.e("LCXUANTEST", "ID:" + user.id + ", name:" + user.name + ", age:" + user.age);
                }
            }
        }).start();
    }

    private void deletedVal(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int id = Integer.parseInt(deleteId.getText().toString());

                User user = new User();
                user.id = id;

                userDao.deleteVal(user);
            }
        }).start();
    }

    private void updateVal(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int id = Integer.parseInt(updateId.getText().toString());
                String name = updateName.getText().toString();
                int age = Integer.parseInt(updateAge.getText().toString());

                User user = new User();
                user.id = id;
                user.name = name;
                user.age = age;

                userDao.updateVal(user);
            }
        }).start();
    }
}