JdbcTemplate.queryForList()方法各不同参数的详解

public <T> List<T> queryForList(
String sql,
Class<T> elementType,
Object... args
)throws DataAccessException

官方文档解释:

Parameters:
sql - SQL query to execute
elementType - the required type of element in the result list (for example, Integer.class)
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also contain SqlParameterValue objects which indicate not only the argument value but also the SQL type and optionally the scale

我的理解

String sql, // 查询用的sql句
Object[] args, // sql句中的参数
int[] argTypes, // sql句中的参数的数据类型(Java类型)
Class elementType // list装载结果的元数据类型

官方文档中最后的一个开变参数中讲到最后一个参数中可以包含SqlParameterValue对象,也就是说如果要通过这种方式给sql语句传参数,要将参数的类型转换成Object类型,如使用 Object[]{参数}这种方法

猜你喜欢

转载自blog.csdn.net/qq_37355731/article/details/61933820