Java 如何使用 Ibatis 返回插入的 ID(使用 RETURNING 关键字)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1769688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto return ids on Inserts with Ibatis ( with RETURNING keyword )
提问by Christoph
I'm using iBatis/Java and Postgres 8.3.
When I do an insert in ibatis i need the id returned.
I use the following table for describing my question:CREATE TABLE sometable ( id serial NOT NULL, somefield VARCHAR(10) );
The Sequence sometable_id_seq
gets autogenerated by running the create statement.
我正在使用 iBatis/Java 和 Postgres 8.3。当我在 ibatis 中插入时,我需要返回 id。
我使用下表来描述我的问题:通过运行 create 语句自动生成CREATE TABLE sometable ( id serial NOT NULL, somefield VARCHAR(10) );
序列sometable_id_seq
。
At the moment i use the following sql map:
目前我使用以下 sql 映射:
<insert id="insertValue" parameterClass="string" >
INSERT INTO sometable ( somefield ) VALUES ( #value# );
<selectKey keyProperty="id" resultClass="int">
SELECT last_value AS id FROM sometable_id_seq
</selectKey>
</insert>
It seems this is the ibatis way of retrieving the newly inserted id. Ibatis first runs a INSERT statement and afterwards it asks the sequence for the last id.
I have doubts that this will work with many concurrent inserts. ( discussed in this question)
似乎这是检索新插入的 id 的 ibatis 方式。Ibatis 首先运行一个 INSERT 语句,然后它向序列询问最后一个 id。
我怀疑这是否适用于许多并发插入。(在这个问题中讨论)
I'd like to use the following statement with ibatis:INSERT INTO sometable ( somefield ) VALUES ( #value# ) RETURNING id;
我想在 ibatis 中使用以下语句:INSERT INTO sometable ( somefield ) VALUES ( #value# ) RETURNING id;
But when i try to use it within a <insert>
sqlMap ibatis does not return the id. It seems to need the <selectKey>
tag.
但是当我尝试在<insert>
sqlMap 中使用它时,ibatis 不会返回 ID。它似乎需要<selectKey>
标签。
So here comes the question:
那么问题来了:
How can i use the above statement with ibatis?
如何在ibatis中使用上述语句?
采纳答案by Christoph
The <selectKey>
element is a child of the <insert>
element and its content is executed beforethe main INSERT
statement. You can use two approaches.
该<selectKey>
元素是的子<insert>
元素,其含量在执行之前主要INSERT
说法。您可以使用两种方法。
Fetch the key after you have inserted the record
插入记录后获取密钥
This approach works depending on your driver. Threading can be a problem with this.
这种方法取决于您的驱动程序。线程可能是一个问题。
Fetching the key before inserting the record
在插入记录之前获取密钥
This approach avoids threading problems but is more work. Example:
这种方法避免了线程问题,但工作量更大。例子:
<insert id="insert">
<selectKey keyProperty="myId"
resultClass="int">
SELECT nextVal('my_id_seq')
</selectKey>
INSERT INTO my
(myId, foo, bar)
VALUES
(#myId#, #foo#, #bar#)
</insert>
On the Java side you can then do
在 Java 方面,您可以执行以下操作
Integer insertedId = (Integer) sqlMap.insert("insert", params)
This should give you the key selected from the my_id_seq
sequence.
这应该为您提供从my_id_seq
序列中选择的键。
回答by leonidv
Here is simple example:
这是一个简单的例子:
<statement id="addObject"
parameterClass="test.Object"
resultClass="int">
INSERT INTO objects(expression, meta, title,
usersid)
VALUES (#expression#, #meta#, #title#, #usersId#)
RETURNING id
</statement>
And in Java code:
在 Java 代码中:
Integer id = (Integer) executor.queryForObject("addObject", object);
object.setId(id);
This way more better than use :
这种方式比使用更好:
- It's simpler;
- It have not requested to know sequence name (what usually hidden from postgresql developers).
- 更简单;
- 它没有要求知道序列名称(通常对 postgresql 开发人员隐藏的内容)。