gpt4 book ai didi

kotlin - Unresolved reference : RoomDatabase

转载 作者:行者123 更新时间:2023-12-05 06:09:21 27 4
gpt4 key购买 nike

我正在尝试在我的应用程序中设置房间(按照文档)。除了我的 Database 类外,一切都编译得很好。找不到 RoomDatabase 类。可能值得注意的是,我使用的是 Jetpack Compose 桌面应用程序,而不是在 Android 上。

import androidx.room.*

@Entity
data class User(
@PrimaryKey val uid: Int,
@ColumnInfo(name = "first_name") val firstName: String?,
@ColumnInfo(name = "last_name") val lastName: String?
)


@Dao
interface UserDao {
@Query("SELECT * FROM user")
fun getAll(): List<User>

@Query("SELECT * FROM user WHERE uid IN (:userIds)")
fun loadAllByIds(userIds: IntArray): List<User>

@Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
"last_name LIKE :last LIMIT 1")
fun findByName(first: String, last: String): User

@Insert
fun insertAll(vararg users: User)

@Delete
fun delete(user: User)
}

@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

apply plugin: 'kotlin-kapt'

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
google()
jcenter()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
def room_version = "2.2.5"
implementation 'androidx.core:core-ktx:1.2.0'

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"

}

最佳答案

问题是所有内容都在一个文件中。您需要创建一个 User.kt 文件:

@Entity
data class User(
@PrimaryKey val uid: Int,
@ColumnInfo(name = "first_name") val firstName: String?,
@ColumnInfo(name = "last_name") val lastName: String?
)

然后,一个 UserDao kotlin 文件:

@Dao
interface UserDao {
@Query("SELECT * FROM user")
fun getAll(): List<User>

@Query("SELECT * FROM user WHERE uid IN (:userIds)")
fun loadAllByIds(userIds: IntArray): List<User>

@Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
"last_name LIKE :last LIMIT 1")
fun findByName(first: String, last: String): User

@Insert
fun insertAll(vararg users: User)

@Delete
fun delete(user: User)
}

然后是应用数据库kotlin文件:

@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}

我的建议是遵循this tutorial .它将教您基础知识并引导您完成一个完整的应用程序。祝你好运:)

关于kotlin - Unresolved reference : RoomDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64811775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com