【androidラーニング】Jetpack:Roomデータベース
6568 ワード
概要
Google社が発表したGreenDao、OrmLiteのようなデータベースフレームワーク
依存の追加
dependencies {
implementation 'android.arch.persistence.room:runtime:2.2.0'
annotationProcessor 'android.arch.persistence.room:compiler:2.2.0'
//
implementation 'android.arch.persistence.room:testing:2.2.0'
}
具体的な実装
History.java
@Entity
public class History {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "type")
private String type;
@ColumnInfo(name = "result")
private String result;
@ColumnInfo(name = "time")
private String time;
// ,
@Ignore
private int i;
public History(String type, String result, String time) {
this.type = type;
this.result = result;
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "History{" +
"id=" + id +
", type='" + type + '\'' +
", result='" + result + '\'' +
", time='" + time + '\'' +
'}';
}
}
HistoryDao.java
@Dao
public interface HistoryDao {
@Insert
void insert(History... histories);
@Delete
void delete(History history);
@Update
void update(History history);
@Query("select * from History")
List getAll();
@Query("select * from History where id in (:historyIds)")
List getAllId(int[] historyIds);
//
@Query("select * from History where result like '%' || :result || '%'")
List getByResult(String result);
}
AppDatabase .java
/**
*
*/
@Database(entities = {History.class},version = 1)
public abstract class AppDatabase extends RoomDatabase {
//
private static final String DB_NAME = "UserDatabase.db";
private static volatile AppDatabase instance;
public static synchronized AppDatabase getInstance(Context context) {
if (instance == null) {
instance = create(context);
}
return instance;
}
//
private static AppDatabase create(final Context context) {
return Room.databaseBuilder(
context,
AppDatabase.class,
DB_NAME).build();
}
// user
public abstract HistoryDao historyDao();
}
データベースの使用
データベースの挿入
class InsterThread extends Thread {
@Override
public void run() {
super.run();
History history = new History(" " + index, "text" + index, DateUtils.dateToStrLong(new Date()));
Log.d(TAG, "--- ---" + history);
AppDatabase.getInstance(MainActivity.this).historyDao().insert(history);
index++;
}
}
データベースの削除
class DeleteThread extends Thread {
@Override
public void run() {
super.run();
History history = new History(" " + index, "text" + index, DateUtils.dateToStrLong(new Date()));
history.setId(3);
Log.d(TAG, "-- --" + history.toString());
AppDatabase.getInstance(MainActivity.this).historyDao().delete(history);
index++;
}
}
データベースの変更
class UpdateThread extends Thread {
@Override
public void run() {
super.run();
History history = new History(" " + index, "text" + index, DateUtils.dateToStrLong(new Date()));
history.setId(2);
Log.d(TAG, "-- --" + history.toString());
AppDatabase.getInstance(MainActivity.this).historyDao().update(history);
index++;
}
}
データベースクエリー
class SelectThread extends Thread {
@Override
public void run() {
super.run();
List all = AppDatabase.getInstance(MainActivity.this).historyDao().getAll();
for (History history : all) {
Log.d(TAG, "---- ----" + history.toString());
}
}
}
ファジイクエリ
class LikeSelectThread extends Thread {
@Override
public void run() {
super.run();
List all = AppDatabase.getInstance(MainActivity.this).historyDao().getByResult("2");
for (History history : all) {
Log.d(TAG, "---- ----" + history.toString());
}
}
}
条件クエリー
class WhereSelectThread extends Thread {
@Override
public void run() {
super.run();
int[] ids = new int[]{1,3,5};
List allId = AppDatabase.getInstance(MainActivity.this).historyDao().getAllId(ids);
for (History history : allId) {
Log.d(TAG, "---- ----" + history.toString());
}
}
}
まとめ
demoダウンロード