如何使用NamedParameterJdbcTemplate查询单个列获取Integer类型列表
问题原因
NamedParameterJdbcTemplate没有提供仅传入SQL语句、返回值类型两个参数的queryForList重载方法,所以编译阶段无法匹配到对应方法触发报错。- 你已经构造了存储查询参数的
MapSqlParameterSource对象,但调用方法时没有传入该参数对象,导致参数匹配失败。
正确实现
String customerName = "JOE"; MapSqlParameterSource customerParameters = new MapSqlParameterSource(); customerParameters.addValue("CustomerName", customerName); private static final String query = "SELECT Customer_Id From dbo.Customers WHERE Customer_Name = :CustomerName"; // 调用queryForList时按顺序传入SQL、参数源、返回值类型三个参数 List<Integer> data = namedParameterJdbcTemplate.queryForList(query, customerParameters, Integer.class);
补充说明
你用到的这个重载方法的官方签名如下:
<T> List<T> queryForList(String sql, SqlParameterSource paramSource, Class<T> elementType)
- 参数1:待执行的SQL语句,支持命名参数占位符
- 参数2:存储命名参数对应值的参数源对象
- 参数3:返回列表中元素的Class类型
内容的提问来源于stack exchange,提问作者mattsmith5
相关产品推荐
相关产品推荐

