概述
创建实体类加入依赖
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
创建实体类
@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.word = word;
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;
}
}
这个类里面的东西我解释一下:
@Entity,表示这个类是数据库中的一张表(实体)
@Embedded,表示在这个表中嵌入另一个类(其实就是嵌了一张表)
@NonNull,主键必须声明非空,而且要在@ColumnInfo上面
@ColumnInfo(name = "id") 表示这个属性是数据库表的某一列,列名是id
@Ignore,让room忽略(我是标在无参构造方法上的,表示不让room用这个构造方法构造对象)
创建DAO类
@Dao // Database aceess object 访问数据库操作的接口
public interface WordDao { // 在这里声明数据库的操作,如增删改查
@Insert
void insertWords(Word... word); // 可以传递三个参数
@Update
void updatWords(Word... words);
@Delete
void deleteWords(Word... words);
@Query("DELETE FROM WORD")
void deleteAllWords();
@Query("SELECT * FROM WORD ORDER BY ID DESC") // 降序排列
List<Word> getAllWords();
创建DataBase
@Database(entities = {Word.class}, version = 1, exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {
public abstract WordDao getWordDao(); // 若有多个 Entity,则应该写多个 Dao
}
使用
public class MainActivity extends AppCompatActivity {
WordDatabase wordDatabase;
WordDao wordDao;
TextView textView;
Button buttonInsert,buttonUpdate,buttonDelete,buttonClear;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView2);
// allowMainThreadQueries() 强制允许在主线程执行,真实开发中不要这么做
wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"word_database").allowMainThreadQueries().build();
wordDao = wordDatabase.getWordDao();
updateView();
buttonInsert = findViewById(R.id.buttonInsert);
buttonUpdate = findViewById(R.id.buttonUpdate);
buttonDelete = findViewById(R.id.buttonDelete);
buttonClear = findViewById(R.id.buttonClear);
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Word word1 = new Word("Hello","你好");
Word word2 = new Word("World","世界");
wordDao.insertWords(word1,word2);
updateView();
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
wordDao.deleteAllWords();
updateView();
}
});
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Word word = new Word("good","真棒");
word.setId(20);
wordDao.updatWords(word);
updateView();
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Word word = new Word("", "");
word.setId(20);
wordDao.deleteWords(word);
updateView();
}
});
}
void updateView(){
List<Word> list = wordDao.getAllWords();
String text = "";
for(int i = 0; i < list.size(); i++){
Word word = list.get(i);
text += word.getId() + ":" + word.getWord() + ":" + word.getChineseMeaning() + "n";
}
textView.setText(text);
}
源码已上传github下载地址
最后
以上就是耍酷过客为你收集整理的安卓Room数据库的使用的全部内容,希望文章能够帮你解决安卓Room数据库的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复