2016-01-07 19 views
1

我正在使用JDBI發出一個簡單的mysql選擇查詢。在這種情況下,如果我嘗試綁定表名,則會出現錯誤的SQL語法錯誤。將表名與使用JDBI的SQL查詢綁定

代碼片段:

@SqlQuery("select * from <table> where rowid=:rowid") 
@SingleValueResult 
@Mapper(ContentRecordMapper.class) 
public abstract Optional<Document> getRecord(@Define("table") String table, @Bind("rowid") String rowid); 

如果我執行此,我得到

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<table> where rowid='row1'' at line 1 [statement:"select * from <table> where rowid=:rowid", located:"select * from <table> where rowid=:rowid", rewritten:"/* ContentDAO.getRecord */ select * from <table> where rowid=?", arguments:{ positional:{}, named:{rowid:'row1'}, finder:[]}] 

如果我硬編碼表名,我得到正確的結果。請幫助。 謝謝

回答

2

您應該使用@ UseStringTemplate3StatementLocator進行註釋。已經討論過here

+0

感謝Manikandan。這樣可行 :) – sriram