3
我正在使用JOOQ與plain/raw SQL,這意味着我不使用任何代碼生成或流體DSL thingy。如何在jooq中使用明確的sql命名參數
下面的代碼工作:
Connection connection = ...;
DSLContext context = DSL.using(connection, ...);
String sql = "select * from mytable t where (t.id = ?)";
String id = ...; //
Result<Record> result = context.fetch(sql, id);
現在讓我們說我有這樣的多個參數的查詢:
String sql = "select * from mytable t where (t.id = ?) " +
"and (t.is_active = ?) and (t.total > ?)";
如何使用這些類型的查詢命名參數? 我想是這樣的:
String sql = "select * from mytable t where (t.id = :id) " +
"and (t.is_active = :is_active) and (t.total > :total)";
ResultQuery<Record> rq = context.resultQuery(sql);
rq.getParam("id").setValue(...);
rq.getParam("is_active").setValue(...);
rq.getParam("total").setValue(...);
Result<Record> result = rq.fetch();
但上面的代碼無法正常工作(原因很明顯)。提前致謝。
好吧,我接受的答案,讓我改的問題了一下:假設我有一個SQL語句的幾個參數,jooq不支持命名參數,建議的解決方法是什麼?手動字符串替換是icky ... – tschan 2013-05-12 15:04:06
解決方法是使用索引參數,當然...我不認爲目前有這樣做的優雅方式,缺乏索引參數,或使用手動字符串替換。 .. – 2013-05-12 15:18:24