[英]How to bind variables to conditional statement when using jOOQ to build SQL?
I'm using jOOQ-3.11.9 to build SQL. 我正在使用jOOQ-3.11.9构建SQL。 The following is my code: 以下是我的代码:
String sql = DSL.using(SQLDialect.MYSQL)
.select(DSL.asterisk())
.from(table("service"))
.where("name = ?", "service1")
.getSQL();
What I expect is 我期望的是
select * from service where (name = "service1")
But the result is 但是结果是
select * from service where (name = ?)
Is there anything wrong with my code? 我的代码有什么问题吗?
You should use the field name from jOOQ-generated classes: 您应该使用jOOQ生成的类中的字段名称:
String sql = DSL.using(SQLDialect.MYSQL)
.select(DSL.asterisk())
.from(YOURENTITY)
.where(YOURENTITY.NAME.eq(nameParam))
.getSQL();
YOURENTITY
should be the jOOQ-generated class in your project. YOURENTITY
应该是项目中jOOQ生成的类。 nameParam
would then be an argument passed to the method wrapping the above query. 然后, nameParam
将成为传递给包装以上查询的方法的参数。
jOOQ has pretty good docs with lots of examples: https://www.jooq.org/doc/3.11/manual/sql-building/sql-statements/select-statement/where-clause/ jOOQ的文档非常不错,有很多示例: https ://www.jooq.org/doc/3.11/manual/sql-building/sql-statements/select-statement/where-clause/
This works as expected. 这按预期工作。 The default Settings.statementType
value is StatementType.PREPARED_STATEMENT
, so jOOQ by default generates bind value placeholders in your SQL string, which can be used for execution in other tools, such as JDBC, Spring, etc. 默认的Settings.statementType
值是StatementType.PREPARED_STATEMENT
,因此默认情况下,jOOQ在您的SQL字符串中生成绑定值占位符,可用于在其他工具(例如JDBC,Spring等)中执行。
You can pass the ParamType.INLINE
value to the getSQL()
method, or specify Settings.withStatementType(StatementType.STATIC_STATEMENT)
您可以将ParamType.INLINE
值传递给getSQL()
方法,或指定Settings.withStatementType(StatementType.STATIC_STATEMENT)
Please consider the explanation in the Javadoc for more details: https://www.jooq.org/javadoc/latest/org/jooq/Query.html#getSQL-- 有关更多详细信息,请考虑Javadoc中的说明: https : //www.jooq.org/javadoc/latest/org/jooq/Query.html#getSQL--
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.