說在前面:
1、使用Room需要添加的依賴:
dependencies { def room_version = "2.2.3" implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor // optional - Kotlin Extensions and Coroutines support for Room implementation "androidx.room:room-ktx:$room_version" // optional - RxJava support for Room implementation "androidx.room:room-rxjava2:$room_version" // optional - Guava support for Room, including Optional and ListenableFuture implementation "androidx.room:room-guava:$room_version" // Test helpers testImplementation "androidx.room:room-testing:$room_version" }
2、數據庫可視化工具安裝及使用說明
3、涉及到的知識:
工程案例:
一、APP描述:對Word實體進行增刪改
二、編寫思路:
1、畫界面:
1)上邊是一個ScrollView(數據多的時候可滑動),ScrollView內有一個TextView
2)下邊是四個按鍵,分別代表,插入、刪除、刪除所有、修改。
2、創建實體(Entity):
package com.me.roombasic; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class Word { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "姓名") private String name; @ColumnInfo(name = "外號") private String other; public Word(String name, String other) { this.name = name; this.other = other; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } }
3、創建dao
package com.me.roombasic; import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; import java.util.List; @Dao public interface WordDao { @Insert void insertWord(Word ...words ); @Update void updateWord(Word... words); @Delete void deleteWord(Word... words); @Query("DELETE FROM WORD") void deleteAllWords(); @Query("SELECT * FROM WORD ORDER BY ID DESC") List<Word> getAllWords(); }
4、創建database
package com.me.roombasic; import androidx.room.Database; import androidx.room.RoomDatabase; @Database(entities = {Word.class},version = 1,exportSchema = false) public abstract class WordDatabase extends RoomDatabase { public abstract WordDao getWordDao(); }
5、暫時在mainActive.java中為TextView、Button綁定數據和監聽:
package com.me.roombasic; import androidx.appcompat.app.AppCompatActivity; import androidx.room.Room; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { WordDao wordDao; WordDatabase wordDatabase; Button buttonInsert,buttonUpdate,buttonDelete,buttonQuery; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); buttonInsert = findViewById(R.id.buttoninster); buttonUpdate = findViewById(R.id.buttonupdate); buttonDelete = findViewById(R.id.buttondelete); buttonQuery = findViewById(R.id.buttonquery); wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"word_database") .allowMainThreadQueries() .build(); wordDao = wordDatabase.getWordDao(); updateView(); buttonInsert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Word word1 = new Word("谷子騰","惡霸"); Word word2 = new Word("張凱鑫","胖鑫"); Word word3 = new Word("王正帥","笑天"); wordDao.insertWord(word1,word2,word3); updateView(); } }); buttonUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Word word2 = new Word("張凱鑫","bujv"); word2.setId(2); wordDao.updateWord(word2); updateView(); } }); buttonDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Word word2 = new Word("張凱鑫","胖鑫"); word2.setId(3); wordDao.deleteWord(word2); updateView(); } });buttonQuery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { wordDao.deleteAllWords(); updateView(); } }); } void updateView(){ List<Word> list = wordDao.getAllWords(); String text = ""; for(int i=0;i<list.size();i++){ text += list.get(i).getId() + ":" + list.get(i).getName() + "=" + list.get(i).getOther() + "\n"; } textView.setText(text); } }
6、效果演示: