Why code gives an error
As mentioned in the docs queryForList Method have following implementations available:
queryForList(String sql, Map<String,?> paramMap)
.
queryForList(String sql, Map<String,?> paramMap, Class<T> elementType)
queryForList(String sql, SqlParameterSource paramSource)
queryForList(String sql, SqlParameterSource paramSource, Class<T> elementType)
None of these implementations matches the parameters used in the given implementation. Thus, we end up with this error:
Error: Cannot resolve method 'queryForList(java.lang.String, java.lang.Class<java.lang.Integer>)'
The idea behind passing missing parameter
The key idea behind passing a Map or ParameterSource is to have a dynamic query where we can put in values later on.
Eg:
String query = "Select :columnName from table";
Map<String,String> map = new HashMap<>();
map.put("columnName", "userName");
When this map is passed along with the query String, internally it is used to replace placeholders with the values from the map.
How to fix the code
There are two ways you can fix this:
- Just pass null
This is not the best way of fixing the problem is definitely not recommended for a production code. But, this can be used if there is no placeholder in the query string.
Code:
List<Integer> data = namedParameterJdbcTemplate.queryForList(query, null, Integer.class);
- Create and pass an empty Map or SqlParameterSource
You already have a MapSqlParameterSource called customerParameters in your code. Simply pass it while calling queryForList()
Code:
List<Integer> data = namedParameterJdbcTemplate.queryForList(query, customerParameters, Integer.class);