在使用Spring JDBC batchUpdate时,有没有获得生成的密钥? [英] Is there anyway to get the generated keys when using Spring JDBC batchUpdate?

查看:92
本文介绍了在使用Spring JDBC batchUpdate时,有没有获得生成的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JDBC并希望批量插入,但我需要为下一轮插入生成的密钥 - 无论如何要实现此目的吗?

I am using JDBC and want to batch insert, but I need the generated keys for the next round of inserts - is there anyway to accomplish this?

MapSqlParameterSource [] batchArgs ....

DAL.getNamedParameterTemplate()。batchUpdate(INSERT INTO ...,batchArgs);

谢谢

推荐答案

Spring框架人员尝试解决方案解决此问题。但是,当很明显无法保证解决方案适用于所有JDBC驱动程序时,他们放弃了尝试。这是因为JDBC规范不保证在批量更新后生成的密钥可用。 JDBC驱动程序可以根据需要随意实现此功能。在某些情况下,底层数据库可能不会返回生成的密钥,因此驱动程序无法支持此功能。

Spring framework folks attempted a solution to this problem. But they abandoned the attempt when it became apparent that there is no way to guarantee that the solution will work with all JDBC Drivers. This is because the JDBC spec doesn't guarantee that the generated keys will be made available after a batch update. JDBC drivers are free to implement this feature as they see fit. In some cases the underlying database might not return the generated keys making it impossible for the driver to support this feature.

因此,即使您直接使用JDBC,您也将需要检查数据库和JDBC驱动程序是否使生成的密钥可用。

So even if you are directly working with JDBC, you will need to check whether your database and JDBC driver makes the generated keys available.

我记得我能够通过MySQL 5.0 JDBC驱动程序实现这一目标,但从未在我们的生产应用程序中集成解决方案,因为我们必须支持旧版本的MySQL作为好吧。

I remember I was able to achieve this with MySQL 5.0 JDBC driver with some effort but never integrated the solution in our production application as we had to support older versions of MySQL as well.

这篇关于在使用Spring JDBC batchUpdate时,有没有获得生成的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆