天天看點

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[]{參數}這種方法

繼續閱讀