java 使用 Spring JdbcTemplate 进行多个数据库操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2564128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using Spring JdbcTemplate for multiple database operations
提问by Joel Carranza
I like the apparent simplicity of JdbcTemplate but am a little confused as to how it works. It appears that each operation (query() or update()) fetches a connection from a datasource and closes it.
我喜欢 JdbcTemplate 明显的简单性,但对它的工作原理有点困惑。似乎每个操作(查询()或更新())从数据源获取连接并关闭它。
Beautiful, but how do you perform multiple SQL queries within the same connection?
很漂亮,但是如何在同一个连接中执行多个 SQL 查询呢?
I might want to perform multiple operations in sequence (for example SELECT followed by an INSERT followed by a commit) or I might want to perform nested queries (SELECT and then perform a second SELECT based on result of each row).
我可能想要按顺序执行多个操作(例如 SELECT 后跟一个 INSERT 后跟一个提交),或者我可能想要执行嵌套查询(SELECT 然后根据每行的结果执行第二个 SELECT)。
How do I do that with JdbcTemplate. Am I using the right class?
我如何使用 JdbcTemplate 做到这一点。我是否使用了正确的课程?
回答by skaffman
how do you perform multiple SQL queries within the same connection?
如何在同一个连接中执行多个 SQL 查询?
The correct answer here is "use transactions". If you begin transaction and then perform multiple operations with JdbcTemplate, each of those operations will be within the scope of the transaction, and therefore are guaranteed to use the same connection.
这里的正确答案是“使用交易”。如果您开始事务,然后使用 执行多个操作JdbcTemplate,则这些操作中的每一个都将在事务范围内,因此保证使用相同的连接。
If you don't want to get involved with transactions, then the alternative is to use the more primitive operations on JdbcTemplate, like execute(ConnectionCallback action), where you supply an instance of ConnectionCallbackwhich is given a Connection, on which you can then perform any operations you choose. Of course, but doing this you don't get JdbcTemplate's help in any of the actual operations.
如果您不想参与事务,那么另一种选择是在 上使用更原始的操作JdbcTemplate,例如execute(ConnectionCallback action),您提供一个实例,ConnectionCallback其中给定 a Connection,然后您可以在该实例上执行您选择的任何操作。当然,但是这样做您JdbcTemplate在任何实际操作中都得不到帮助。
Transactions are really quite easy in Spring, you should look into using them (see above link).
事务在 Spring 中真的很容易,你应该考虑使用它们(见上面的链接)。
回答by cletus
I assume you want transactions? If so, take a look at Spring, JdbcTemplate and Transactions.
我假设你想要交易?如果是这样,请查看Spring、JdbcTemplate 和 Transactions。
On a side note, I would suggest you take a look at Ibatis. Spring JDBC seems convenient but it has one major issue: the default mapping of result sets to objects uses Spring classes, which is actually really slow when dealing with large result sets. You can get around this by writing your own row mappers for these queries but personally I don't want to be writing this kind of boilerplate.
另外,我建议你看看Ibatis。Spring JDBC 看起来很方便,但它有一个主要问题:结果集到对象的默认映射使用 Spring 类,这在处理大型结果集时实际上很慢。您可以通过为这些查询编写自己的行映射器来解决这个问题,但我个人不想编写这种样板。
To give you an example of the difference: I had one query take 50 seconds with the Spring reflection-based row mapper that took 2 seconds with a hand coded row mapper.
举一个不同的例子:我有一个查询需要 50 秒,使用基于 Spring 反射的行映射器,而手动编码的行映射器需要 2 秒。
Also, Spring JDBC uses inline SQL. In Java this is fairly ugly as Java (annoyingly) doesn't have a good multi-line String format.
此外,Spring JDBC 使用内联 SQL。在 Java 中,这是相当丑陋的,因为 Java(令人讨厌的)没有好的多行字符串格式。

