简体   繁体   English

使用Spring JdbcTemplate进行多个数据库操作

[英]using Spring JdbcTemplate for multiple database operations

I like the apparent simplicity of JdbcTemplate but am a little confused as to how it works. 我喜欢JdbcTemplate的简单易用性,但我对它的工作方式感到有些困惑。 It appears that each operation (query() or update()) fetches a connection from a datasource and closes it. 似乎每个操作(query()或update())从数据源获取连接并关闭它。

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. 我如何使用JdbcTemplate执行此操作。 Am I using the right class? 我使用合适的班级吗?

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 ConnectionCallback which is given a Connection , on which you can then perform any operations you choose. 如果您不想涉及事务,那么另一种方法是在JdbcTemplate上使用更原始的操作,比如execute(ConnectionCallback action) ,在这里你提供一个ConnectionCallback的实例,它给了一个Connection ,然后你就可以了执行您选择的任何操作。 Of course, but doing this you don't get JdbcTemplate 's help in any of the actual operations. 当然,但这样做你不会在任何实际操作中获得JdbcTemplate的帮助。

Transactions are really quite easy in Spring, you should look into using them (see above link). Spring中的事务非常简单,您应该考虑使用它们(参见上面的链接)。

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 . 另外,我建议你看看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. Spring JDBC似乎很方便,但它有一个主要问题:结果集到对象的默认映射使用Spring类,这在处理大型结果集时实际上非常慢。 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. 您可以通过为这些查询编写自己的行映射器来解决这个问题,但我个人并不想编写这种样板文件。

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. 为了给你一个不同的例子:我用一个基于Spring反射的行映射器,用一个手动编码的行映射器花了2秒,用一个查询花了50秒。

Also, Spring JDBC uses inline SQL. 此外,Spring JDBC使用内联SQL。 In Java this is fairly ugly as Java (annoyingly) doesn't have a good multi-line String format. 在Java中,这是相当丑陋的,因为Java(令人讨厌)没有良好的多行String格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM