我正在使用JOOQ和普通/原始SQL,这意味着我没有使用任何代码生成或流畅的DSL东西.
以下代码有效:
Connection connection = ...;
DSLContext context = DSL.using(connection, ...);
String sql = "select * from mytable t where (t.id = ?)";
String id = ...; //
Result 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 rq = context.resultQuery(sql);
rq.getParam("id").setValue(...);
rq.getParam("is_active").setValue(...);
rq.getParam("total").setValue(...);
Result result = rq.fetch();
但上面的代码不起作用(出于显而易见的原因).提前致谢.