繁体   English   中英

如何在Spring JDBCTemplate中提供SQL Cast?

[英]How to provide SQL cast in Spring JDBCTemplate?

我在postgres中有一张表,用于存储IP地址(inet数据类型)。 我正在查询以下内容-

NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);


    Map<String, Object> params = new HashedMap();
        Collection<Object> ipList = new LinkedList<>();
        ips.add("1.2.3.4");
        ips.add("5.6.7.8");

    namedParameterJdbcTemplate.query("select * from myTable source_ip in (:ipList)",
            params, new RowMapper<Object>() {
              @Nullable @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                System.out.println(rs.toString());
                return null;
              }
            });

上面的代码片段给了我DB异常- No operator matches the given name and argument type(s). You might need to add explicit type casts. No operator matches the given name and argument type(s). You might need to add explicit type casts.

我查看了NamedParameterJdbcTemplate.class源代码,发现没有办法指定参数数据类型。

有任何想法吗?

现在,我已经扩展了getPreparedStatementCreator中的NamedParameterJdbcTemplate方法,以便将sql NamedParameterJdbcTemplate变量附加到“?”上。 在准备好的声明中

我想您错过了将ipList添加到参数映射:

Map<String, Object> params = new HashedMap<>();
Collection<Object> ipList = new ArrayList<>();
ipList.add("1.2.3.4");
ipList.add("5.6.7.8");
params.put("ipList", ipList);

但我建议使用:

MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("ipList", Arrays.asList("1.2.3.4", "5.6.7.8"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM