2021SC@SDUSC
Room本地数据库
使用 Room 将数据保存到本地数据库
1.添加依赖
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava2 support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - RxJava3 support for Room
implementation "androidx.room:room-rxjava3:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
// optional - Paging 3 Integration
implementation "androidx.room:room-paging:2.4.0-beta02"
}
Room 包含 3 个主要组件:
应用使用 Room 数据库来获取与该数据库关联的数据访问对象 (DAO)。然后,应用使用每个 DAO 从数据库中获取实体,然后再将对这些实体的所有更改保存回数据库中。 最后,应用使用实体来获取和设置与数据库中的表列相对应的值。
Room 不同组件之间的关系如图 1 所示:
图 1.Room 架构图
appdatebase初始化
package com.example.minichatapp.db;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import com.example.minichatapp.data.model.Friend;
import com.example.minichatapp.db.dao.FriendDao;
import com.example.minichatapp.db.dao.UserDao;
import com.example.minichatapp.db.model.FriendRoom;
import com.example.minichatapp.db.model.UserRoom;
@Database(entities = {UserRoom.class, FriendRoom.class},version = 1,exportSchema = false)
public abstract class APPDataBase extends RoomDatabase {
public abstract UserDao userDao();
public abstract FriendDao friendDao();
}
dao设计
FriendDao.java
package com.example.minichatapp.db.dao;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import com.example.minichatapp.db.model.FriendRoom;
import java.util.List;
import io.reactivex.rxjava3.core.Completable;
@Dao
public interface FriendDao {
@Query("select * from friend where uid=:uid")
LiveData<List<FriendRoom>> getFriendsByUId(String uid);
@Insert(onConflict = OnConflictStrategy.REPLACE)
Completable insertFriends(List<FriendRoom> roomList);
@Insert(onConflict = OnConflictStrategy.REPLACE)
Completable insertFriendOne(FriendRoom friendRoom);
}
UserDao.java
package com.example.minichatapp.db.model;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "user")
public class UserRoom {
@PrimaryKey
@ColumnInfo(name = "uid")
@NonNull
public String uid = null;
@ColumnInfo(name = "user_name")
public String userName;
@ColumnInfo(name = "is_log")
public int isLog;
@Override
public String toString() {
return "UserRoom{" +
"uid='" + uid + '\'' +
", userName='" + userName + '\'' +
", isLog=" + isLog +
'}';
}
}
3.表设计
UserRoom.java
package com.example.minichatapp.db.model;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "user")
public class UserRoom {
@PrimaryKey
@ColumnInfo(name = "uid")
@NonNull
public String uid = null;
@ColumnInfo(name = "user_name")
public String userName;
@ColumnInfo(name = "is_log")
public int isLog;
@Override
public String toString() {
return "UserRoom{" +
"uid='" + uid + '\'' +
", userName='" + userName + '\'' +
", isLog=" + isLog +
'}';
}
}
FriendRoom.java
package com.example.minichatapp.db.model;
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "friend",primaryKeys = {"fid","uid"})
public class FriendRoom {
@ColumnInfo(name = "fid")
@NonNull
public String fid="-1";
@ColumnInfo(name = "uid")
@NonNull
public String uid="-1";
@ColumnInfo(name = "f_name")
public String f_name;
@ColumnInfo(name = "nick_name")
public String nick_name;
public FriendRoom(@NonNull String fid, @NonNull String uid, String f_name, String nick_name) {
this.fid = fid;
this.uid = uid;
this.f_name = f_name;
this.nick_name = nick_name;
}
}
|