【问题标题】:Jooq - Plain SQL Parameterized Array/ListJooq - 普通 SQL 参数化数组/列表
【发布时间】:2016-10-08 06:03:02
【问题描述】:

我想在“IN”参数中提供集合/数组,但我得到了

当我使用数组时:

org.jooq.exception.SQLDialectNotSupportedException: Cannot bind ARRAY types in dialect MYSQL    

当我使用列表时:

org.jooq.exception.SQLDialectNotSupportedException: Type class java.util.Arrays$ArrayList is not supported in dialect DEFAULT

这是我的普通 sql:

String sql = "SELECT SUM(foo.reply = 'Y') yes " +
            "FROM foo " +
            "LEFT OUTER JOIN bar " +
            "ON foo.id = bar.foo_id " +
            "WHERE " +
            "foo.id = ? " +
            "AND foo.some_id IN (?) "; //this is the part I would like to use array or list

这是我的执行方式

dslContext.fetch(sql, val(fooId), val(someIds))
                    .into(Summary.class);

【问题讨论】:

    标签: java mysql jdbc jooq


    【解决方案1】:

    你不能使用单个绑定变量来做到这一点(PostgreSQL 中的数组除外)。但是您可以在 jOOQ 中使用嵌套的纯 SQL 查询部分:

    String sql = "SELECT SUM(foo.reply = 'Y') yes " +
                "FROM foo " +
                "LEFT OUTER JOIN bar " +
                "ON foo.id = bar.foo_id " +
                "WHERE " +
                "foo.id = {0} " +
                "AND foo.some_id IN ({1}) "; // Use the {0}, {1} placeholders
    

    然后

    dslContext.fetch(sql, 
                   val(fooId), 
                   DSL.list(someIds.stream().map(DSL::val).collect(toList())))
              .into(Summary.class);
    

    另见DSL.list(QueryPart...)

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      相关资源
      最近更新 更多