首页 > 解决方案 > 设置 SQL 语句参数值未正确替换为值

问题描述

我在我的 Java 代码中构建 SQL 查询,如下所示:

return "where 1 = 1" +
        (!isNullOrEmpty(model.getAccount()) ? " and a.accounts ->> 'id' = :id " : "") +

我需要将表格从更改a.accounts ->> 'id' = ?a.accounts @> '{"id": "XXXX"}'

所以我尝试了:

 return "where 1 = 1" +
                (!isNullOrEmpty(search.getAccount()) ? " and a.accounts @> '{\"id\": \":id\"}' " : "") +

但这并不能取代 param id

有人可以帮助我做错什么吗?

标签: javaspring-jdbc

解决方案


我已经尝试过了,它有效:

public List<Person> findWithContains(int id) {

    Map<String, Object> params = new HashMap<>();
    params.put("accounts", "{ \"id\": " + id + " }");

    return template.query(
            "select * from people where accounts @> CAST(:accounts as jsonb)", 
            params,
            new PersonRowMapper());
}

推荐阅读