Jetpack之Room操作数据库

Room数据库框架由google开发,未集成到Android SDK中,作为第三方框架提供。该框架基于SQLIte,通过注解的方式简化对数据库的繁琐操作。

使用步骤如下:(以保存图书信息表案例)

1、引入依赖:

dependencies {  
    ......
    // room库各版本见 https://mvnrepository.com/artifact/androidx.room/room-runtime
    implementation 'androidx.room:room-runtime:2.4.3'
    annotationProcessor 'androidx.room:room-compiler:2.4.3'
}

2、创建表信息对应的实体类

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class BookInfo {
    @PrimaryKey // 该字段是主键,不能重复
    @NonNull // 主键必须是非空字段
    private String name; // 书籍名称
    private String author; // 作者
    private String press; // 出版社
    private double price; // 价格
    //以下get** 和set**方法省略。。。
}
@Entity注解:表示该类是Room专用数据类型,对应表名称也叫做“BookInfo”。(将类的实例与数据库表中的行对应起来。)
@PrimaryKey 和 @NonNull注解:表示name字段一个不为空的主键

3、编写数据表对应的持久化类(操作数据库表)

@Dao
public interface BookDao {

    @Query("SELECT * FROM BookInfo") // 设置查询语句
    List<BookInfo> queryAllBook(); // 加载所有书籍信息

    @Query("SELECT * FROM BookInfo WHERE name = :name") // 设置带条件的查询语句
    BookInfo queryBookByName(String name); // 根据名字加载书籍

    @Insert(onConflict = OnConflictStrategy.REPLACE) // 记录重复时替换原记录
    void insertOneBook(BookInfo book); // 插入一条书籍信息

    @Insert
    void insertBookList(List<BookInfo> bookList); // 插入多条书籍信息

    @Update(onConflict = OnConflictStrategy.REPLACE)// 出现重复记录时替换原记录
    int updateBook(BookInfo book); // 更新书籍信息

    @Delete
    void deleteBook(BookInfo book); // 删除书籍信息

    @Query("DELETE FROM BookInfo WHERE 1=1") // 设置删除语句
    void deleteAllBook(); // 删除所有书籍信息
}

4、编写数据表对应的持久化类

import androidx.room.Database;
import androidx.room.RoomDatabase;

import com.andorid.demo.room.dao.BookDao;
import com.andorid.demo.room.entity.BookInfo;

//entities表示该数据库有哪些表,version表示数据库的版本号
//exportSchema表示是否导出数据库信息的json串,建议设为false,若设为true还需指定json文件的保存路径
@Database(entities = {BookInfo.class},version = 1, exportSchema = false)
public abstract class BookDatabase extends RoomDatabase {
    // 获取该数据库中某张表的持久化对象
    public abstract BookDao bookDao();
}

5、在Application类中声明数据库的唯一实例

       为了避免重复打开数据库造成的内存泄漏问题,每个数据库在App运行过程中理应只有一个实例。在Application该类中声明并获取图书数据库的实例,并将自定义的Application类设为单例模式,保证App在运行之时有且仅有一个应用实例。下面是自定义Application类的代码例子:

public class BaseApplication extends MultiDexApplication {
    private static BaseApplication mApp;//声明一个当前应用的静态实例
    private BookDatabase bookDatabase;//声明一个图书数据库的对象
    /**
     * 获取当前应用的实例
     * @return
     */
    public static BaseApplication getInstance() {
        return mApp;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mApp =this;

        //构建图书数据库的实例
        bookDatabase = Room.databaseBuilder(mApp, BookDatabase.class, "BookInfo")
                .addMigrations()// 允许迁移数据库(发生数据库变更时,Room默认删除原数据库再创建新数据库。如此一来原来的记录会丢失,故而要改为迁移方式以便保存原有记录)
                .allowMainThreadQueries()// 允许在主线程中操作数据库(Room默认不能在主线程中操作数据库)
                .build();
    }

    /**
     * 获取书籍数据库的实例
     * @return
     */
    public BookDatabase getBookDB(){
        return bookDatabase;
    }
}

6、在操作表的地方获取数据库持久化对象

从APP实例中获取唯一的图书持久化对象

BookDao bookDao = MainApplication.getInstance().getBookDB().bookDao ();


完成以上6个编码步骤之后,调用持久化对象的query***、insert***、update***、delete***等方法,就能实现图书信息的增删改查操作了。

主要操作业务逻辑如下:

  • 新增数据核心代码
 // 以下声明一个书籍信息对象,并填写它的各字段值
        BookInfo info = new BookInfo();
        info.setName(name);
        info.setAuthor(author);
        info.setPress(press);
        info.setPrice(Double.parseDouble(price));
        bookDao.insertOneBook(info); // 往数据库插入一条书籍记录

  • 查询数据
List<BookInfo> bookList = bookDao.queryAllBook(); // 获取所有书籍记录
String desc = String.format("数据库查询到%d条记录,详情如下:", bookList.size());
for (int i = 0; i < bookList.size(); i++) {
   BookInfo info = bookList.get(i);
   desc = String.format("%s\n第%d条记录信息如下:", desc, i + 1);
   desc = String.format("%s\n 书名为《%s》", desc, info.getName());
   desc = String.format("%s\n 作者为%s", desc, info.getAuthor());
   desc = String.format("%s\n 出版社为%s", desc, info.getPress());
   desc = String.format("%s\n 价格为%f", desc, info.getPrice());
 }

 if (bookList.size() <= 0) {
    desc = "数据库查询到的记录为空";
 }

  • 删除全部数据
bookDao.deleteAllBook(); // 删除所有书籍记录

完整代码如下:

xml布局文件:

<?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"
    android:orientation="vertical"
    tools:viewBindingIgnore="true"
    android:padding="12dp"
    tools:context="com.andorid.demo.room.RoomWriteReadActivity">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="书 名:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/tv_name"
            android:background="@drawable/editext_selector"
            android:gravity="left|center"
            android:hint="请输入书籍名称"
            android:inputType="text"
            android:maxLength="30"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp" >

        <TextView
            android:id="@+id/tv_author"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="作 者:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_author"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/tv_author"
            android:background="@drawable/editext_selector"
            android:gravity="left|center"
            android:hint="请输入作者姓名"
            android:inputType="text"
            android:maxLength="10"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp" >

        <TextView
            android:id="@+id/tv_press"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="出版社:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_press"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/tv_press"
            android:background="@drawable/editext_selector"
            android:gravity="left|center"
            android:hint="请输入出版社名称"
            android:inputType="text"
            android:maxLength="20"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp" >

        <TextView
            android:id="@+id/tv_price"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="价 格:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/et_price"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/tv_price"
            android:background="@drawable/editext_selector"
            android:gravity="left|center"
            android:hint="请输入书籍价格"
            android:inputType="numberDecimal"
            android:maxLength="6"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </RelativeLayout>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存到数据库"
        android:textColor="@color/white"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除所有记录"
        android:textColor="@color/white"
        android:textSize="17sp" />

    <Button
        android:id="@+id/btn_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取数据"
        android:textColor="@color/white"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/tv_room"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

Activity对应的处理页面

/**
 * Room数据的使用
 */
public class RoomWriteReadActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name; // 声明一个编辑框对象
    private EditText et_author; // 声明一个编辑框对象
    private EditText et_press; // 声明一个编辑框对象
    private EditText et_price; // 声明一个编辑框对象
    private Button btn_save; //保存数据
    private Button btn_read; //读取数据
    private Button btn_delete; //删除数据
    private TextView tv_room; // 声明一个文本信息展示

    private BookDao bookDao; // 声明一个书籍的持久化对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_write_read);
        et_name = findViewById(R.id.et_name);
        et_author = findViewById(R.id.et_author);
        et_press = findViewById(R.id.et_press);
        et_price = findViewById(R.id.et_price);
        tv_room = findViewById(R.id.tv_room);

        btn_save = findViewById(R.id.btn_save);
        btn_read = findViewById(R.id.btn_read);
        btn_delete = findViewById(R.id.btn_delete);

        // 从App实例中获取唯一的书籍持久化对象
        bookDao = BaseApplication.getInstance().getBookDB().bookDao();

        btn_save.setOnClickListener(this);
        btn_read.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save://保存数据
                writeRoom();
                break;
            case R.id.btn_read://读取数据
                readRoom();
                break;
            case R.id.btn_delete://删除数据
                bookDao.deleteAllBook(); // 删除所有书籍记录
                Toast.makeText(this, "已删除所有记录", Toast.LENGTH_SHORT).show();
                readRoom(); // 读取数据库中的所有书籍记录
                break;
            default:
        }
    }

    /**
     * 写入Room数据库
     */
    private void writeRoom() {
        String name = et_name.getText().toString();
        String author = et_author.getText().toString();
        String press = et_press.getText().toString();
        String price = et_price.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先填写书籍名称", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(author)) {
            Toast.makeText(this, "请先填写作者姓名", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(press)) {
            Toast.makeText(this, "请先填写出版社名称", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(price)) {
            Toast.makeText(this, "请先填写价格", Toast.LENGTH_SHORT).show();
            return;
        }
        // 以下声明一个书籍信息对象,并填写它的各字段值
        BookInfo info = new BookInfo();
        info.setName(name);
        info.setAuthor(author);
        info.setPress(press);
        info.setPrice(Double.parseDouble(price));
        bookDao.insertOneBook(info); // 往数据库插入一条书籍记录
        Toast.makeText(this, "数据已写入Room数据库", Toast.LENGTH_SHORT).show();
    }

    /**
     * 读取数据库中的所有书籍记录
     */
    private void readRoom() {
        List<BookInfo> bookList = bookDao.queryAllBook(); // 获取所有书籍记录
        String desc = String.format("数据库查询到%d条记录,详情如下:", bookList.size());
        for (int i = 0; i < bookList.size(); i++) {
            BookInfo info = bookList.get(i);
            desc = String.format("%s\n第%d条记录信息如下:", desc, i + 1);
            desc = String.format("%s\n 书名为《%s》", desc, info.getName());
            desc = String.format("%s\n 作者为%s", desc, info.getAuthor());
            desc = String.format("%s\n 出版社为%s", desc, info.getPress());
            desc = String.format("%s\n 价格为%f", desc, info.getPrice());
        }
        if (bookList.size() <= 0) {
            desc = "数据库查询到的记录为空";
        }
        tv_room.setText(desc);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值