如何生成动态的“(...)"?通过Spring JdbcTemplate的sql列表? [英] How to generate a dynamic "in (...)" sql list through Spring JdbcTemplate?

查看:589
本文介绍了如何生成动态的“(...)"?通过Spring JdbcTemplate的sql列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Jdbc模板在SQL查询中生成任意"in()"列表:

Is it possible to generate arbitrary "in ()" lists in a SQL query through Jdbc template:

示例:

从t中选择*,其中(#)中的c",但是#"可以是仅在运行时才知道的任意值列表.

"select * from t where c in (#)" , However '#' could be an arbitrary list of values only known at runtime.

推荐答案

是的,如果您在命名参数中使用NamedParameterJdbcTemplateSimpleJdbcTemplate,则在Spring中是可能的.列表参数可以设置为java.util.List:

Yes, it's possible in Spring if you use NamedParameterJdbcTemplate or SimpleJdbcTemplate with named parameters. List parameter can be set as a java.util.List:

List<String> list = new ArrayList<String>();

list.add("A");
list.add("B");
list.add("C");

List<SomeObject> result = simpleJdbcTemplate.query("SELECT * FROM t WHERE c in (:list)",
    new RowMapper<SomeObject>() { ... },
    Collections.singletonMap("list", list));

在这种情况下,当用? s替换命名参数时,Spring会根据实际列表的大小在内部创建带有所需占位符数量的SQL查询.

In this case Spring internally creates the SQL query with the required number of placeholders based on the size of the actual list when replacing named parameters with ?s.

这篇关于如何生成动态的“(...)"?通过Spring JdbcTemplate的sql列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆