Room數(shù)據(jù)庫
1.Room使用
Step1:Gradle文件的dependencies中添加以下代碼
//room數(shù)據(jù)庫
compile "android.arch.persistence.room:runtime:1.1.0-beta2"
annotationProcessor "android.arch.persistence.room:compiler:1.1.0-beta2"
//rxjava2對(duì)room支持
compile "android.arch.persistence.room:rxjava2:1.1.0-beta2"
Step2:創(chuàng)建實(shí)體類文件,比如UserModel.java
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
@Entity(tableName = "user_table") //數(shù)據(jù)表名字user_table
public class UserModel {
@PrimaryKey(autoGenerate = false) //主鍵均函,自動(dòng)增長=false
@ColumnInfo(name = "user_id") //userId數(shù)據(jù)表字段為user_id
@NonNull //主鍵設(shè)置為非空
private String userId; //用戶id
@ColumnInfo(name = "user_name") //userName數(shù)據(jù)表字段為user_name众眨,默認(rèn)允許空值
private String userName; //用戶名(公司或個(gè)人姓名)
@ColumnInfo(name = "user_pet_name")
private String userPetName; //用戶呢稱(系統(tǒng)生成炼团,之后可以修改)
@ColumnInfo(name = "user_phone")
private String userPhone; //用戶電話
@ColumnInfo(name = "user_icon")
private String userIcon; //用戶頭像
@ColumnInfo(name = "vip_type")
private int vipType; //用戶vip類型
@ColumnInfo(name = "create_time")
private long createTime; //用戶注冊(cè)時(shí)間
@ColumnInfo(name = "sex")
private int sex; //用戶性別 1-->男 2-->女
@ColumnInfo(name = "integral")
private double integral; //積分
@ColumnInfo(name = "id_card_number")
private String idCardNumber; //身份證號(hào)
@ColumnInfo(name = "email")
private String email; //用戶郵箱
@ColumnInfo(name = "status")
private int status; //用戶狀態(tài) 0-->禁用 1-->正常
public UserModel() { //Room調(diào)用的構(gòu)造函數(shù)
}
@Ignore //有2個(gè)構(gòu)造函數(shù)估脆,讓Room忽略掉這個(gè)贫导,使用上面一個(gè)構(gòu)造函數(shù)
public UserModel(String userIcon, String userName) {
this.userIcon = userIcon;
this.userName = userName;
}
@Override
public String toString() {
return "UserModel{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", userPetName='" + userPetName + '\'' +
", userPhone='" + userPhone + '\'' +
", userIcon='" + userIcon + '\'' +
", vipType=" + vipType +
", createTime=" + createTime +
", sex=" + sex +
", integral=" + integral +
", idCardNumber='" + idCardNumber + '\'' +
", email='" + email + '\'' +
", status=" + status +
'}';
}
//=================================================================
// 以為字段設(shè)置成private弄兜,提供get\set方法給room使用
//=================================================================
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPetName() {
return userPetName;
}
public void setUserPetName(String userPetName) {
this.userPetName = userPetName;
}
public String getUserPhone() {
return userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
public String getUserIcon() {
return userIcon;
}
public void setUserIcon(String userIcon) {
this.userIcon = userIcon;
}
public int getVipType() {
return vipType;
}
public void setVipType(int vipType) {
this.vipType = vipType;
}
public long getCreateTime() {
return createTime;
}
public void setCreateTime(long createTime) {
this.createTime = createTime;
}
public int getSex() {
return sex;
}
public String getSexString() {
return getSex() == 1 ? Util.getString(R.string.man) : Util.getString(R.string.woman);
}
public void setSex(int sex) {
this.sex = sex;
}
public double getIntegral() {
return integral;
}
public void setIntegral(double integral) {
this.integral = integral;
}
public String getIdCardNumber() {
return idCardNumber;
}
public void setIdCardNumber(String idCardNumber) {
this.idCardNumber = idCardNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Step3:創(chuàng)建Dao層
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import io.reactivex.Flowable;
//Dao層用@Dao標(biāo)識(shí)
@Dao
public interface UserModelDao {
//查詢語句
@Query("SELECT * FROM user_table WHERE user_id = :id")
Flowable<UserModel> getUserModel(String id);
//插入讥电,onConflict = OnConflictStrategy.REPLACE表示插入有沖突時(shí)候替代
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(UserModel userModel);
@Delete
void delete(UserModel userModel);
}
Step4:創(chuàng)建提供Dao層的抽象類御毅,繼承RoomDatabase
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
//創(chuàng)建一個(gè)抽象類。用@Database標(biāo)識(shí)羽杰,表示是RoomDatabase
//entities = {UserModel.class}渡紫,表示數(shù)據(jù)庫存儲(chǔ)的實(shí)體類有UserModel
//version = 1到推,表示數(shù)據(jù)庫版本號(hào)為1
@Database(entities = {UserModel.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
//抽象方法,提供UserModelDao
public abstract UserModelDao getUserModelDao();
//單例
private static volatile AppDatabase instance;
//獲取單例
public static AppDatabase getInstance() {
if (instance == null) {
synchronized (AppDatabase.class) {
if (instance == null) {
//Room為我們實(shí)現(xiàn)抽象類
instance = Room.databaseBuilder(Util.getApplication(),
AppDatabase.class,
Constant.DATABASE_NAME).build();
}
}
}
return instance;
}
}
Step5:使用
AppDatabase.getInstance()
.getUserModelDao()
.insert(userModel);
常見問題
- 無法import RoomDatabase
- 因?yàn)镽oomDatabase需要com.android.support在26.0.1之后
- 項(xiàng)目中com.android.support修改為26.0.1之后就可以了