簡體   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