2016-05-12 44 views
0

我想在SQL語句中使用參數調用標籤:春SQL參數映射(陣列)

SELECT * FROM reply WHERE array[:tags] @> array[2293,2294]; 

我傳遞參數,並通過執行以下語句:

MapSqlParameterSource params = new MapSqlParameterSource(); 
Integer[] a={2293,2294}; 
params.addValue("tags", Arrays.asList(a)); 
namedParameterJdbcTemplate.queryForObject(statement, params, String.class); 

但引發錯誤:

class org.springframework.dao.InvalidDataAccessApiUsageException No value supplied for the SQL parameter 'tags]': No value registered for key 'tags]'

如果我改變聲明:

SELECT * FROM reply WHERE '{:tags}' @> array[2293,2294]; 

它引發錯誤:

class org.springframework.dao.DataIntegrityViolationException PreparedStatementCallback; SQL [SELECT * FROM reply WHERE '{:tags}' @> array[2293,2294]; ERROR: invalid input syntax for integer: ":tags" Position: 72; nested exception is org.postgresql.util.PSQLException: ERROR: invalid input syntax for integer: ":tags" Position: 72

如果我改變聲明:

SELECT * FROM reply WHERE (:tags) @> array[2293,2294]; 

它引發錯誤:

class org.springframework.jdbc.BadSqlGrammarException PreparedStatementCallback; bad SQL grammar [SELECT * FROM reply WHERE (?, ?) @> array[2293,2294]; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: record @> integer[] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 81

+0

看起來像有一個類似的問題:http://stackoverflow.com/questions/31925109/how-to-pass-an-array-parameter-using-namedparameterjdbctemplate-to-a-function-wh –

回答

1

從不同的括號:標籤解決問題:

SELECT * FROM reply WHERE array[ :tags ] @> array[2293,2294];