java - 设置 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
。
有人可以帮助我做错什么吗?
解决方案
我已经尝试过了,它有效:
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());
}
推荐阅读
- java - 文本卡在 netbeansIDE 中的不可编辑行中
- javascript - 如何使用 javascript 从表单返回 json?
- laravel - 在 API 中间件中检索登录用户
- r - 如何更改 coxph R 中的“基线风险函数”
- javascript - 根据屏幕上显示的幻灯片更改对象的 div 类
- swift - 如何更新核心数据中的值?
- javascript - 会话超时模式隐藏在已经打开的对话框后面
- mysql - MySql 输出到 csv 不起作用 ERROR 1045 (28000)
- python - Python - 从 numpy 表创建字典以显示列表
- c++ - `sizeof` C++ 应用程序中的所有类型