使用 JdbcTemplate 插入多行
如何使用 JdbcTemplate 在 mySQL 上运行。在这种情况下,可扩展意味着:
- 服务器上仅执行一个 SQL 语句
- 在适用于任意数量行的
。语句如下:
INSERT INTO myTable (foo, bar) VALUES ("asdf", "asdf"), ("qwer", "qwer")
假设我有一个带有 foo
和 bar
字段的 POJO 列表。我意识到我可以迭代列表并执行:
jdbcTemplate.update("INSERT INTO myTable(foo, bar) VALUES (?, ?)", paramMap)
但这并没有达到第一个标准。
我相信我也可以执行:
jdbcTemplate.batchUpdate("INSERT INTO myTable(foo, bar) VALUES (?, ?)", paramMapArray)
但据我所知,这只会编译 SQL 一次并执行多次,再次失败第一个标准。
最后一种可能性,似乎同时满足了这两个标准,就是简单地用 StringBuffer 自己构建 SQL,但我想避免这种情况。
How can I execute the following SQL in a scalable way using JdbcTemplate running on mySQL. In this case, scalable means:
- Only one SQL statement is executed on the server
- it works for any number of rows.
Here's the statement:
INSERT INTO myTable (foo, bar) VALUES ("asdf", "asdf"), ("qwer", "qwer")
Assume that I have a list of POJO's with foo
and bar
fields. I realize that I could just iterate over the list and execute:
jdbcTemplate.update("INSERT INTO myTable(foo, bar) VALUES (?, ?)", paramMap)
but that doesn't doesn't accomplish the first criterion.
I believe I could also execute:
jdbcTemplate.batchUpdate("INSERT INTO myTable(foo, bar) VALUES (?, ?)", paramMapArray)
but from what I can tell, that will just compile the SQL once and execute it multiple times, failing the first criterion again.
The final possibility, which seems to pass both criteria, would be to simply build the SQL myself with a StringBuffer
, but I'd like to avoid that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以像下面一样使用BatchPreparedStatementSetter。
You can use BatchPreparedStatementSetter like below.
在我看来,JdbcTemplate 的batchUpdate() 方法在这种情况下可能会有帮助(从这里复制 http://www.mkyong.com/spring/spring-jdbctemplate-batchupdate-example/):
It looks to me that batchUpdate() method of JdbcTemplate could be helpful in this case (copied from here http://www.mkyong.com/spring/spring-jdbctemplate-batchupdate-example/):
多行插入(使用“行值构造函数”)实际上是 SQL-92 标准的一部分。看
http://en.wikipedia.org/wiki/Insert_(SQL)#Multirow_inserts。
有些数据库不支持此语法,但许多数据库都支持。根据我的经验,Derby/Cloudscape、DB2、Postgresql 和较新的 Hypersonic 2.*+ 版本确实支持此功能。
您对使其作为PreparedStatement工作的担忧是可以理解的,但我见过类似的情况,其中Spring JDBC确实自动处理某些查询的项目集合(例如在(?)中的位置),但我不能保证这种情况。
我确实找到了一些可能有用的信息(无法在这篇文章中添加第二个链接)
这可能会有一些帮助。
我可以告诉您,从最严格的意义上讲,您的第二个要求(适用于任意数量的参数)可能不可能得到满足:我使用的每个数据库确实都会施加会发挥作用的查询长度限制。
Multirow inserts (using "row value constructors") are in fact part of the SQL-92 standard. See
http://en.wikipedia.org/wiki/Insert_(SQL)#Multirow_inserts.
Some databases do not support this syntax, but many do. In my experience Derby/Cloudscape, DB2, Postgresql and the newer Hypersonic 2.*+ releases do support this.
Your concern about getting this to work as a PreparedStatement is understandable, but I've seen similar cases where Spring JDBC does automatically handle a Collection of items for certain queries (like where in (?)), but I cannot vouch for this case.
I did find some possibly helpful information at (can't add second link to this post)
which might be of some help.
I can tell you that its probably not possible for your second requirement (works for any number of arguments) to be met in the most strict sense: every database I've used does impose query length limitations that would come into play.
您还可以尝试使用 jdbcInsert.executeBatch(sqlParamSourceArray)
you can also try with jdbcInsert.executeBatch(sqlParamSourceArray)