Introduction to Android's official database framework Room

Reference for this article: Use room to save data locally

What is Room

Room is the official database launched by Android in 2017. Room persistence library provides an abstraction layer based on SQLite, allowing users to fully utilize the powerful functions of SQLite while enjoying a more robust database access mechanism.
Insert picture description here

It can create a cache of application data on the device running the application, and you can view the cached data when there is no internet.

How to use Room

Room contains 3 main components:

  • Database : Contains the database holder and serves as the main access point for the underlying connection of the reserved relational data. Classes annotated with @Database should meet the following conditions
    • Extend the abstract class of RoomDatabase.
      Add a list of entities associated with the database in the comment.
    • Contains abstract methods that have 0 parameters and return classes annotated with @Dao.
      At runtime, you can
      obtain an instance of Database by calling Room.databaseBuilder() or Room. inMemoryDatabaseBuilder().
  • Entity : Represents a table in the database.
  • DAO : Contains methods for accessing the database.
    Insert picture description here

To use Room, you must first declare dependencies:

Add the dependencies of the required artifacts in the build.gradle file of the app or module:
Insert picture description here

dependencies {
implementation "androidx.room:room-runtime:$room_version"//写代码的有一个库
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
//注解处理器

// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
implementation 'androidx.recyclerview:recyclerview:1.1.0'

}

Then create an Entity Java class: (The following uses word as an example)

@Entity
public class Word  {
    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "english_word")
    private String word;
    @ColumnInfo(name = "chinese_meaning")
    private String chinesemeaning;

    public Word(String word,String chinesemeaning) {
        this.chinesemeaning = chinesemeaning;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getChinesemeaning() {
        return chinesemeaning;
    }

    public void setChinesemeaning(String chinesemeaning) {
        this.chinesemeaning = chinesemeaning;
    }
}

Then you can start to prepare Dao (Database access object).
It is an interface, which mainly contains the methods you want to operate on the data:

@Dao   //Database access object
public interface WordDao {
    @Insert //增加
    //void insertWords(Word word);传入一个参数
    void insertWords(Word... words);//传入多个参数

    @Update
    void updateWords(Word... words);

    @Delete
    void daleteWords(Word... words);

    @Query("DELETE FROM WORD")
    void deleteAllWords();

    @Query("SELECT * FROM WORD ORDER BY ID DESC")
    List<Word> getAllWords();
}

Here is only the case of one entity. If there are multiple entities, multiple Dao should be written.

Then we use an abstract class WordDatabase that inherits RoomDatabase to get the methods in Dao

@Database(entities = {Word.class},version=1,exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
    public abstract WordDao getWordDao();

}

In MainActivity we pass

wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"word_database")
        .build();

To get an instance of the database we built.

Guess you like

Origin blog.csdn.net/xiongdan626/article/details/106095472