Room数据库
1.Room使用
Step1:Gradle文件的dependencies中添加以下代码
//room数据库
compile "android.arch.persistence.room:runtime:1.1.0-beta2"
annotationProcessor "android.arch.persistence.room:compiler:1.1.0-beta2"
//rxjava2对room支持
compile "android.arch.persistence.room:rxjava2:1.1.0-beta2"
Step2:创建实体类文件,比如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") //数据表名字user_table
public class UserModel {
@PrimaryKey(autoGenerate = false) //主键,自动增长=false
@ColumnInfo(name = "user_id") //userId数据表字段为user_id
@NonNull //主键设置为非空
private String userId; //用户id
@ColumnInfo(name = "user_name") //userName数据表字段为user_name,默认允许空值
private String userName; //用户名(公司或个人姓名)
@ColumnInfo(name = "user_pet_name")
private String userPetName; //用户呢称(系统生成,之后可以修改)
@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; //用户注册时间
@ColumnInfo(name = "sex")
private int sex; //用户性别 1-->男 2-->女
@ColumnInfo(name = "integral")
private double integral; //积分
@ColumnInfo(name = "id_card_number")
private String idCardNumber; //身份证号
@ColumnInfo(name = "email")
private String email; //用户邮箱
@ColumnInfo(name = "status")
private int status; //用户状态 0-->禁用 1-->正常
public UserModel() { //Room调用的构造函数
}
@Ignore //有2个构造函数,让Room忽略掉这个,使用上面一个构造函数
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 +
'}';
}
//=================================================================
// 以为字段设置成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:创建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标识
@Dao
public interface UserModelDao {
//查询语句
@Query("SELECT * FROM user_table WHERE user_id = :id")
Flowable<UserModel> getUserModel(String id);
//插入,onConflict = OnConflictStrategy.REPLACE表示插入有冲突时候替代
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(UserModel userModel);
@Delete
void delete(UserModel userModel);
}
Step4:创建提供Dao层的抽象类,继承RoomDatabase
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
//创建一个抽象类。用@Database标识,表示是RoomDatabase
//entities = {UserModel.class},表示数据库存储的实体类有UserModel
//version = 1,表示数据库版本号为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为我们实现抽象类
instance = Room.databaseBuilder(Util.getApplication(),
AppDatabase.class,
Constant.DATABASE_NAME).build();
}
}
}
return instance;
}
}
Step5:使用
AppDatabase.getInstance()
.getUserModelDao()
.insert(userModel);
常见问题
- 无法import RoomDatabase
- 因为RoomDatabase需要com.android.support在26.0.1之后
- 项目中com.android.support修改为26.0.1之后就可以了