簡體   English   中英

ORMLite + MySQL:如何找到一條匹配的記錄?

[英]ORMLite + MySQL: how to find one matching record?

我有一個名為“ Article”的Java類,它使用ORMLite與MySQL數據庫進行連接/通信。 我的插入工作正常,但是我有一個問題,在給定一個“ id”(在mysql數據庫表中自動遞增)的情況下,僅檢索了一篇文章(對應於mysql數據庫表中的一行)。

看來我必須使用List變量來保存查詢結果:

Dao<Article,String> articleDAO = DaoManager.createDao(connectionSource, Article.class);
List<Article> article = articleDAO.queryForEq("id", id); /* is there a method to find only one matching record and returns an Article type ? */

我希望做這樣的事情:

Article article = articleDAO.queryOne("id", id);

您查詢List<Article> article = articleDAO.queryForEq("id", id); 而不是這樣想:

if (article != null && article.size() !=0){
  return article.get(0)
} else {
  return null;
}

文章article = articleDAO.queryOne(“ id”,id);

嗯,您閱讀過任何在線文檔嗎? dao.queryForId(id)是在各處引用的內容。 它旨在返回與行的唯一ID匹配的值,方法與dao.update(...)dao.refresh(...)更改並從數據庫讀取該行的方式相同。

另外,還有一個dao.queryForFirst(preparedQuery)從查詢中獲取第一個結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM