How to get generated keys with commons dbutils?

夙愿已清 提交于 2019-12-01 15:31:09

问题


I don't understand how to get auto generated keys with commons-dbutils?


回答1:


You can use 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:

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");



回答2:


As a matter of fact I think it cannot be done with the current version of common-dbutils. A few months ago, when I was working for another company, I extented the QueryRunner with my own implementation.

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.

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



来源:https://stackoverflow.com/questions/8705036/how-to-get-generated-keys-with-commons-dbutils

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!