我有一个下面的界面,用于创建标准的crud方法,并使用插入,更新和删除方法对方法进行注释。
interface BaseDao<T> {
@Insert
fun insert(table: T): Single<Long>
@Insert
fun insert(vararg table: T): Single<List<Long>>
@Update
fun update(table: T): Single<Int>
@Delete
fun delete(table: T): Single<Int>
}
然后我为DAO创建一个接口
@Dao
interface WeatherDao : BaseDao<WeatherTable> {
override fun insert(table: WeatherTable): Single<Long>
override fun insert(vararg table: WeatherTable): Single<List<Long>>
override fun update(table: WeatherTable): Single<Int>
override fun delete(table: WeatherTable): Single<Int>
@Query("SELECT * FROM weatherTable")
fun getAllWeather(): Single<List<WeatherTable>>
@Query("SELECT * FROM weatherTable WHERE id = :id LIMIT 1")
fun getWeatherById(id: Long): Single<WeatherTable>
@Query("SELECT count(*) FROM weatherTable")
fun count(): Single<Int>
}
当我编译时,会出现很多如下错误:
error: An abstract DAO method must be annotated with one and only one of the following annotations: Insert,Delete,Query,Update,RawQuery
public abstract io.reactivex.Single<java.lang.Long> delete(@org.jetbrains.annotations.NotNull()
因为当我从接口继承时。我必须手动添加@ Insert,@ Update和@Delete。
只是想知道为什么这些注释会自动添加到我的WeatherDao界面中。
所以我现在必须像这样手动添加它们:
@Dao
interface WeatherDao : BaseDao<WeatherTable> {
@Insert
override fun insert(table: WeatherTable): Single<Long>
@Insert
override fun insert(vararg table: WeatherTable): Single<List<Long>>
@Update
override fun update(table: WeatherTable): Single<Int>
@Delete
override fun delete(table: WeatherTable): Single<Int>
@Query("SELECT * FROM weatherTable")
fun getAllWeather(): Single<List<WeatherTable>>
@Query("SELECT * FROM weatherTable WHERE id = :id LIMIT 1")
fun getWeatherById(id: Long): Single<WeatherTable>
@Query("SELECT count(*) FROM weatherTable")
fun count(): Single<Int>
}
只是想知道我是否使用了这个错误:
答案 0 :(得分:1)
遵循此google repo之后,您没有正确进行抽象。综上所述,您无需在@Dao接口中插入/更新/删除操作,它应该是抽象的。
interface BaseDao<T> {
/**
* Insert an object in the database.
*
* @param obj the object to be inserted.
*/
@Insert
fun insert(obj: T)
/**
* Insert an array of objects in the database.
*
* @param obj the objects to be inserted.
*/
@Insert
fun insert(vararg obj: T)
/**
* Update an object from the database.
*
* @param obj the object to be updated
*/
@Update
fun update(obj: T)
/**
* Delete an object from the database
*
* @param obj the object to be deleted
*/
@Delete
fun delete(obj: T)
}
@Entity(tableName = "data")
data class Data(@PrimaryKey val id: String, val value: String)
@Dao
abstract class DataDao : BaseDao<Data>() {
/**
* Get all data from the Data table.
*/
@Query("SELECT * FROM Data")
abstract fun getData(): List<Data>
}