[英]Choosing between jdbcTemplate.update(String sql,Object[] args,int[] argTypes) and jdbcTemplate.update(String sql,Object[] args)
I'm learning the combination of Spring Boot and jdbcTemplate for some basic crud operations, and trying to better understand which update method I should choose. 我正在学习Spring Boot和jdbcTemplate的结合以进行一些基本的操作,并试图更好地理解我应该选择哪种更新方法。
I understand that the following two class approaches (adapted from this post ) will write the same record to the database. 我知道以下两种类方法(改编自本文 )会将相同的记录写入数据库。
Example 1: 范例1:
public class InsertDemo {
private static final String sql =
"INSERT INTO records (title, " +
" release_date, " +
" artist_id, " +
" label_id, " +
" created) " +
"VALUES (?, ?, ?, ?, ?)";
private DataSource dataSource;
public InsertDemo(DataSource dataSource) {
this.dataSource = dataSource;
}
public void saveRecord(String title, Date releaseDate,
Integer artistId, Integer labelId) {
JdbcTemplate template = new JdbcTemplate(this.dataSource);
Object[] params = new Object[] {
title, releaseDate, artistId, labelId, new Date()
};
int[] types = new int[] {
Types.VARCHAR,
Types.DATE,
Types.INTEGER,
Types.INTEGER,
Types.DATE
};
int row = template.update(sql, params, types);
System.out.println(row + " row inserted.");
}
Example 2: 范例2:
public class InsertDemo {
private static final String sql =
"INSERT INTO records (title, " +
" release_date, " +
" artist_id, " +
" label_id, " +
" created) " +
"VALUES (?, ?, ?, ?, ?)";
private DataSource dataSource;
public InsertDemo(DataSource dataSource) {
this.dataSource = dataSource;
}
public void saveRecord(String title, Date releaseDate,
Integer artistId, Integer labelId) {
JdbcTemplate template = new JdbcTemplate(this.dataSource);
Object[] params = new Object[] {
title, releaseDate, artistId, labelId, new Date()
};
int row = template.update(sql, params);
System.out.println(row + " row inserted.");
}
but I'm unclear on why I would/should use the first one that specifies argument types. 但是我不清楚为什么/应该使用第一个指定参数类型的参数。 I've read the javadoc but I'm still not sure why I would need to specify the types. 我已经阅读了Javadoc,但仍不确定为什么需要指定类型。 What am I missing? 我想念什么?
Setting argument type provides correctness and optimisation (slight) for the underlying SQL statement. 设置参数类型可为基础SQL语句提供正确性和优化(轻微)。 (The JdbcTemplate
internally builds a PreparedStatement
and sets values to it using provided/derived types). ( JdbcTemplate
内部构建一个PreparedStatement
并使用提供的/派生的类型为其设置值)。
In your example, if you don't specify Types
array, they will be set as SqlTypeValue.TYPE_UNKOWN
which eventually be guessed or resolved as; 在您的示例中,如果不指定Types
数组,则将它们设置为SqlTypeValue.TYPE_UNKOWN
,最终将其猜测或解析为;
if (sqlType == SqlTypeValue.TYPE_UNKNOWN || sqlType == Types.OTHER) {
if (isStringValue(inValue.getClass())) {
ps.setString(paramIndex, inValue.toString());
}
else if (isDateValue(inValue.getClass())) {
ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
}
else if (inValue instanceof Calendar) {
Calendar cal = (Calendar) inValue;
ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
}
else {
// Fall back to generic setObject call without SQL type specified.
ps.setObject(paramIndex, inValue);
}
}
Have a look at org.springframework.jdbc.core.StatementCreatorUtils#setValue
看看org.springframework.jdbc.core.StatementCreatorUtils#setValue
So it's a good practice to set the arg types. 因此,设置arg类型是一个好习惯。
Without Type
parameter: 没有Type
参数:
sql SQL containing bind parameters args arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); 包含绑定参数args参数的sql SQL绑定到查询(将其保留在PreparedStatement中以猜测相应的SQL类型); may also contain SqlParameterValue objects which indicate not only the argument value but also the SQL type and optionally the scale 可能还包含SqlParameterValue对象,这些对象不仅指示参数值,还指示SQL类型以及可选的小数位数
With Type
parameter: 使用Type
参数:
sql SQL containing bind parameters args arguments to bind to the query argTypes SQL types of the arguments (constants from java.sql.Types) 包含绑定参数args参数的sql SQL绑定到查询的argTypes参数的SQL类型(来自java.sql.Types的常量)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.