数组包含到带有 Spring 数据 JDBC 的 postgres jsonb
Array contains into postgres jsonb with Spring data JDBC
让我们踏上旅程...
我想达到的是
SELECT * FROM people WHERE interest->'interests' ?| ARRAY['sport','cars'];
进入我定义为
的存储库
public interface PeopleRepository extends CrudRepository<People, UUID> {
@Query("SELECT * FROM people where interest->'interests' ??| :array::text[] ")
List<People> findByInterest(@Param("array") String interest);
最后select兴趣,就这样使用方法
findByInterest("{foo, beer}")
这是我经过一天的测试和
恕我直言真是MEH
我认为除此之外,最好的解决方案是可能的
(在运行时没有 sql 转换和字符串连接)
你能帮忙提供一个更“干净的代码”的解决方案吗?
(抱歉,长篇post)
更新
我会详细说明我的问题。
我正在搜索类似
的内容
@Query("SELECT * FROM people where interest->'interests' ??| :array::text[] ")
List<People> findByInterest(@Param("array") List<String> interest);
这可能吗?
JSON数组元素按以下方式存储数据
{
“兴趣”:[“运动”,“汽车”]
}
所以如果你直接 java 列出它是行不通的,所以一个选择是将 java 数组转换成一个字符串,可以将其视为 json 数组,如 ["sport ", "汽车"] 或
使用 json 用于创建 json 数组的对象,如下所示
JSONArray interests = new JSONArray();
a.add(0, "sport");
a.add(0, "car");
findByInterest(interests.toString())
@Query("SELECT * FROM people where interest->'interests' ??| :array::text[] ")
List<People> findByInterest(@Param("array") String interest);
让我们踏上旅程...
我想达到的是
SELECT * FROM people WHERE interest->'interests' ?| ARRAY['sport','cars'];
进入我定义为
的存储库public interface PeopleRepository extends CrudRepository<People, UUID> {
@Query("SELECT * FROM people where interest->'interests' ??| :array::text[] ")
List<People> findByInterest(@Param("array") String interest);
最后select兴趣,就这样使用方法
findByInterest("{foo, beer}")
这是我经过一天的测试和 恕我直言真是MEH
我认为除此之外,最好的解决方案是可能的 (在运行时没有 sql 转换和字符串连接)
你能帮忙提供一个更“干净的代码”的解决方案吗? (抱歉,长篇post)
更新
我会详细说明我的问题。 我正在搜索类似
的内容@Query("SELECT * FROM people where interest->'interests' ??| :array::text[] ")
List<People> findByInterest(@Param("array") List<String> interest);
这可能吗?
JSON数组元素按以下方式存储数据
{ “兴趣”:[“运动”,“汽车”]
}
所以如果你直接 java 列出它是行不通的,所以一个选择是将 java 数组转换成一个字符串,可以将其视为 json 数组,如 ["sport ", "汽车"] 或 使用 json 用于创建 json 数组的对象,如下所示
JSONArray interests = new JSONArray();
a.add(0, "sport");
a.add(0, "car");
findByInterest(interests.toString())
@Query("SELECT * FROM people where interest->'interests' ??| :array::text[] ")
List<People> findByInterest(@Param("array") String interest);