【问题标题】:How to used a NamedJDBCTemplate placeholder in the where clause of a postgres ARRAY comparison?如何在 postgres ARRAY 比较的 where 子句中使用 NamedJDBCTemplate 占位符?
【发布时间】:2022-01-15 00:01:46
【问题描述】:

我正在尝试使用 spring-jdbc 使用 ARRAY/GIN where 子句进行 postgres 查询。 当我尝试在 ARRAY[] 中使用命名参数时,NamedParameterJdbcTemplate 会引发错误。

小例子:

CREATE TABLE testing(col1 text[]);
INSERT INTO testing VALUES ('{item1, item2, item3}');
INSERT INTO testing VALUES ('{item4, item5, item6}');

=> select * from testing where col1 @> ARRAY['item4'];
        col1         
---------------------
 {item4,item5,item6}

Java:

        var sql = "select * from testing where col1 @> ARRAY[:item]";
        var paramMap = Map.of("item", "item4");
        var results = namedJDBCTemplate.queryForList(sql, paramMap);

例外:

org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'item]': No value registered for key 'item]'
    at org.springframework.jdbc.core.namedparam.NamedParameterUtils.buildValueArray
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForList
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForList

【问题讨论】:

    标签: java postgresql spring-boot jdbc spring-jdbc


    【解决方案1】:

    在参数周围添加空格并将其转换为 sql 查询,例如

    select * from testing where col1 @> ARRAY[ CAST ( :item AS text) ]
    

    修好了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2019-03-28
      • 2017-05-09
      • 1970-01-01
      相关资源
      最近更新 更多