查询id的需要整型参数
问题描述:
中提及以下文件: http://ormlite.com/docs/types查询id的需要整型参数
Account account = accountDao.queryForId("John Smith");
if (account == null) {
// the name "John Smith" does not match any rows
}
但在Eclipse(机器人)我只看到选项整数关口作为参数? 有帮助吗?
答
的Dao
对象使用泛型执行与您的实体相关联的ID类型。如果你只看到选项整数关口进入dao.queryForId(...)
那么你很可能,错误,这样定义的道:
Dao<Account, Integer> accountDao = getDao(Account.class);
第一个泛型参数指定实体和第二泛型参数的类型指定的类型该实体中的ID字段。使用Integer
,您将拨打accountDao.queryForId(Integer)
。
正如@Tomas提到的,你需要的东西定义你的DOA,如:
Dao<Account, String> accountDao = getDao(Account.class);
然后,你可以通过一个String
ID为Account
查询:
Account account = accountDao.queryForId("John Smith");
答
首先,你应该定义实体ID为String类型的内容:
@DatabaseTable()
public class Account {
@DatabaseField(id = true)
private String mFullName;
...
}
那么你应该根据实体类型的蚂蚁其ID类型声明DAO对象。现在你可以使用queryForId ID为String类型:
Dao<Account, String> accountDao = getAccountDao();
Account account = accountDao.queryForId("John Smith");
if (account == null) {
// the name "John Smith" does not match any rows
}
谢谢!这解决了我的问题。 – Eli 2013-03-29 14:29:47