我可以要求 JDBCTemplate 扩展列表参数以在 in() 子句中使用吗?

发布于 2024-09-17 20:01:08 字数 270 浏览 2 评论 0原文

我可以做这样的事情:

select * from mytable m where m.group_id in (?)

... 并传入要扩展为我的参数的参数列表或数组,即:

select * from mytable m where m.group_id in (1,2,3,4)

具体来说,我正在使用 Spring 和 JdbcTemplate/SimpleJdbcTemplate 类。

Can I do something like this:

select * from mytable m where m.group_id in (?)

... and pass in a list or array of arguments to be expanded in to my parameter, ie:

select * from mytable m where m.group_id in (1,2,3,4)

Specifically, I'm using Spring and the JdbcTemplate/SimpleJdbcTemplate classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奶茶白久 2024-09-24 20:01:08

您可以使用 NamedParameterJdbcTemplate 来完成此操作。

对于你的样本,它会是这样的:

NamedParameterJdbcTemplate db = ...;
List paramList = ...;

Map idsMap = Collections.singletonMap("ids", paramList);
db.query("select * from mytable m where m.group_id in (:ids)", idsMap);

You can do it by using NamedParameterJdbcTemplate.

With your sample it would go something like:

NamedParameterJdbcTemplate db = ...;
List paramList = ...;

Map idsMap = Collections.singletonMap("ids", paramList);
db.query("select * from mytable m where m.group_id in (:ids)", idsMap);
晨与橙与城 2024-09-24 20:01:08

抱歉,不能这样做。您可以自己编写一个方便的方法来做到这一点,但据我所知,没有像 Hibernate 这样的 setParameterList()

Sorry, can't do that. You can write yourself a convenience method to do that, but there's no setParameterList() like Hibernate, as far as I know.

风苍溪 2024-09-24 20:01:08

请找到下面的代码

public Collection<Employee> findByIds(List<String> ids) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);

        List<Employee> employees = namedParameterJdbcTemplate.query(
                "SELECT * FROM trn_employee where employee_id IN (:ids)",
                params,
                ParameterizedBeanPropertyRowMapper.newInstance(Employee.class));

        return employees;

    }

Please find the below code

public Collection<Employee> findByIds(List<String> ids) {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);

        List<Employee> employees = namedParameterJdbcTemplate.query(
                "SELECT * FROM trn_employee where employee_id IN (:ids)",
                params,
                ParameterizedBeanPropertyRowMapper.newInstance(Employee.class));

        return employees;

    }
蓝眼泪 2024-09-24 20:01:08

是的,您可以在 Spring 3 中使用命名参数。

请参阅http: //docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-in-clause

它应该采用任何基元列表并展开该列表。请注意,您的列表不要超过数据库支持的最大大小。 (Oracle 限制为 1000)。像这样的东西应该有效:

    List<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(2);
    ids.add(3);

    Map<String,Object>  params = new HashMap<String, Object>();

    String sql = "SELECT PERSON.ID, PERSON.USERNAME, PERSON.EMAIL_ADDRESS, PERSON.FIRST_NAME, PERSON.LAST_NAME, PERSON.ACCOUNT_STATUS FROM PERSON WHERE ID IN (:ids)";
    params.put("ids",ids);

    return getSimpleJdbcTemplate().query(sql, rowMapper, params);

Yes you can in Spring 3 using a named parameter.

See http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-in-clause

It should take any list of primitives and expand the list. Just be careful that your list does not go over the max size your DB supports. (Oracle limit is 1000). Something like this should work:

    List<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(2);
    ids.add(3);

    Map<String,Object>  params = new HashMap<String, Object>();

    String sql = "SELECT PERSON.ID, PERSON.USERNAME, PERSON.EMAIL_ADDRESS, PERSON.FIRST_NAME, PERSON.LAST_NAME, PERSON.ACCOUNT_STATUS FROM PERSON WHERE ID IN (:ids)";
    params.put("ids",ids);

    return getSimpleJdbcTemplate().query(sql, rowMapper, params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文