本文介绍了具有Android Room的可重用的通用基类DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过Android Room创建可重用的通用基类DAO?
Is there any way to create reusable generic base class DAOs with Android Room?
public interface BaseDao<T> {
@Insert
void insert(T object);
@Update
void update(T object);
@Query("SELECT * FROM #{T} WHERE id = :id")
void findAll(int id);
@Delete
void delete(T object);
}
public interface FooDao extends BaseDao<FooObject> { ... }
public interface BarDao extends BaseDao<BarEntity> { ... }
在没有声明相同的接口成员并为每个子类编写查询的情况下,我无法找到实现此目的的任何方法.当处理大量类似的DAO时,这变得非常乏味...
I haven't been able to figure out any way of achieving this without having to declare the same interface members and write the query for each sub class. When dealing with a large number of similar DAOs this becomes very tedious...
推荐答案
今天,2017年8月8日,版本为 1.0.0-alpha8 的以下Dao可以运行.我可以让其他的Dao英雄在GenericDao上.
Today, August 08, 2017, with version 1.0.0-alpha8 the Dao below works. I can have other Dao heroing the GenericDao.
@Dao
public interface GenericDao<T> {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(T... entity);
@Update
void update(T entity);
@Delete
void delete(T entity);
}
但是,GenericDao无法包含在我的数据库类中
However, GenericDao can not be included in my Database class
这篇关于具有Android Room的可重用的通用基类DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!