Room使用


@Dao
interface XXXXDao {
    /**
     * 插入/替换H5秒开本地缓存
     */
    @Insert
    fun insertXXXXX(h5LocalPageConfig: H5LocalPageConfig): Long

    /*
     * 查询所有礼物
     */
    @Query("SELECT * FROM XXXX")
    fun getXXXXX(): List

}
@Entity(tableName = "xxxx")
open class xxxxxEntity() : Parcelable {
    @PrimaryKey(autoGenerate = false)
    var id: Long = 0L
    // 名称
    var pageName: String = ""
    var feature: Int = 3

    // 资源地址
    var resourceCdnUrl: String = ""
    var md5: String = ""
    var status: Int = 0

    constructor(parcel: Parcel) : this() {
        id = parcel.readLong()
        pageName = parcel.readString() ?: ""
        feature = parcel.readInt()
        resourceCdnUrl = parcel.readString() ?: ""
        md5 = parcel.readString() ?: ""
        status = parcel.readInt()
    }
    constructor(h5LocalPageConfig: H5LocalPageConfigList.H5LocalPageConfig) : this(){
        this.id = h5LocalPageConfig.id
        this.pageName = h5LocalPageConfig.pageName
        this.feature = h5LocalPageConfig.feature
        this.resourceCdnUrl = h5LocalPageConfig.resourceCdnUrl
        this.md5 = h5LocalPageConfig.md5
        this.status = h5LocalPageConfig.status

    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeLong(id)
        parcel.writeString(pageName)
        parcel.writeInt(feature)
        parcel.writeString(resourceCdnUrl)
        parcel.writeString(md5)
        parcel.writeInt(status)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator {
        override fun createFromParcel(parcel: Parcel): H5LocalPageConfig {
            return H5LocalPageConfig(parcel)
        }

        override fun newArray(size: Int): Array {
            return arrayOfNulls(size)
        }
    }
}
@Database(
    entities = [MessageListEntity::class,
        ChatEntity::class,
        UserFollowBlockUidEntity::class,
        MessageVersionEntity::class,
        GiftTakeRecordEntity::class,
        PhraseEntity::class,
        BriefProfileEntity::class,
        MsgVersionPageInfoEntity::class,
        ChatHintRecord::class,
        StrategyHistory::class,
        ValidateEntity::class,
        SensitiveWord::class,
        LiveGiftEntity::class,
        ReportRelationshipEntity::class,
        FollowRecordEntity::class,
        BuriedPointEntity::class,
        PushDataEntity::class,
        H5LocalPageConfig::class,
        xxxxEntity::class]
    , version = 19, exportSchema = false
)
@TypeConverters(Converters::class)
abstract class BMDatabase : RoomDatabase() {
    abstract fun chatDao(): ChatDao

    abstract fun userFollowBlockUidDao(): UserFollowBlockUidDao

    abstract fun validateDao(): ValidateDao

    abstract fun sensitiveDao(): SensitiveDao

    abstract fun giftDao(): GiftDao

    abstract fun redPacketReportDao(): RedPacketReportDao

    abstract fun PushMessageDao(): PushMessageDao

    abstract fun buriedDao(): BuriedOptDao

    abstract fun musicDao(): MusicDao

    abstract fun xxxxDao(): xxxxDao
}
@Module
class DBModule {

    @Singleton
    @Provides
    fun getBMDatabase(): BMDatabase {
        // return DBManager.userDatabase.value ?: DBManager.createUserDatabase()
        return DBManager.initUserDB()
    }

    @Singleton
    @Provides
    fun getChatDao(bmDatabase: BMDatabase): ChatDao {
        return bmDatabase.chatDao()
    }

    @Singleton
    @Provides
    fun getUserFollowBlockUidDao(bmDatabase: BMDatabase): UserFollowBlockUidDao {
        return bmDatabase.userFollowBlockUidDao()
    }

    @Singleton
    @Provides
    fun getValidateDao(bmDatabase: BMDatabase): ValidateDao {
        return bmDatabase.validateDao()
    }

    @Singleton
    @Provides
    fun getSensitiveDao(bmDatabase: BMDatabase): SensitiveDao {
        return bmDatabase.sensitiveDao()
    }

    @Singleton
    @Provides
    fun getGiftDao(bmDatabase: BMDatabase): GiftDao {
        return bmDatabase.giftDao()
    }

    @Singleton
    @Provides
    fun getPushMessageDao(bmDatabase: BMDatabase): PushMessageDao {
        return bmDatabase.PushMessageDao()
    }

    @Singleton
    @Provides
    fun getxxxxDao(bmDatabase: BMDatabase): xxxxtDao {
        return bmDatabase.xxxxDao()
    }
}
object DBManager {
      PPLog.i(TAG, "createNewDB")
        BM_USER_DB = newUserDb
        return Room.databaseBuilder(
            BMApplication.context,
            BMDatabase::class.java, BM_USER_DB
        ).allowMainThreadQueries()
            .addMigrations(MIGRATION_1_2)
            .addMigrations(MIGRATION_2_3)
            .addMigrations(MIGRATION_3_4)
            .addMigrations(MIGRATION_4_5)
            .addMigrations(MIGRATION_5_6)
            .addMigrations(MIGRATION_6_7)
            .addMigrations(MIGRATION_7_8)
            .addMigrations(MIGRATION_8_9)
            .addMigrations(MIGRATION_9_10)
            .addMigrations(MIGRATION_10_11)
            .addMigrations(MIGRATION_11_12)
            .addMigrations(MIGRATION_12_13)
            .addMigrations(MIGRATION_13_14)
            .addMigrations(MIGRATION_14_15)
            .addMigrations(MIGRATION_15_16)
            .addMigrations(MIGRATION_16_17)
            .addMigrations(MIGRATION_17_18)
            .addMigrations(MIGRATION_18_19)//添加表
            .build().also { bmDatabase = it }
    }
  /**
     * h5秒开
     * 3.2.0
     * 新增表[h5fast] 3.2.0
     */

    val MIGRATION_18_19:Migration=object :Migration(18,19){
        override fun migrate(database: SupportSQLiteDatabase) {
//新增表
            database.execSQL("CREATE TABLE IF NOT EXISTS `h5fast` (`id` INTEGER NOT NULL, `pageName` TEXT NOT NULL, `feature` INTEGER NOT NULL, `resourceCdnUrl` TEXT NOT NULL, `md5` TEXT NOT NULL, `status` INTEGER NOT NULL, PRIMARY KEY(`id`))")

        }

    }

}
在build指定Room文件生成
//指定room.schemaLocation生成的文件路径

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

你可能感兴趣的:(Room使用)