Android使用Room數(shù)據(jù)庫解決本地持久化的操作
Room概述
Room 是一個持久性庫,屬于 Android Jetpack 的一部分。
Room 是 SQLite 數(shù)據(jù)庫之上的一個抽象層。Room 并不直接使用 SQLite,而是負責簡化數(shù)據(jù)庫設(shè)置和配置以及與數(shù)據(jù)庫交互方面的瑣碎工作。此外,Room 還提供 SQLite 語句的編譯時檢查。
Room主要組件
Room 包含三個主要組件:
- 數(shù)據(jù)實體表示應(yīng)用的數(shù)據(jù)庫中的表。數(shù)據(jù)實體用于更新表中的行所存儲的數(shù)據(jù)以及創(chuàng)建新行供插入。
- 數(shù)據(jù)訪問對象 (DAO) 提供應(yīng)用在數(shù)據(jù)庫中檢索、更新、插入和刪除數(shù)據(jù)所用的方法。
- 數(shù)據(jù)庫類持有數(shù)據(jù)庫,并且是應(yīng)用數(shù)據(jù)庫底層連接的主要訪問點。數(shù)據(jù)庫類為應(yīng)用提供與該數(shù)據(jù)庫關(guān)聯(lián)的 DAO 的實例。
Room各組件之間協(xié)同工作圖例

添加Room庫
在build.gradle文件中添加依賴:
dependencies {
//Room
def room_version = "2.5.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
版本號請查閱官方文檔:
https://developer.android.google.cn/jetpack/androidx/releases/room?hl=zh_cn#groovy
創(chuàng)建Item 實體—Entity
實體類定義了一個表,每個實例都是表的某一行。用映射的方式,實體類告知Room數(shù)據(jù)庫,它應(yīng)如何呈現(xiàn)數(shù)據(jù)庫中的信息并與之交互。

@Entity 注解用于將某個類標記為數(shù)據(jù)庫實體類。對于每個實體類,系統(tǒng)都會創(chuàng)建一個數(shù)據(jù)庫表來保存相關(guān)項。存儲在數(shù)據(jù)庫中的每個實體實例必須有一個主鍵,用于唯一標識數(shù)據(jù)庫表中的每個記錄/條目。主鍵一旦賦值就不能修改,只要它還存在于數(shù)據(jù)庫中,它就表示相應(yīng)的實體對象。
在類之前加上 @Entity 注解來標識數(shù)據(jù)庫的實體,編譯時,系統(tǒng)便會根據(jù)這個類的設(shè)定來創(chuàng)建數(shù)據(jù)庫。
@Entity()
public class Word{
}
如果Entity()的參數(shù)為空,系統(tǒng)在創(chuàng)建數(shù)據(jù)庫時,會把類名作為數(shù)據(jù)庫的表名,如果要自定義表名,可以直接在Entity()里輸入?yún)?shù):
@Entity(tableName = "yourTableName")
接下來,我們應(yīng)該添加:
- 數(shù)據(jù)庫的元素
- 主鍵:每一個實體至少定義一個字段作為主鍵。可以將@PrimaryKey的autoGenerate屬性設(shè)置為true來設(shè)置自動id。如果實體有一個復(fù)合的主鍵,可以使用 @Entity的primaryKeys屬性來指定主鍵。
元素包括:
- id
- word
- chineseMearning
@PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "english_word") private String word; @ColumnInfo(name = "chinese_mearning") private String chineseMeaning;
再加上幾個get/set方法就定義完成了!
創(chuàng)建item 數(shù)據(jù)訪問對象—Dao
新建一個MyDao對象的接口作為數(shù)據(jù)庫操作的接口,在這個接口中,我們定義數(shù)據(jù)庫的增刪改查操作,在此之前,我們需要用 @Dao 來標記這個類為Dao類:
例如:
@Dao //Database access object
public interface WordDao {
@Insertvoid
insertWords(Word... words);
@Updateint
updateWords(Word... words);
@Deletevoid
deleteWords(Word...words);
}
如果我們要使用SQL語句,我們可以這么寫:
@Dao //Database access object
public interface WordDao {
@Insertvoid
insertWords(Word... words);
@Updateint
updateWords(Word... words);
@Deletevoid
deleteWords(Word...words);
@Query("DELETE FROM WORD")
void deleteWords();
@Query("SELECT * FROM WORD ORDER BY ID DESC")
List<Word> getAllWords();
}
創(chuàng)建數(shù)據(jù)庫實例—Database
我們將創(chuàng)建database類,通過繼承改寫room的database,把word.class和wordDao.class聯(lián)系在一起,組成一個完整的數(shù)據(jù)庫。
在同一個位置新建一個WordDatabase的數(shù)據(jù)庫,父類是androidx.room.RoomDatabase,為abstract類型。
我們通過@Database()來標記這個類為database類,在它的參數(shù)中我們可以定義:
- entities:傳入所有Entity的class對象;
- version:數(shù)據(jù)庫版本號。
- exportSchema:設(shè)置是否導(dǎo)出數(shù)據(jù)庫schema,默認為true,需要在build.gradle中設(shè)置:
當數(shù)據(jù)庫發(fā)生改變時,數(shù)據(jù)庫版本號會接著改變,以便更好的進行備份恢復(fù),這里我們用不到,就隨便設(shè)計一個值。
上面的Database()中,我們已經(jīng)把Entity連接到了database中,在Dao類中,我們需要把wordDao類連接到數(shù)據(jù)庫中,我們就需要實例化一個WordDao類:
@Database(entities = {Word.class},version = 1, exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
public abstract WordDao getWordDao();
}
使用Room數(shù)據(jù)庫
獲取AppDatabase實例:
AppDatabase wordDatabase = Room.databaseBuilder(getApplicationContext(),AppDatabase.class, "user.db").build();
使用Room可以參考:
wordDao = wordDatabase.getWordDao(); // new a dao List<Word> list = wordDao.getAllWords(); //get all data from database with sql: select * from word
結(jié)語
以上就是Android使用Room數(shù)據(jù)庫解決本地持久化的操作的詳細內(nèi)容,更多關(guān)于Android Room本地持久化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android實現(xiàn)把文件存放在SDCard的方法
這篇文章主要介紹了Android實現(xiàn)把文件存放在SDCard的方法,涉及Android針對SDCard的讀寫技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
Android中SharedPreferences簡單使用實例
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android編程實現(xiàn)獲取所有傳感器數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實現(xiàn)獲取所有傳感器數(shù)據(jù)的方法,涉及Android針對傳感器Sensor相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

