【问题标题】:How to use named parameters instead of question marks using jOOQ如何使用命名参数而不是使用 jOOQ 的问号
【发布时间】:2019-07-04 02:51:25
【问题描述】:

我正在使用以下 jOOQ 配置:

private fun createConfiguration(dataSource: DataSource, loggerListener: JooqLoggerListener): Configuration {
    return DefaultConfiguration()
            .set(dataSource)
            .set(SQLDialect.POSTGRES_10)
            .set(DefaultExecuteListenerProvider(loggerListener))
            .set(Settings()
                    .withExecuteLogging(false) // We already use the custom [JooqLoggerListener].
                    .withParamType(ParamType.NAMED)
                    .withRenderFormatted(true)
                    .withRenderKeywordStyle(RenderKeywordStyle.UPPER)
                    .withRenderNameStyle(RenderNameStyle.LOWER))
}

除withParamType(ParamType.NAMED) 外,所有设置均有效。

但是将 SQL 记录在我的自定义 JooqLoggerListener 中,例如:

override fun renderEnd(ctx: ExecuteContext) {
    if (logger.isDebugEnabled) {
        val configuration = ctx.configuration()
        val newline = if (configuration.settings().isRenderFormatted) "\n" else ""
        val inlined = DSL.using(configuration).renderInlined(ctx.query())
        logger.debug("Executing query:{}{}", newline, ctx.sql())
        logger.debug("Executing query (with bind values):{}{}", newline, inlined)
    }
}

仍然打印问号:

13:41:47.472 [nioEventLoopGroup-1-3] DEBUG c.e.logging.JooqLoggerListener - Executing query:
SELECT 
  country.id, 
  country.alpha2_code, 
  country.alpha3_code, 
  country.name, 
  country.demonym, 
  country.continent_id
FROM country
WHERE country.id = ?

我本来希望是这样的:

WHERE country.id = :id

我的假设是正确的还是withParamType(ParamType.NAMED) 用于其他用途?

【问题讨论】:

    标签: sql kotlin jooq


    【解决方案1】:

    要使用参数名称,需要在查询中提供参数名称。

    我第一次拥有:

            return dslContext
                    .selectFrom(COUNTRY)
                    .where(COUNTRY.ID.eq(id))
                    .fetchOneInto(Country::class.java)
    

    改成之后:

            return dslContext
                    .selectFrom(COUNTRY)
                    .where(COUNTRY.ID.eq(param("country_id", id)))
                    .fetchOneInto(Country::class.java)
    

    成功了!

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 2015-09-23
      • 1970-01-01
      • 2020-11-29
      • 2019-04-15
      相关资源
      最近更新 更多