使用 JDBI 将 table 名称绑定到 SQL 查询

Binding table names to SQL query using JDBI

我正在使用 JDBI 发出一个简单的 mysql select 查询。如果我尝试绑定 table 名称,我会出错 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:[]}]

如果我硬编码 table 名称,我会得到正确的结果。请帮忙。 谢谢

您应该使用@UseStringTemplate3StatementLocator 进行注释。已经讨论过 here