【问题标题】:How use '?' in @Query? How use Jsonb of postgres in spring-boot at all?如何使用'?在@Query 中?如何在 spring-boot 中使用 postgres 的 Jsonb?
【发布时间】:2020-05-18 19:25:51
【问题描述】:

如何使用?| Spring存储库中postgres查询中的运算符?我需要在查询中使用 where 文本类型列,其中内容为 json。

@Query(value =
        "SELECT * \n" +
        "FROM tbl t \n" +
        "WHERE t.some_ids::::jsonb ?| array['152960','188775']", nativeQuery = true
)
List<Model> getModelsByIds();

但这不起作用,我发现了下一个异常: org.springframework.dao.InvalidDataAccessApiUsageException:至少提供了 1 个参数,但查询中只存在 0 个参数。

【问题讨论】:

  • 确实,混淆层确实让 Postgres 的潜力难以发挥

标签: postgresql spring-boot


【解决方案1】:

您可以改用该运算符的关联函数。大多数情况下,混淆层也会阻塞 :: 转换运算符,因此您可能希望改用 cast()

WHERE pg_catalog.jsonb_exists_any(cast(t.some_ids as jsonb), array['152960','188775'])

但是我认为这将无法使用在 some_ids::jsonb 上定义的索引

你没有提到some_ids 的内容到底长什么样。

如果这是一个 JSON 数组(例如 '["123", "456"]'::jsonb),那么您也可以使用包含运算符 @&gt;

WHERE cast(t.some_ids as jsonb) @> '["152960","188775"]'

如果您的 JSON 数组包含数字而不是字符串 ('[123,456]'),您还需要在参数中传递数字:

WHERE cast(t.some_ids as jsonb) @> '[152960,188775]'

【讨论】:

  • 谢谢。这就是我需要的。你解释得很好。还有...你能告诉我如何在@Query 中将数组作为参数传递吗?
  • Tkanks,我使用 string_to_array(:ids, ',') 解决了它。
猜你喜欢
  • 2017-12-13
  • 1970-01-01
  • 2017-08-12
  • 2017-07-28
  • 1970-01-01
  • 2019-09-12
  • 2017-02-11
  • 1970-01-01
  • 2021-06-19
相关资源
最近更新 更多