简体   繁体   English

如何使用commons dbutils生成密钥?

[英]How to get generated keys with commons dbutils?

我不明白如何使用commons-dbutils获取自动生成的密钥?

You can use QueryRunner#insert() . 您可以使用QueryRunner#insert() Below is an example. 以下是一个例子。 Given a table called users , which has an auto generated primary key column and a varchar column called username , you can do something like this: 给定一个名为users的表,它具有自动生成的主键列和名为usernamevarchar列,您可以执行以下操作:

DataSource dataSource = ... // however your app normally gets a DataSource 
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "insert into users (username) values (?)";
long userId = queryRunner.insert(sql, new ScalarHandler<Long>(), "test");

As a matter of fact I think it cannot be done with the current version of common-dbutils. 事实上,我认为使用当前版本的common-dbutils无法做到这一点。 A few months ago, when I was working for another company, I extented the QueryRunner with my own implementation. 几个月前,当我在另一家公司工作时,我用自己的实现扩展了QueryRunner。

The request has been submitted to the DbUtils project, and there you can even find a viable implementation which I guess you could copy if you really need it. 该请求已经提交给DbUtils项目,在那里你甚至可以找到一个可行的实现,我猜你可以复制它,如果你真的需要它。

https://issues.apache.org/jira/browse/DBUTILS-54 https://issues.apache.org/jira/browse/DBUTILS-54

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

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