2017-11-27 5 views
0

私はIPアドレス(inetデータ型)を格納するテーブルをpostgresに持っています。私は、次のようにそれを照会しています - コード片の上Spring JDBCTemplateでSQLキャストを提供するには?

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は、私はNamedParameterJdbcTemplate.classソースコードに見て、パラメータのデータ型を指定する方法を発見したNo operator matches the given name and argument type(s). You might need to add explicit type casts.

をexception-与えます。

アイデア?

答えて

0

今のところ、拡張メソッドをgetPreparedStatementCreatorNamedParameterJdbcTemplateに拡張しました。これは、 "?"フィールドにsqlキャスト変数を追加するようになりました。プリペアドステートメント

0

に私はあなたがパラメータマップに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")); 
関連する問題