public static void aa(Object... args){ }
在这个方法里面Object... 代表什么?
意思就是参数是可变的,意思就是说,你在调用这个方法的时候你可以给他传一个String类型的参数,也可以给他传一个Int类型的参数,或者多个不同类型的参数,很灵活的。
如:
public class Test {
public static void main(String[] args) {
int aa = 0;
aa("sdfsf","String2","ddddd",aa);
}
public static void aa(Object... args){
System.out.println(args[1]);
System.out.println("asfdlsdlkfs");
}
}
执行后输出: String2
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[]{参数}这种方法