1.Room框架
Room是Android Jetpack组件库中的一部分,它是一个SQLite数据库的抽象层,提供了更简单的API和更好的性能,适合于中大型应用程序。
2.Room的使用
使用Room和之前使用SQLite搭建数据库的过程类似,但是更加简单了。
1)构建实体类
举个例子,我们要将书本信息存储到数据库中,那么可以先创建一个书本信息的新类,用于封装书本信息
在该实体类中,用注解表明实体:@Entity, 和对应表中的主键:@PrimaryKey
package com.example.study06.entity;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class BookInfo {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private String isbn;
private String publisher;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getIsbn() {
return isbn;
}
public String getPublisher() {
return publisher;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
@Override
public String toString() {
return "BookInfo{" +
"id=" + id +
", name='" + name + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
'}';
}
}
2)创建Dao接口:data access operation
这个接口不需要实现具体的增删改查操作,只需要用注解标记好对应的操作。
package com.example.study06.Dao;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import com.example.study06.entity.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
void 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);
}
Room会自动生成一个实现该接口的类,这个类可以完成增删查改的操作:
3)创建数据库的抽象类
也是类似的,用注解写清楚该数据库涉及的实体类。
package com.example.study06.database;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import com.example.study06.Dao.BookDao;
import com.example.study06.entity.BookInfo;
@Database(entities = {BookInfo.class}, version = 1, exportSchema = true)
public abstract class BookDataBase extends RoomDatabase {
public abstract BookDao bookDao();
}
4)在Myapplication的OnCreate函数中创建数据库
这样数据库的实例就是全局变量
package com.example.study06;
import android.app.Application;
import androidx.room.Room;
import com.example.study06.database.BookDataBase;
import java.util.HashMap;
public class MyApplication extends Application {
public static MyApplication myapp;
public HashMap<String, String> infoMap = new HashMap<>();
public BookDataBase bookDataBase;
public static MyApplication getInstance(){
return myapp;
}
@Override
public void onCreate() {
super.onCreate();
myapp = this;
bookDataBase = Room.databaseBuilder(this,BookDataBase.class,"book")
.addMigrations()
.allowMainThreadQueries()
.build();
}
public BookDataBase getBookDataBase() {
return bookDataBase;
}
}
5)使用数据库
对BookInfo表进行增删查改
package com.example.study06;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.study06.Dao.BookDao;
import com.example.study06.entity.BookInfo;
import java.util.List;
public class RoomWriteActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_name;
private EditText et_isbn;
private EditText et_publisher;
private BookDao bookDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_write);
et_name = findViewById(R.id.et_name);
et_isbn = findViewById(R.id.et_isbn);
et_publisher = findViewById(R.id.et_publisher);
findViewById(R.id.bt_save).setOnClickListener(this);
findViewById(R.id.bt_delete).setOnClickListener(this);
findViewById(R.id.bt_update).setOnClickListener(this);
findViewById(R.id.bt_query).setOnClickListener(this);
bookDao = MyApplication.getInstance().getBookDataBase().bookDao();
}
@Override
public void onClick(View view) {
String name = et_name.getText().toString();
String isbn = et_isbn.getText().toString();
String publisher = et_publisher.getText().toString();
switch (view.getId()){
case R.id.bt_save:
BookInfo b1 = new BookInfo();
b1.setIsbn(isbn);
b1.setName(name);
b1.setPublisher(publisher);
bookDao.insert(b1);
break;
case R.id.bt_query:
List<BookInfo> books = bookDao.queryAll();
for(BookInfo b : books){
Log.d("test ", b.toString());
}
break;
case R.id.bt_delete:
BookInfo b2 = bookDao.queryByName(name);
bookDao.delete(b2);
break;
case R.id.bt_update:
BookInfo b3 = new BookInfo();
BookInfo b4 = bookDao.queryByName(name);
b3.setId(b4.getId());
b3.setIsbn(isbn);
b3.setName(name);
b3.setPublisher(publisher);
bookDao.update(b3);
break;
}
}
}