spring JdbcTemplate如何识别数据类型? [英] How spring JdbcTemplate recognizes data types?
问题描述
将spring JdbcTemplate与预处理语句一起使用时,我们既可以单独设置参数的值,也可以仅传递对象数组.
When using spring JdbcTemplate with prepared statements we can either set values of the parameters individually or just pass an array of objects.
jdbctemplate.update(sql, arg1, arg2);
或
jdbctemplate.update(sql, new Object[]{arg1, arg2});
这两种方法都有效.但是我想知道jdbctemplate如何知道在作为对象数组传递时将数据强制转换为与数据库列的类型匹配.
Both of the methods are working. But I want to know how jdbctemplate knows to cast the data to match with the type of the database column when passed as an Object array.
两种方法在性能上有区别吗?
And is there a performance difference in two methods?
如何记录在数据库上执行的最终查询.为org.springframework.jdbc软件包启用DEBUG日志对我不起作用.
How can I log the final query executed on the database. Enabling DEBUG logs for org.springframework.jdbc package didn't work for me.
推荐答案
对JdbcTemplate.update
的两次调用之间没有区别,实际上,您的两个调用都使用相同的方法.只有一个
There is no difference between the two calls to JdbcTemplate.update
, In Fact both of your calls go to the same method. There is only one update method in JdbcTemplate
具有以下签名
public int update(String sql, Object... args) throws DataAccessException
如您所见,最后一个参数是Java变量参数(...),而不是数组.因此,您可以将单个值或一个对象数组赋予同一变量参数.
As you can see the last argument is a Java Variable Argument (...) not an Array. So you can either give individual values or an Object array to the same Variable Argument.
关于JdbcTemplate
如何知道如何将数据转换为与目标数据类型匹配的问题,它只是创建一个PreparedStatement
并根据提交变量参数的顺序和值调用PreparedStatement.set***
方法,并且实际上仅检查给定的值是String
,Date
还是Calendar
,并调用PreparedStatement.setString
或PreparedStatement.setTimestamp
.对于其他所有内容,只需PreparedStatement.setObject
For your question of How JdbcTemplate
knows to cast the data to match with the destination data type, It just creates a PreparedStatement
and calls PreparedStatement.set***
methods based with the Order of Submitted Variable Arguments and value and actually only checks whether the given values are of String
, Date
or Calendar
and calls PreparedStatement.setString
or PreparedStatement.setTimestamp
. For everything else just PreparedStatement.setObject
这篇关于spring JdbcTemplate如何识别数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!