赞
踩
Room框架同样基于SQL,但是通过注解技术极大简化了数据库操作,减少了原来的编码工作
在使用Room之前,要先修改模块的build.gradle文件,往
dependencies节点添加下面两行配置,表示导入指定版本的Room库。
implementation 'androidx.room:room-runtime:2.2.5' annotationProcessor 'android.room:room-compiler:2.2.5'
Room框架编码步骤有以下五步
1.构建一个实体类保存书籍信息,注意打上注解Entity,对于id打上@PrimaryKey(autoGenerate = true)表示自动增长
- package com.example.chapter06.enity;
-
- import androidx.room.Entity;
- import androidx.room.PrimaryKey;
-
- @Entity
- public class BookInfo {
-
- @PrimaryKey(autoGenerate = true)
- private int id;
- private String name; // 书籍名称
- private String author; // 作者
- private String press; // 出版社
- private double price; // 价格
-
- public void setId(int id) {
- this.id = id;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- public void setPress(String press) {
- this.press = press;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public int getId() {
- return id;
- }
-
- public String getName() {
- return name;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public String getPress() {
- return press;
- }
-
- public double getPrice() {
- return price;
- }
-
- @Override
- public String toString() {
- return "BookInfo{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", press='" + press + '\'' +
- ", price=" + price +
- '}';
- }
- }
2.编写书籍信息表对应的持久化类,该类添加@Dao注解(Database access operator)
- package com.example.chapter06.dao;
-
- import androidx.room.Dao;
- import androidx.room.Delete;
- import androidx.room.Insert;
- import androidx.room.Query;
- import androidx.room.Update;
-
- import com.example.chapter06.enity.BookInfo;
-
- import java.util.List;
-
- @Dao
- public interface BookDao {
- @Insert
- void insert(BookInfo... book);
-
- @Delete
- void delete(BookInfo... book);
-
- @Query("DELETE FROM BookInfo")
- void deleteAll();
-
- @Update
- int update(BookInfo... book);
-
- @Query("SELECT * FROM BookInfo")
- List<BookInfo> queryAll();
-
- @Query("SELECT * FROM BookInfo WHERE name = :name ORDER BY id DESC limit 1")
- BookInfo queryByName(String name);
- }
3.提供一个database
- package com.example.chapter06.database;
-
- import androidx.room.Database;
- import androidx.room.RoomDatabase;
-
- import com.example.chapter06.dao.BookDao;
- import com.example.chapter06.enity.BookInfo;
-
-
- //entities表示该数据库有哪些表,version表示数据库的版本号
- //exportSchema表示是否导出数据库信息的json串,建议设为false,若设为true还需指定json文件的保存路径
- @Database(entities = {BookInfo.class}, version = 1, exportSchema = true)
- public abstract class BookDataBase extends RoomDatabase {
- //
- public abstract BookDao bookDao();
-
- }
-
- //如果exportSchema 被设置为true了,需要在build.gradle中设置
- // javaCompileOptions {
- // annotationProcessorOptions {
- // arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]//指定数据库schema导出的位置
- // }
- // }
4.在自定义的Application类中声明书籍数据库的唯一实例
//构建数据库的实例
- bookDataBase = Room.databaseBuilder(this, BookDataBase.class, "book")
- // 允许迁移数据库(发生数据库变更时,Room默认删除原数据库再创建新数据库。如此一来原来的记录会丢失,故而要改为迁移方式以便保存原有记录)
- .addMigrations()
- //允许在主线程中操作数据库,(ROOM 默认不能在主线程中操作数据库)
- .allowMainThreadQueries().build();
5.获取实例
bookDao = MyApplication.getInstance().getBookDataBase().bookDao();
增删改查
- public void onClick(View view) {
- String name = et_name.getText().toString();
- String author = et_author.getText().toString();
- String press = et_press.getText().toString();
- String price = et_price.getText().toString();
-
- switch (view.getId()) {
- case R.id.btn_save:
- //以下声明一个书籍信息对象,并填写它的各个字段值
- BookInfo b1 = new BookInfo();
- b1.setName(name);
- b1.setAuthor(author);
- b1.setPress(press);
- b1.setPrice(Double.parseDouble(price));
- bookDao.insert(b1);
- ToastUtil.show(this, "保存成功");
- break;
- case R.id.btn_query:
- List<BookInfo> list = bookDao.queryAll();
- for (BookInfo b : list) {
- Log.d("xr", b.toString());
- }
- break;
- case R.id.btn_delete:
- BookInfo b2 = new BookInfo();
- b2.setId(1);
- bookDao.delete(b2);
- break;
- case R.id.btn_update:
- BookInfo b3 = new BookInfo();
- BookInfo b4 = bookDao.queryByName(name);
- b3.setId(b4.getId());
- b3.setName(name);
- b3.setAuthor(author);
- b3.setPress(press);
- b3.setPrice(Double.parseDouble(price));
- bookDao.update(b3);
- break;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。