目录

Android开发集成AndroidxRoom+RxJava2

http://image.catbro.cn/b3add008abbf.png


  • room是google官方推出的Androidx JectPack组件中的一员,闲话就不说了,room的介绍官方和网上很多,本篇只是记录集成的配置和步骤

引入依赖

  • 根据当前最新版本来配置 implementation ‘io.reactivex.rxjava2:rxandroid:2.1.1’ implementation “io.reactivex.rxjava2:rxjava:2.2.7”

        def room_version = "2.1.0-alpha05"
        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"
    

创建Database

  • 注意,实现的是一个抽象类 @Database(entities = {UserBean.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract UserDao userDao(); private static volatile AppDatabase INSTANCE; public static AppDatabase getDatabase(Context context) { if (INSTANCE == null) { synchronized (AppDatabase.class) { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, “app_database”).build(); } } } return INSTANCE; } }

创建数据Bean

  • 以创建简单的userBean为例子

      @Entity(tableName = "user_table")
      public class UserBean {
          @PrimaryKey
          public int uid;
    
          @ColumnInfo(name = "userName")
          @NonNull
          private String userName;
          @ColumnInfo(name = "password")
          private String password;
    
          public String getUserName() {
              return userName;
          }
    
          public void setUserName(String userName) {
              this.userName = userName;
          }
    
          public String getPassword() {
              return password;
          }
    
          public void setPassword(String password) {
              this.password = password;
          }
    
          public int getUid() {
              return uid;
          }
    
          public void setUid(int uid) {
              this.uid = uid;
          }
      }
    

创建Dao类

  • 创建一个带有增加和查找方法的dao类,注意,用@Dao注解

      @Dao
      public interface UserDao {
          @Insert
          Single<Long> insert(UserBean userBean);
    
          @Query("SELECT * FROM user_table WHERE userName = :userName AND password = :password")
          Single<UserBean> findByUserNameAndPassword(String userName, String password);
      }
    

使用

  • 简单的插入一个用户信息然后查询出来

      UserBean userBean = new UserBean();
              userBean.setUid(1);
              userBean.setPassword("yes");
              userBean.setUserName("user1");
              AppDatabase.getDatabase(this).userDao().insert(userBean)
                      .flatMap(new Function<Long, SingleSource<UserBean>>() {
                          @Override
                          public SingleSource<UserBean> apply(Long aLong) throws Exception {
                              return AppDatabase.
                                      getDatabase(MainActivity.this).
                                      userDao().
                                      findByUserNameAndPassword("user1", "yes");
                          }
                      })
                      .subscribeOn(Schedulers.io())
                      .observeOn(AndroidSchedulers.mainThread())
                      .subscribe(new SingleObserver<UserBean>() {
                          @Override
                          public void onSubscribe(Disposable d) {
    
                          }
    
                          @Override
                          public void onSuccess(UserBean userBean) {
                              printLog("插入的用户名为" + userBean.getUserName());
                          }
    
                          @Override
                          public void onError(Throwable e) {
                              e.printStackTrace();
                          }
                      });