java-Jooq-普通SQL参数化数组/列表

我想在“ 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);

解决方法:

您不能使用单个绑定变量来做到这一点(在带有数组的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...)

上一篇:java-Jooq模式区分大小写


下一篇:JOOQ Java 8 Schema是Abstract且无法实例化