美文网首页
Kotlin中Room的简单使用

Kotlin中Room的简单使用

作者: 孤诣 | 来源:发表于2020-05-08 15:14 被阅读0次

    增加引用

      // kotlin
      implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
      implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
      implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
      // Room
      implementation "androidx.room:room-runtime:2.2.5"
      kapt "androidx.room:room-compiler:2.2.5"
    

    apply

    apply plugin: 'kotlin-kapt'
    

    导出数据库建表语句

    android {
    // ...
        defaultConfig {
            //指定room.schemaLocation生成的文件路径  处理Room 警告 Schema export Error
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                            "room.schemaLocation"  : "$projectDir/schemas".toString(),
                            "room.incremental"     : "true",
                            "room.expandProjection": "true"]
                }
            }
        }
    // ...
    }
    

    实体类

    @Entity(tableName = "worker")
    class Worker() : Parcelable {
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "_id")
        var id: Long = 0
        lateinit var name: String
    
        constructor(parcel: Parcel) : this() {
            id = parcel.readLong()
            name = parcel.readString().toString()
        }
    
        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeLong(id)
            parcel.writeString(name)
        }
    
        override fun describeContents(): Int {
            return 0
      }
    
        companion object CREATOR : Parcelable.Creator<Worker> {
            override fun createFromParcel(parcel: Parcel): Worker {
                return Worker(parcel)
            }
    
            override fun newArray(size: Int): Array<Worker?> {
                return arrayOfNulls(size)
            }
        }
    }
    

    Dao

    @Dao 
    interface WorkerDao {
        @Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insertWorker(worker: Worker)
    
        @Query("DELETE FROM worker WHERE name=:name")
        fun deleteWorker(name: String)
    
        @Query("SELECT * FROM worker")
        fun queryAllWorkers(): List<Worker>
    }
    

    创建数据库

    @Database(entities = [Worker::class], version = 1)
    abstract class KotDatabase : RoomDatabase() {
        abstract fun getWorkerDao(): WorkerDao
    
        companion object {
            @Volatile
            private var INSTANCE: KotDatabase? = null
            fun getInstance(context: Context): KotDatabase =
                    INSTANCE ?: synchronized(this) {
                       INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
                    }    
            private fun buildDatabase(context: Context) =
                    Room.databaseBuilder(context.applicationContext, KotDatabase::class.java, "kot.db")
                            .build()
        }
    }
    

    GlobalScope.launch(Dispatchers.IO) {
      val worker = Worker()
      worker.name = "李华"
      KotDatabase.getInstance(applicationContext).getWorkerDao().insertWorker(worker)
      withContext(Dispatchers.Main) {
          Toast.makeText(applicationContext, "插入完成", Toast.LENGTH_SHORT).show()
    } }
    

    GlobalScope.launch(Dispatchers.IO) {
      KotDatabase.getInstance(applicationContext).getWorkerDao().deleteWorker("李华")
      withContext(Dispatchers.Main) {
          Toast.makeText(applicationContext, "删除完成", Toast.LENGTH_SHORT).show()
    } }
    

    GlobalScope.launch(Dispatchers.IO) {
      val workers = KotDatabase.getInstance(applicationContext).getWorkerDao().queryAllWorkers()
      withContext(Dispatchers.Main) {
          Toast.makeText(applicationContext, "查询完成", Toast.LENGTH_SHORT).show()
    } }
    

    升级数据库

    实体类,新增age字段

    @Entity(tableName = "worker")
    class Worker() : Parcelable {
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "_id")
        var id: Long = 0
        var age: Int = 0
        lateinit var name: String
    
        constructor(parcel: Parcel) : this() {
            id = parcel.readLong()
            age = parcel.readInt()
            name = parcel.readString().toString()
        }
    
        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeLong(id)
            parcel.writeInt(age)
            parcel.writeString(name)
        }
    
        override fun describeContents(): Int {
            return 0
      }
    
        override fun toString(): String {
            return "Worker(id=$id, age=$age, name='$name')"
      }
    
        companion object CREATOR : Parcelable.Creator<Worker> {
            override fun createFromParcel(parcel: Parcel): Worker {
                return Worker(parcel)
            }
    
            override fun newArray(size: Int): Array<Worker?> {
                return arrayOfNulls(size)
            }
        }
    }
    

    数据库

    @Database(entities = [Worker::class], version = 2)
    abstract class KotDatabase : RoomDatabase() {
        abstract fun getWorkerDao(): WorkerDao
    
        companion object {
            @Volatile
            private var INSTANCE: KotDatabase? = null
            fun getInstance(context: Context): KotDatabase =
                    INSTANCE ?: synchronized(this) {
                        INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
                    }    
            private fun buildDatabase(context: Context) =
                    Room.databaseBuilder(context.applicationContext, KotDatabase::class.java, "kot.db")
                            .addMigrations(migration1to2)
                            .build()
    
            private val migration1to2: Migration = object : Migration(1, 2) {
                override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL("ALTER TABLE worker ADD COLUMN 'age' INTEGER NOT NULL")
                }
            }
        }
    }
    

    多版本迁移

    @Database(entities = [Worker::class], version = 3)
    abstract class KotDatabase : RoomDatabase() {
        abstract fun getWorkerDao(): WorkerDao
    
        companion object {
            @Volatile
            private var INSTANCE: KotDatabase? = null
            fun getInstance(context: Context): KotDatabase =
                    INSTANCE ?: synchronized(this) {
                        INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
                    }    
            private fun buildDatabase(context: Context) =
                    Room.databaseBuilder(context.applicationContext, KotDatabase::class.java, "kot.db")
                            .addMigrations(migration1to2, migration2to3)
                            .build()
    
            private val migration1to2: Migration = object : Migration(1, 2) {
                override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL("ALTER TABLE worker ADD COLUMN 'age' INTEGER NOT NULL")
                }
            }
    
            private val migration2to3: Migration = object : Migration(2, 3) {
                override fun migrate(database: SupportSQLiteDatabase) {
                    // 新建表
                    database.execSQL("CREATE TABLE IF NOT EXISTS worker_new (`_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `age` INTEGER NOT NULL, `name` TEXT NOT NULL)")
                    // 复制表
                    database.execSQL("INSERT INTO worker_new (`_id`, `age`, `name`) SELECT `_id`, `age`, `name` FROM worker")
                    // 删除表
                    database.execSQL("DROP TABLE worker")
                    // 重命名表
                    database.execSQL("ALTER TABLE worker_new RENAME TO worker")
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Kotlin中Room的简单使用

          本文链接:https://www.haomeiwen.com/subject/dxmfnhtx.html