我嘗試從 MySql 資料庫中獲取一些隨機資料,為了獲取該資料,我使用以下查詢
@Query(nativeQuery = true,value = "select question from test_questions order by RAND() limit 4")
并將該查詢放在 Jpa Repository 介面中,我制作了這個自定義方法
public String getRandItems();
,但是當我嘗試從郵遞員那里獲取資料時給我這個錯誤
查詢沒有回傳唯一結果:4
請注意,當我使用select *而不是它時它對我有用,select question 但我不想要我只想要question column.
uj5u.com熱心網友回復:
您將結果限制為 4 行,因此您應該回傳一個字串串列而不是一個簡單的字串作為結果:
public List<String> getRandItems();
uj5u.com熱心網友回復:
是的,因為它回傳多個結果,因此可以將其存盤在單個字串中,您可以更改方法簽名
public String getRandItems();
至:
public List<String> getRandItems();
所以它接受所有結果
uj5u.com熱心網友回復:
您將結果限制為 4 行,因此您應該回傳一個字串串列而不是一個簡單的字串作為結果:
public List<String> getRandItems();
或者您可以將結果限制為 1 以僅獲取一個字串:
@Query(nativeQuery = true,value = "select question from test_questions order by RAND() limit 1")
查看它說它沒有回傳唯一結果的錯誤,因為它回傳了更多與方法回傳型別不匹配的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511710.html
標籤:爪哇mysql春天弹簧靴
